@@ -368,6 +368,28 @@ impl LocalManifest {
368
368
Ok ( ( ) )
369
369
}
370
370
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
+
371
393
/// Remove references to `dep_key` if its no longer present.
372
394
pub fn gc_dep ( & mut self , dep_key : & str ) {
373
395
let explicit_dep_activation = self . is_explicit_dep_activation ( dep_key) ;
@@ -517,3 +539,14 @@ fn parse_manifest_err() -> anyhow::Error {
517
539
fn non_existent_table_err ( table : impl std:: fmt:: Display ) -> anyhow:: Error {
518
540
anyhow:: format_err!( "the table `{table}` could not be found." )
519
541
}
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