Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for ignore dependencies #8

Merged
merged 1 commit into from Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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