Skip to content

Commit 3a9da71

Browse files
committed
Use toml_edit
Signed-off-by: hi-rustin <[email protected]>
1 parent 38babd5 commit 3a9da71

File tree

7 files changed

+78
-57
lines changed
  • src/cargo
  • tests/testsuite
    • cargo_new
      • inherit_workspace_package_table/out/crates/foo
      • inherit_workspace_package_table_with_edition/out/crates/foo
      • inherit_workspace_package_table_with_registry/out/crates/foo
      • inherit_workspace_package_table_without_version/out/crates/foo
    • init/inherit_workspace_package_table/out/crates/foo

7 files changed

+78
-57
lines changed

src/cargo/ops/cargo_new.rs

+37-51
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::core::{Edition, Shell, Workspace};
22
use crate::util::errors::CargoResult;
33
use crate::util::toml::parse_document;
4+
use crate::util::toml_mut::manifest::LocalManifest;
45
use crate::util::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo};
56
use crate::util::{restricted_names, Config};
67
use anyhow::{anyhow, Context as _};
@@ -879,16 +880,9 @@ mod tests {
879880
.and_then(|workspace| workspace.get("package"))
880881
.and_then(|package| package.as_table())
881882
{
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.
887883
return create_manifest_with_inherited_workspace_package_fields(
888884
opts,
889885
path,
890-
name,
891-
cargotoml_path_specifier.as_str(),
892886
workspace_package_keys,
893887
);
894888
}
@@ -909,75 +903,67 @@ mod tests {
909903
fn create_manifest_with_inherited_workspace_package_fields(
910904
opts: &MkOptions<'_>,
911905
path: &Path,
912-
name: &str,
913-
cargotoml_path_specifier: &str,
914906
workspace_package_keys: &toml::value::Table,
915907
) -> CargoResult<()> {
916908
if workspace_package_keys.is_empty() {
917909
return Ok(());
918910
}
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+
919922
// Try inherit the edition from the workspace if it is not specified.
920-
let edition = match opts.edition {
923+
match opts.edition {
921924
Some(edition) => {
922-
format!("edition = {}", toml::Value::String(edition.to_string()))
925+
manifest.set_package_key("edition", toml_edit::value(edition))?;
923926
}
924927
None => {
925928
if workspace_package_keys.contains_key("edition") {
926-
format!("edition.workspace = {}", toml::Value::Boolean(true))
929+
remove_and_inherit_package_key("edition", &mut manifest)?;
927930
} 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+
)?;
932935
}
933936
}
934937
};
935938
// 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)?;
938941
} else {
939-
"version = \"0.1.0\"".to_string()
942+
manifest.set_package_key("version", toml_edit::value("0.1.0"))?;
940943
};
941944
// 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+
}
947951
None => {
948952
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)?;
952954
}
953955
}
954956
};
955957
// Inherit other keys from the workspace.
956-
let workspace_package_fields = workspace_package_keys
958+
for (key, _) in workspace_package_keys
957959
.iter()
958960
.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+
}
970964

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(())
983969
}

src/cargo/util/toml_mut/manifest.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ops::{Deref, DerefMut};
44
use std::path::{Path, PathBuf};
55
use std::str;
66

7-
use anyhow::Context as _;
7+
use anyhow::{Context as _, Ok};
88

99
use super::dependency::Dependency;
1010
use crate::core::dependency::DepKind;
@@ -445,6 +445,36 @@ impl LocalManifest {
445445
}
446446
status
447447
}
448+
449+
pub fn set_package_key(&mut self, key: &str, value: toml_edit::Item) -> CargoResult<()> {
450+
let package = self.data["package"]
451+
.as_table_mut()
452+
.ok_or_else(parse_manifest_err)?;
453+
package[key] = value;
454+
455+
Ok(())
456+
}
457+
458+
pub fn remove_package_key(&mut self, key: &str) -> CargoResult<()> {
459+
let package = self.data["package"]
460+
.as_table_mut()
461+
.ok_or_else(parse_manifest_err)?;
462+
package.remove(key);
463+
464+
Ok(())
465+
}
466+
467+
pub fn set_workspace_inherited_package_key(&mut self, key: &str) -> CargoResult<()> {
468+
let package = self.data["package"]
469+
.as_table_mut()
470+
.ok_or_else(parse_manifest_err)?;
471+
let mut table = toml_edit::Table::new();
472+
table.set_dotted(true);
473+
table["workspace"] = toml_edit::value(true);
474+
package.insert(key, toml_edit::Item::Table(table));
475+
476+
Ok(())
477+
}
448478
}
449479

450480
impl std::fmt::Display for LocalManifest {

tests/testsuite/cargo_new/inherit_workspace_package_table/out/crates/foo/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "foo"
3-
version.workspace = true
43
edition.workspace = true
4+
version.workspace = true
55
publish.workspace = true
66
authors.workspace = true
77
categories.workspace = true
@@ -15,6 +15,7 @@ license.workspace = true
1515
readme.workspace = true
1616
repository.workspace = true
1717
rust-version.workspace = true
18+
1819
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1920

2021
[dependencies]

tests/testsuite/cargo_new/inherit_workspace_package_table_with_edition/out/crates/foo/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "foo"
3-
version.workspace = true
43
edition = "2021"
4+
version.workspace = true
55
publish.workspace = true
66
authors.workspace = true
77
categories.workspace = true
@@ -15,6 +15,7 @@ license.workspace = true
1515
readme.workspace = true
1616
repository.workspace = true
1717
rust-version.workspace = true
18+
1819
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1920

2021
[dependencies]

tests/testsuite/cargo_new/inherit_workspace_package_table_with_registry/out/crates/foo/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[package]
22
name = "foo"
3-
version.workspace = true
4-
edition.workspace = true
53
publish = ["foo"]
4+
edition.workspace = true
5+
version.workspace = true
66
authors.workspace = true
77
categories.workspace = true
88
description.workspace = true
@@ -15,6 +15,7 @@ license.workspace = true
1515
readme.workspace = true
1616
repository.workspace = true
1717
rust-version.workspace = true
18+
1819
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1920

2021
[dependencies]

tests/testsuite/cargo_new/inherit_workspace_package_table_without_version/out/crates/foo/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ license.workspace = true
1515
readme.workspace = true
1616
repository.workspace = true
1717
rust-version.workspace = true
18+
1819
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1920

2021
[dependencies]

tests/testsuite/init/inherit_workspace_package_table/out/crates/foo/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "foo"
3-
version.workspace = true
43
edition.workspace = true
4+
version.workspace = true
55
publish.workspace = true
66
authors.workspace = true
77
categories.workspace = true
@@ -15,6 +15,7 @@ license.workspace = true
1515
readme.workspace = true
1616
repository.workspace = true
1717
rust-version.workspace = true
18+
1819
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1920

2021
[dependencies]

0 commit comments

Comments
 (0)