Skip to content

Commit 4110521

Browse files
committed
Add remove_from_table manifest util
Needed for `cargo remove`.
1 parent aaadab6 commit 4110521

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/cargo/util/toml_mut/manifest.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,28 @@ 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+
{
376+
let dep = parent_table
377+
.get_mut(name)
378+
.filter(|t| !t.is_none())
379+
.ok_or_else(|| non_existent_dependency_err(name, table_path.join(".")))?;
380+
381+
// remove the dependency
382+
*dep = toml_edit::Item::None;
383+
}
384+
385+
// remove table if empty
386+
if parent_table.as_table_like().unwrap().is_empty() {
387+
*parent_table = toml_edit::Item::None;
388+
}
389+
390+
Ok(())
391+
}
392+
371393
/// Remove references to `dep_key` if its no longer present.
372394
pub fn gc_dep(&mut self, dep_key: &str) {
373395
let explicit_dep_activation = self.is_explicit_dep_activation(dep_key);
@@ -517,3 +539,14 @@ fn parse_manifest_err() -> anyhow::Error {
517539
fn non_existent_table_err(table: impl std::fmt::Display) -> anyhow::Error {
518540
anyhow::format_err!("the table `{table}` could not be found.")
519541
}
542+
543+
fn non_existent_dependency_err(
544+
name: impl std::fmt::Display,
545+
table: impl std::fmt::Display,
546+
) -> anyhow::Error {
547+
anyhow::format_err!(
548+
"The dependency `{}` could not be found in `{}`.",
549+
name,
550+
table,
551+
)
552+
}

0 commit comments

Comments
 (0)