Skip to content

Commit

Permalink
Include module lists in the output
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip committed Sep 2, 2020
1 parent 75ee87f commit 7275101
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Changed

- Now it includes the used module list in the output.

### Fixed

- `--check` option will work with `{ path = ".." }` dependencies.
59 changes: 59 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ syn = { version = "1.0.39", features = ["full"] }
tempfile = "3.1.0"
termcolor = "1.1.0"
toml_edit = "0.2.0"
url = "2.1.1"
86 changes: 68 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use anyhow::{anyhow, Context as _};
use quote::ToTokens as _;
use std::{collections::BTreeSet, path::PathBuf, str::FromStr};
use structopt::{clap::AppSettings, StructOpt};
use url::Url;

#[derive(StructOpt, Debug)]
#[structopt(
Expand Down Expand Up @@ -125,6 +126,10 @@ pub fn run(opt: Opt, ctx: Context<'_>) -> anyhow::Result<()> {

let code = &std::fs::read_to_string(&bin.src_path)?;

if syn::parse_file(code)?.shebang.is_some() {
todo!("shebang is currently not supported");
}

let edit = if let Some(Equipment {
extern_crate_name,
mods,
Expand Down Expand Up @@ -172,6 +177,46 @@ pub fn run(opt: Opt, ctx: Context<'_>) -> anyhow::Result<()> {

let mut edit = "".to_owned();

edit += "//! # Bundled libraries\n";
edit += "//!\n";
edit += "//! ## ";
let link = if matches!(&lib_package.source, Some(s) if s.is_crates_io()) {
format!(
"https://crates.io/{}/{}",
lib_package.name, lib_package.version
)
.parse::<Url>()
.ok()
} else {
lib_package.repository.as_ref().and_then(|s| s.parse().ok())
};
if let Some(link) = link {
edit += "[`";
edit += &lib_package.name;
edit += "`](";
edit += link.as_str();
edit += ")";
} else {
edit += "`";
edit += &lib_package.name;
edit += "` (private)";
}
edit += "\n";
edit += "//!\n";
for (mod_name, mod_content) in &mod_contents {
if mod_content.is_some() {
edit += "//! - `";
edit += &lib.name;
edit += "::";
edit += &mod_name.to_string();
edit += "` → `$crate::";
edit += &mod_name.to_string();
edit += "`\n";
}
}

edit += "\n";

for (i, s) in code.lines().enumerate() {
if i + 1 == span.start().line && i + 1 == span.end().line {
edit += &s[..span.start().column];
Expand Down Expand Up @@ -205,29 +250,34 @@ pub fn run(opt: Opt, ctx: Context<'_>) -> anyhow::Result<()> {
if oneline == Oneline::Mods {
edit += "\n";
for (mod_name, mod_content) in mod_contents {
edit += "#[allow(clippy::deprecated_cfg_attr)] #[cfg_attr(rustfmt, rustfmt::skip)]";
edit += " pub mod ";
edit += &mod_name.to_string();
edit += " { ";
edit += &mod_content
.parse::<proc_macro2::TokenStream>()
.map_err(|e| anyhow!("{:?}", e))?
.to_string();
edit += " }\n";
if let Some(mod_content) = mod_content {
edit += "#[allow(clippy::deprecated_cfg_attr)] ";
edit += "#[cfg_attr(rustfmt, rustfmt::skip)] ";
edit += "pub mod ";
edit += &mod_name.to_string();
edit += " { ";
edit += &mod_content
.parse::<proc_macro2::TokenStream>()
.map_err(|e| anyhow!("{:?}", e))?
.to_string();
edit += " }\n";
}
}
} else {
for (mod_name, mod_content) in mod_contents {
edit += "\npub mod ";
edit += &mod_name.to_string();
edit += " {\n";
for line in mod_content.lines() {
if !line.is_empty() {
edit += " ";
if let Some(mod_content) = mod_content {
edit += "\npub mod ";
edit += &mod_name.to_string();
edit += " {\n";
for line in mod_content.lines() {
if !line.is_empty() {
edit += " ";
}
edit += line;
edit += "\n";
}
edit += line;
edit += "\n";
edit += "}\n";
}
edit += "}\n";
}
}

Expand Down
59 changes: 31 additions & 28 deletions src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,41 +171,44 @@ pub(crate) fn parse_exactly_one_use(file: &syn::File) -> syn::Result<Option<Equi
pub(crate) fn read_mods(
src_path: &std::path::Path,
names: Option<&BTreeSet<String>>,
) -> anyhow::Result<BTreeMap<Ident, String>> {
) -> anyhow::Result<BTreeMap<Ident, Option<String>>> {
let file = syn::parse_file(&std::fs::read_to_string(src_path)?)?;

let mut contents = btreemap!();

for item in &file.items {
if let Item::Mod(item_mod) = item {
if names.map_or(true, |names| names.contains(&item_mod.ident.to_string())) {
if item_mod.content.is_some() {
todo!("TODO: inline mod");
}
if let Some(Meta::List(_)) = item_mod
.attrs
.iter()
.flat_map(|a| a.parse_meta())
.find(|m| matches!(m.path().get_ident(), Some(i) if i == "path"))
{
todo!("TODO: `#[path = \"..\"]`");
}
let paths = vec![
src_path
.with_file_name("")
.join(item_mod.ident.to_string())
.join("mod.rs"),
src_path
.with_file_name("")
.join(item_mod.ident.to_string())
.with_extension("rs"),
];
if let Some(path) = paths.iter().find(|p| p.exists()) {
let content = std::fs::read_to_string(path)?;
contents.insert(item_mod.ident.clone(), content);
let is_target = names.map_or(true, |names| names.contains(&item_mod.ident.to_string()));
if item_mod.content.is_some() {
todo!("TODO: inline mod");
}
if let Some(Meta::List(_)) = item_mod
.attrs
.iter()
.flat_map(|a| a.parse_meta())
.find(|m| matches!(m.path().get_ident(), Some(i) if i == "path"))
{
todo!("TODO: `#[path = \"..\"]`");
}
let paths = vec![
src_path
.with_file_name("")
.join(item_mod.ident.to_string())
.join("mod.rs"),
src_path
.with_file_name("")
.join(item_mod.ident.to_string())
.with_extension("rs"),
];
if let Some(path) = paths.iter().find(|p| p.exists()) {
let content = if is_target {
Some(std::fs::read_to_string(path)?)
} else {
bail!("none of `{:?}` found", paths);
}
None
};
contents.insert(item_mod.ident.clone(), content);
} else {
bail!("none of `{:?}` found", paths);
}
}
}
Expand Down

0 comments on commit 7275101

Please sign in to comment.