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