From 72751012b140bbafeccdb784314f770d82f42185 Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Wed, 2 Sep 2020 18:23:23 +0900 Subject: [PATCH] Include module lists in the output --- CHANGELOG.md | 4 +++ Cargo.lock | 59 +++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/lib.rs | 86 +++++++++++++++++++++++++++++++++++++++++----------- src/rust.rs | 59 ++++++++++++++++++----------------- 5 files changed, 163 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49d825c..2c3badd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Changed + +- Now it includes the used module list in the output. + ### Fixed - `--check` option will work with `{ path = ".." }` dependencies. diff --git a/Cargo.lock b/Cargo.lock index 90666bc..7433c16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -64,6 +64,7 @@ dependencies = [ "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "termcolor 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "toml_edit 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -161,6 +162,16 @@ dependencies = [ "libc 0.2.76 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "idna" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-normalization 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "itertools" version = "0.9.0" @@ -194,6 +205,11 @@ name = "maplit" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "matches" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "memchr" version = "2.3.3" @@ -216,6 +232,11 @@ dependencies = [ "autocfg 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "percent-encoding" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "ppv-lite86" version = "0.2.9" @@ -437,6 +458,11 @@ dependencies = [ "winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tinyvec" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "toml_edit" version = "0.2.0" @@ -447,6 +473,22 @@ dependencies = [ "linked-hash-map 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "unicode-bidi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-normalization" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "tinyvec 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "unicode-segmentation" version = "1.6.0" @@ -470,6 +512,16 @@ dependencies = [ "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "url" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "vec_map" version = "0.8.2" @@ -540,15 +592,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" "checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" "checksum hermit-abi 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9" +"checksum idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" "checksum itertools 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b" "checksum itoa 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum libc 0.2.76 (registry+https://github.com/rust-lang/crates.io-index)" = "755456fae044e6fa1ebbbd1b3e902ae19e73097ed4ed87bb79934a867c007bc3" "checksum linked-hash-map 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a" "checksum maplit 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" +"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" "checksum memchr 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" "checksum num-integer 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b" "checksum num-traits 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)" = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611" +"checksum percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" "checksum ppv-lite86 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c36fa947111f5c62a733b652544dd0016a43ce89619538a8ef92724a6f501a20" "checksum proc-macro-error 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" "checksum proc-macro-error-attr 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" @@ -575,11 +630,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum termcolor 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" "checksum time 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)" = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +"checksum tinyvec 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "238ce071d267c5710f9d31451efec16c5ee22de34df17cc05e56cbc92e967117" "checksum toml_edit 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "09391a441b373597cf0888d2b052dcf82c5be4fee05da3636ae30fb57aad8484" +"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" +"checksum unicode-normalization 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6fb19cf769fa8c6a80a162df694621ebeb4dafb606470b2b2fce0be40a98a977" "checksum unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" "checksum unicode-width 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" "checksum unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" +"checksum url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb" "checksum vec_map 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" "checksum version_check 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" diff --git a/Cargo.toml b/Cargo.toml index e5b69ab..e285174 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/lib.rs b/src/lib.rs index a4dca1f..2e06eca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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( @@ -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, @@ -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::() + .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]; @@ -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::() - .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::() + .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"; } } diff --git a/src/rust.rs b/src/rust.rs index a1686ca..67b7468 100644 --- a/src/rust.rs +++ b/src/rust.rs @@ -171,41 +171,44 @@ pub(crate) fn parse_exactly_one_use(file: &syn::File) -> syn::Result>, -) -> anyhow::Result> { +) -> anyhow::Result>> { 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); } } }