Skip to content

Commit

Permalink
feat: exit code is 0 when performing fix (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
erwanncloarec committed May 29, 2024
1 parent 36a3664 commit fcbb13f
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@ pub struct CargoShear {
options: CargoShearOptions,

unused_dependencies: usize,

fixed_dependencies: usize,
}

type Deps = HashSet<String>;

impl CargoShear {
#[must_use]
pub const fn new(options: CargoShearOptions) -> Self {
Self { options, unused_dependencies: 0 }
Self { options, unused_dependencies: 0, fixed_dependencies: 0 }
}

#[must_use]
Expand All @@ -59,7 +61,13 @@ impl CargoShear {

match self.shear() {
Ok(()) => {
let has_deps = self.unused_dependencies > 0;
let has_fixed = self.fixed_dependencies > 0;

if has_fixed {
println!("Fixed {} dependencies!", self.fixed_dependencies);
}

let has_deps = (self.unused_dependencies - self.fixed_dependencies) > 0;

if has_deps {
println!(
Expand Down Expand Up @@ -292,7 +300,11 @@ impl CargoShear {
Ok(imports)
}

fn try_fix_package(&self, cargo_toml_path: &Path, unused_dep_names: &[String]) -> Result<()> {
fn try_fix_package(
&mut self,
cargo_toml_path: &Path,
unused_dep_names: &[String],
) -> Result<()> {
if !self.options.fix {
return Ok(());
}
Expand Down Expand Up @@ -323,6 +335,7 @@ impl CargoShear {
}
}

self.fixed_dependencies += unused_dep_names.len();
let serialized = manifest.to_string();
fs::write(cargo_toml_path, serialized)?;
Ok(())
Expand Down

0 comments on commit fcbb13f

Please sign in to comment.