Skip to content

Commit

Permalink
Merge pull request #8 from Dunqing/feat/ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Mar 25, 2024
2 parents 236d7e1 + e98f3f9 commit 9bd2e77
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -31,7 +31,8 @@ The exit code gives an indication whether unused dependencies have been found:
- [ ] make the reporting more granular for `[dependencies]`, `[dev-dependencies]` and `[build-dependencies]`
- [ ] add tests
- [ ] print things nicely
- [ ] ignore `[package.metadata.cargo-shear] ignored = ["crate"]`
- [x] ignore `[package.metadata.cargo-shear] ignored = ["crate"]`


## Prior Arts

Expand Down
18 changes: 17 additions & 1 deletion src/lib.rs
Expand Up @@ -118,7 +118,23 @@ impl CargoShear {
})
.collect::<HashMap<String, String>>();

let mod_names = package_deps_map.keys().cloned().collect::<HashSet<_>>();
let ignored_names = package
.metadata
.as_object()
.and_then(|object| object.get("cargo-shear").and_then(|object| object.get("ignored")))
.and_then(|ignored| {
ignored.as_array().map(|ignored| {
ignored.iter().filter_map(|item| item.as_str()).collect::<HashSet<_>>()
})
})
.unwrap_or_default();

let mod_names = package_deps_map
.keys()
.filter(|name| !ignored_names.contains(name.as_str()))
.cloned()
.collect::<HashSet<_>>();

let rust_file_deps = Self::get_package_dependencies_from_rust_files(package)?;
let unused_deps = mod_names.difference(&rust_file_deps).collect::<Vec<_>>();
if unused_deps.is_empty() {
Expand Down

0 comments on commit 9bd2e77

Please sign in to comment.