@@ -759,69 +759,54 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
759
759
init_vcs ( path, vcs, config) ?;
760
760
write_ignore_file ( path, & ignore, vcs) ?;
761
761
762
- let mut cargotoml_path_specifier = String :: new ( ) ;
762
+ // Create `Cargo.toml` file with necessary `[lib]` and `[[bin]]` sections, if needed.
763
+ let mut manifest = toml_edit:: Document :: new ( ) ;
764
+ manifest[ "package" ] = toml_edit:: Item :: Table ( toml_edit:: Table :: new ( ) ) ;
765
+ manifest[ "package" ] [ "name" ] = toml_edit:: value ( name) ;
766
+ manifest[ "package" ] [ "version" ] = toml_edit:: value ( "0.1.0" ) ;
767
+ let edition = match opts. edition {
768
+ Some ( edition) => edition. to_string ( ) ,
769
+ None => Edition :: LATEST_STABLE . to_string ( ) ,
770
+ } ;
771
+ manifest[ "package" ] [ "edition" ] = toml_edit:: value ( edition) ;
772
+ if let Some ( registry) = opts. registry {
773
+ let mut array = toml_edit:: Array :: default ( ) ;
774
+ array. push ( registry) ;
775
+ manifest[ "package" ] [ "publish" ] = toml_edit:: value ( array) ;
776
+ }
777
+ let mut dep_table = toml_edit:: Table :: default ( ) ;
778
+ dep_table. decor_mut ( ) . set_prefix ( "\n # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html\n \n " ) ;
779
+ manifest[ "dependencies" ] = toml_edit:: Item :: Table ( dep_table) ;
763
780
764
781
// Calculate what `[lib]` and `[[bin]]`s we need to append to `Cargo.toml`.
765
-
766
782
for i in & opts. source_files {
767
783
if i. bin {
768
784
if i. relative_path != "src/main.rs" {
769
- cargotoml_path_specifier. push_str ( & format ! (
770
- r#"
771
- [[bin]]
772
- name = "{}"
773
- path = {}
774
- "# ,
775
- i. target_name,
776
- toml:: Value :: String ( i. relative_path. clone( ) )
777
- ) ) ;
785
+ let mut bin = toml_edit:: Table :: new ( ) ;
786
+ bin[ "name" ] = toml_edit:: value ( i. target_name . clone ( ) ) ;
787
+ bin[ "path" ] = toml_edit:: value ( i. relative_path . clone ( ) ) ;
788
+ manifest[ "bin" ]
789
+ . or_insert ( toml_edit:: Item :: ArrayOfTables (
790
+ toml_edit:: ArrayOfTables :: new ( ) ,
791
+ ) )
792
+ . as_array_of_tables_mut ( )
793
+ . expect ( "bin is an array of tables" )
794
+ . push ( bin) ;
778
795
}
779
796
} else if i. relative_path != "src/lib.rs" {
780
- cargotoml_path_specifier. push_str ( & format ! (
781
- r#"
782
- [lib]
783
- name = "{}"
784
- path = {}
785
- "# ,
786
- i. target_name,
787
- toml:: Value :: String ( i. relative_path. clone( ) )
788
- ) ) ;
797
+ let mut lib = toml_edit:: Table :: new ( ) ;
798
+ lib[ "name" ] = toml_edit:: value ( i. target_name . clone ( ) ) ;
799
+ lib[ "path" ] = toml_edit:: value ( i. relative_path . clone ( ) ) ;
800
+ manifest[ "lib" ] = toml_edit:: Item :: Table ( lib) ;
789
801
}
790
802
}
791
803
792
- // Create `Cargo.toml` file with necessary `[lib]` and `[[bin]]` sections, if needed.
793
-
794
804
paths:: write (
795
805
& path. join ( "Cargo.toml" ) ,
796
- format ! (
797
- r#"[package]
798
- name = "{}"
799
- version = "0.1.0"
800
- edition = {}
801
- {}
802
- # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
803
-
804
- [dependencies]
805
- {}"# ,
806
- name,
807
- match opts. edition {
808
- Some ( edition) => toml:: Value :: String ( edition. to_string( ) ) ,
809
- None => toml:: Value :: String ( Edition :: LATEST_STABLE . to_string( ) ) ,
810
- } ,
811
- match opts. registry {
812
- Some ( registry) => format!(
813
- "publish = {}\n " ,
814
- toml:: Value :: Array ( vec!( toml:: Value :: String ( registry. to_string( ) ) ) )
815
- ) ,
816
- None => "" . to_string( ) ,
817
- } ,
818
- cargotoml_path_specifier
819
- )
820
- . as_bytes ( ) ,
806
+ format ! ( "{}" , manifest. to_string( ) ) ,
821
807
) ?;
822
808
823
809
// Create all specified source files (with respective parent directories) if they don't exist.
824
-
825
810
for i in & opts. source_files {
826
811
let path_of_source_file = path. join ( i. relative_path . clone ( ) ) ;
827
812
0 commit comments