Skip to content

Commit 69874ab

Browse files
committed
Add remove_from_table manifest util
Needed for `cargo remove`.
1 parent 0b84a35 commit 69874ab

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/cargo/util/toml_mut/manifest.rs

+27
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,26 @@ impl LocalManifest {
368368
Ok(())
369369
}
370370

371+
/// Remove entry from a Cargo.toml.
372+
pub fn remove_from_table(&mut self, table_path: &[String], name: &str) -> CargoResult<()> {
373+
let parent_table = self.get_table_mut(table_path)?;
374+
375+
let dep = parent_table
376+
.get_mut(name)
377+
.filter(|t| !t.is_none())
378+
.ok_or_else(|| non_existent_dependency_err(name, table_path.join(".")))?;
379+
380+
// remove the dependency
381+
*dep = toml_edit::Item::None;
382+
383+
// remove table if empty
384+
if parent_table.as_table_like().unwrap().is_empty() {
385+
*parent_table = toml_edit::Item::None;
386+
}
387+
388+
Ok(())
389+
}
390+
371391
/// Remove references to `dep_key` if its no longer present.
372392
pub fn gc_dep(&mut self, dep_key: &str) {
373393
let explicit_dep_activation = self.is_explicit_dep_activation(dep_key);
@@ -517,3 +537,10 @@ fn parse_manifest_err() -> anyhow::Error {
517537
fn non_existent_table_err(table: impl std::fmt::Display) -> anyhow::Error {
518538
anyhow::format_err!("the table `{table}` could not be found.")
519539
}
540+
541+
fn non_existent_dependency_err(
542+
name: impl std::fmt::Display,
543+
table: impl std::fmt::Display,
544+
) -> anyhow::Error {
545+
anyhow::format_err!("the dependency `{name}` could not be found in `{table}`.")
546+
}

0 commit comments

Comments
 (0)