1
1
use crate :: core:: { Edition , Shell , Workspace } ;
2
2
use crate :: util:: errors:: CargoResult ;
3
3
use crate :: util:: toml:: parse_document;
4
+ use crate :: util:: toml_mut:: manifest:: LocalManifest ;
4
5
use crate :: util:: { existing_vcs_repo, FossilRepo , GitRepo , HgRepo , PijulRepo } ;
5
6
use crate :: util:: { restricted_names, Config } ;
6
7
use anyhow:: { anyhow, Context as _} ;
@@ -879,16 +880,9 @@ mod tests {
879
880
. and_then ( |workspace| workspace. get ( "package" ) )
880
881
. and_then ( |package| package. as_table ( ) )
881
882
{
882
- // We create a new manifest with the inherited package field.
883
- // We do not use the existing manifest because we do not want
884
- // to couple those two write operations together.
885
- // Moreover, if we want to add the package as a member of the workspace automatically,
886
- // we can directly reuse this function.
887
883
return create_manifest_with_inherited_workspace_package_fields (
888
884
opts,
889
885
path,
890
- name,
891
- cargotoml_path_specifier. as_str ( ) ,
892
886
workspace_package_keys,
893
887
) ;
894
888
}
@@ -909,75 +903,67 @@ mod tests {
909
903
fn create_manifest_with_inherited_workspace_package_fields (
910
904
opts : & MkOptions < ' _ > ,
911
905
path : & Path ,
912
- name : & str ,
913
- cargotoml_path_specifier : & str ,
914
906
workspace_package_keys : & toml:: value:: Table ,
915
907
) -> CargoResult < ( ) > {
916
908
if workspace_package_keys. is_empty ( ) {
917
909
return Ok ( ( ) ) ;
918
910
}
911
+
912
+ let manifest_path = path. join ( "Cargo.toml" ) ;
913
+ let mut manifest = LocalManifest :: try_new ( & manifest_path) ?;
914
+
915
+ let remove_and_inherit_package_key =
916
+ |key : & str , manifest : & mut LocalManifest | -> CargoResult < ( ) > {
917
+ manifest. remove_package_key ( key) ?;
918
+ manifest. set_workspace_inherited_package_key ( key) ?;
919
+ Ok ( ( ) )
920
+ } ;
921
+
919
922
// Try inherit the edition from the workspace if it is not specified.
920
- let edition = match opts. edition {
923
+ match opts. edition {
921
924
Some ( edition) => {
922
- format ! ( "edition = {} " , toml :: Value :: String ( edition. to_string ( ) ) )
925
+ manifest . set_package_key ( "edition" , toml_edit :: value ( edition) ) ? ;
923
926
}
924
927
None => {
925
928
if workspace_package_keys. contains_key ( "edition" ) {
926
- format ! ( "edition.workspace = {} " , toml :: Value :: Boolean ( true ) )
929
+ remove_and_inherit_package_key ( "edition" , & mut manifest ) ? ;
927
930
} else {
928
- format ! (
929
- "edition = {} " ,
930
- toml :: Value :: String ( Edition :: LATEST_STABLE . to_string( ) )
931
- )
931
+ manifest . set_package_key (
932
+ "edition" ,
933
+ toml_edit :: value ( Edition :: LATEST_STABLE . to_string ( ) ) ,
934
+ ) ? ;
932
935
}
933
936
}
934
937
} ;
935
938
// Try inherit the version from the workspace if it is not specified.
936
- let version = if workspace_package_keys. contains_key ( "version" ) {
937
- format ! ( "version.workspace = {} " , toml :: Value :: Boolean ( true ) )
939
+ if workspace_package_keys. contains_key ( "version" ) {
940
+ remove_and_inherit_package_key ( "version" , & mut manifest ) ? ;
938
941
} else {
939
- "version = \ " 0.1.0\" " . to_string ( )
942
+ manifest . set_package_key ( "version" , toml_edit :: value ( "0.1.0" ) ) ? ;
940
943
} ;
941
944
// Try inherit the publish from the workspace if it is not specified.
942
- let publish = match opts. registry {
943
- Some ( registry) => format ! (
944
- "publish = {}\n " ,
945
- toml:: Value :: Array ( vec!( toml:: Value :: String ( registry. to_string( ) ) ) )
946
- ) ,
945
+ match opts. registry {
946
+ Some ( registry) => {
947
+ let mut array = toml_edit:: Array :: default ( ) ;
948
+ array. push ( registry) ;
949
+ manifest. set_package_key ( "publish" , toml_edit:: value ( array) ) ?;
950
+ }
947
951
None => {
948
952
if workspace_package_keys. contains_key ( "publish" ) {
949
- format ! ( "publish.workspace = {}\n " , toml:: Value :: Boolean ( true ) )
950
- } else {
951
- "" . to_string ( )
953
+ remove_and_inherit_package_key ( "publish" , & mut manifest) ?;
952
954
}
953
955
}
954
956
} ;
955
957
// Inherit other keys from the workspace.
956
- let workspace_package_fields = workspace_package_keys
958
+ for ( key , _ ) in workspace_package_keys
957
959
. iter ( )
958
960
. filter ( |( key, _) | * key != "edition" && * key != "version" && * key != "publish" )
959
- . map ( |( key, _) | format ! ( "{}.workspace = {}" , key, toml:: Value :: Boolean ( true ) ) )
960
- . collect :: < Vec < String > > ( ) ;
961
- paths:: write (
962
- & path. join ( "Cargo.toml" ) ,
963
- format ! (
964
- r#"[package]
965
- name = "{}"
966
- {}
967
- {}
968
- {}{}
969
- # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
961
+ {
962
+ manifest. set_workspace_inherited_package_key ( key) ?;
963
+ }
970
964
971
- [dependencies]
972
- {}"# ,
973
- name,
974
- version,
975
- edition,
976
- publish,
977
- workspace_package_fields. join( "\n " ) ,
978
- cargotoml_path_specifier
979
- )
980
- . as_bytes ( ) ,
981
- ) ?;
982
- return Ok ( ( ) ) ;
965
+ // Re-write the `Cargo.toml` file with the inherited fields.
966
+ manifest. write ( ) ?;
967
+
968
+ Ok ( ( ) )
983
969
}
0 commit comments