Skip to content

Commit

Permalink
Add remove_from_table manifest util
Browse files Browse the repository at this point in the history
Needed for `cargo remove`.
  • Loading branch information
cassaundra committed Sep 20, 2022
1 parent 531ce13 commit 8e7077f
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/cargo/util/toml_mut/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,26 @@ impl LocalManifest {
Ok(())
}

/// Remove entry from a Cargo.toml.
pub fn remove_from_table(&mut self, table_path: &[String], name: &str) -> CargoResult<()> {
let parent_table = self.get_table_mut(table_path)?;

let dep = parent_table
.get_mut(name)
.filter(|t| !t.is_none())
.ok_or_else(|| non_existent_dependency_err(name, table_path.join(".")))?;

// remove the dependency
*dep = toml_edit::Item::None;

// remove table if empty
if parent_table.as_table_like().unwrap().is_empty() {
*parent_table = toml_edit::Item::None;
}

Ok(())
}

/// Remove references to `dep_key` if its no longer present.
pub fn gc_dep(&mut self, dep_key: &str) {
let explicit_dep_activation = self.is_explicit_dep_activation(dep_key);
Expand Down Expand Up @@ -517,3 +537,10 @@ fn parse_manifest_err() -> anyhow::Error {
fn non_existent_table_err(table: impl std::fmt::Display) -> anyhow::Error {
anyhow::format_err!("the table `{table}` could not be found.")
}

fn non_existent_dependency_err(
name: impl std::fmt::Display,
table: impl std::fmt::Display,
) -> anyhow::Error {
anyhow::format_err!("the dependency `{name}` could not be found in `{table}`.")
}

0 comments on commit 8e7077f

Please sign in to comment.