Skip to content

Commit f69b46a

Browse files
committed
Use toml_edit to new/init cargo project
Signed-off-by: hi-rustin <[email protected]>
1 parent e5e68c4 commit f69b46a

File tree

1 file changed

+33
-48
lines changed

1 file changed

+33
-48
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -759,69 +759,54 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
759759
init_vcs(path, vcs, config)?;
760760
write_ignore_file(path, &ignore, vcs)?;
761761

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);
763780

764781
// Calculate what `[lib]` and `[[bin]]`s we need to append to `Cargo.toml`.
765-
766782
for i in &opts.source_files {
767783
if i.bin {
768784
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);
778795
}
779796
} 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);
789801
}
790802
}
791803

792-
// Create `Cargo.toml` file with necessary `[lib]` and `[[bin]]` sections, if needed.
793-
794804
paths::write(
795805
&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()),
821807
)?;
822808

823809
// Create all specified source files (with respective parent directories) if they don't exist.
824-
825810
for i in &opts.source_files {
826811
let path_of_source_file = path.join(i.relative_path.clone());
827812

0 commit comments

Comments
 (0)