Skip to content

Commit 7b59dd0

Browse files
committed
Use toml_edit to new/init cargo project
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
1 parent f7d0c8b commit 7b59dd0

File tree

1 file changed

+32
-48
lines changed

1 file changed

+32
-48
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 32 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -759,69 +759,53 @@ 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+
array.decor_mut().set_suffix("\n\n# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html");
776+
manifest["package"]["publish"] = toml_edit::value(array);
777+
} else {
778+
manifest["package"]["edition"].as_value_mut().expect("edition is a string value").decor_mut().set_suffix("\n\n# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html");
779+
}
780+
manifest["dependencies"] = toml_edit::Item::Table(toml_edit::Table::new());
763781

764782
// Calculate what `[lib]` and `[[bin]]`s we need to append to `Cargo.toml`.
765-
766783
for i in &opts.source_files {
767784
if i.bin {
768785
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-
));
786+
let mut bin = toml_edit::Table::new();
787+
bin["name"] = toml_edit::value(i.target_name.clone());
788+
bin["path"] = toml_edit::value(i.relative_path.clone());
789+
manifest["bin"]
790+
.or_insert(toml_edit::Item::ArrayOfTables(
791+
toml_edit::ArrayOfTables::new(),
792+
))
793+
.as_array_of_tables_mut()
794+
.expect("bin is an array of tables")
795+
.push(bin);
778796
}
779797
} 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-
));
798+
manifest["lib"]["name"] = toml_edit::value(i.target_name.clone());
799+
manifest["lib"]["path"] = toml_edit::value(i.relative_path.clone());
789800
}
790801
}
791802

792-
// Create `Cargo.toml` file with necessary `[lib]` and `[[bin]]` sections, if needed.
793-
794803
paths::write(
795804
&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(),
805+
format!("{}", manifest.to_string()),
821806
)?;
822807

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

0 commit comments

Comments
 (0)