cargo/ops/
cargo_remove.rsuse crate::core::Package;
use crate::util::toml_mut::manifest::DepTable;
use crate::util::toml_mut::manifest::LocalManifest;
use crate::CargoResult;
use crate::GlobalContext;
#[derive(Debug)]
pub struct RemoveOptions<'a> {
pub gctx: &'a GlobalContext,
pub spec: &'a Package,
pub dependencies: Vec<String>,
pub section: DepTable,
pub dry_run: bool,
}
pub fn remove(options: &RemoveOptions<'_>) -> CargoResult<()> {
let dep_table = options
.section
.to_table()
.into_iter()
.map(String::from)
.collect::<Vec<_>>();
let manifest_path = options.spec.manifest_path().to_path_buf();
let mut manifest = LocalManifest::try_new(&manifest_path)?;
for dep in &options.dependencies {
let section = if dep_table.len() >= 3 {
format!("{} for target `{}`", &dep_table[2], &dep_table[1])
} else {
dep_table[0].clone()
};
options
.gctx
.shell()
.status("Removing", format!("{dep} from {section}"))?;
manifest.remove_from_table(&dep_table, dep)?;
manifest.gc_dep(dep);
}
if options.dry_run {
options
.gctx
.shell()
.warn("aborting remove due to dry run")?;
} else {
manifest.write()?;
}
Ok(())
}