Skip to content

Commit

Permalink
fix(add): Stop auto-formatting content
Browse files Browse the repository at this point in the history
Huh, overlooked the cargo side when I fixed this in killercup/cargo-edit#725

Fixes rust-lang#10850
  • Loading branch information
epage committed Oct 17, 2023
1 parent 716ae62 commit 99e4847
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 30 deletions.
45 changes: 28 additions & 17 deletions src/cargo/util/toml_mut/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,17 +464,17 @@ impl Dependency {
} else if let Some(table) = item.as_table_like_mut() {
match &self.source {
Some(Source::Registry(src)) => {
table.insert("version", toml_edit::value(src.version.as_str()));
overwrite_value(table, "version", src.version.as_str());

for key in ["path", "git", "branch", "tag", "rev", "workspace"] {
table.remove(key);
}
}
Some(Source::Path(src)) => {
let relpath = path_field(crate_root, &src.path);
table.insert("path", toml_edit::value(relpath));
overwrite_value(table, "path", relpath);
if let Some(r) = src.version.as_deref() {
table.insert("version", toml_edit::value(r));
overwrite_value(table, "version", r);
} else {
table.remove("version");
}
Expand All @@ -484,24 +484,24 @@ impl Dependency {
}
}
Some(Source::Git(src)) => {
table.insert("git", toml_edit::value(src.git.as_str()));
overwrite_value(table, "git", src.git.as_str());
if let Some(branch) = src.branch.as_deref() {
table.insert("branch", toml_edit::value(branch));
overwrite_value(table, "branch", branch);
} else {
table.remove("branch");
}
if let Some(tag) = src.tag.as_deref() {
table.insert("tag", toml_edit::value(tag));
overwrite_value(table, "tag", tag);
} else {
table.remove("tag");
}
if let Some(rev) = src.rev.as_deref() {
table.insert("rev", toml_edit::value(rev));
overwrite_value(table, "rev", rev);
} else {
table.remove("rev");
}
if let Some(r) = src.version.as_deref() {
table.insert("version", toml_edit::value(r));
overwrite_value(table, "version", r);
} else {
table.remove("version");
}
Expand All @@ -511,7 +511,7 @@ impl Dependency {
}
}
Some(Source::Workspace(_)) => {
table.insert("workspace", toml_edit::value(true));
overwrite_value(table, "workspace", true);
table.set_dotted(true);
key.fmt();
for key in [
Expand All @@ -533,7 +533,7 @@ impl Dependency {
}
if table.contains_key("version") {
if let Some(r) = self.registry.as_deref() {
table.insert("registry", toml_edit::value(r));
overwrite_value(table, "registry", r);
} else {
table.remove("registry");
}
Expand All @@ -542,11 +542,11 @@ impl Dependency {
}

if self.rename.is_some() {
table.insert("package", toml_edit::value(self.name.as_str()));
overwrite_value(table, "package", self.name.as_str());
}
match self.default_features {
Some(v) => {
table.insert("default-features", toml_edit::value(v));
overwrite_value(table, "default-features", v);
}
None => {
table.remove("default-features");
Expand All @@ -564,29 +564,40 @@ impl Dependency {
})
.unwrap_or_default();
features.extend(new_features.iter().map(|s| s.as_str()));
let features = toml_edit::value(features.into_iter().collect::<toml_edit::Value>());
let features = features.into_iter().collect::<toml_edit::Value>();
table.set_dotted(false);
table.insert("features", features);
overwrite_value(table, "features", features);
} else {
table.remove("features");
}
match self.optional {
Some(v) => {
table.set_dotted(false);
table.insert("optional", toml_edit::value(v));
overwrite_value(table, "optional", v);
}
None => {
table.remove("optional");
}
}

table.fmt();
} else {
unreachable!("Invalid dependency type: {}", item.type_name());
}
}
}

fn overwrite_value(
table: &mut dyn toml_edit::TableLike,
key: &str,
value: impl Into<toml_edit::Value>,
) {
let mut value = value.into();
let existing = table.entry(key).or_insert_with(|| Default::default());
if let Some(existing_value) = existing.as_value() {
*value.decor_mut() = existing_value.decor().clone();
}
*existing = toml_edit::Item::Value(value);
}

fn invalid_type(dep: &str, key: &str, actual: &str, expected: &str) -> anyhow::Error {
anyhow::format_err!("Found {actual} for {key} when {expected} was expected for {dep}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ name = "cargo-list-test-fixture"
version = "0.0.0"

[dependencies]
cargo-list-test-fixture-dependency = { optional = true, path = "../dependency", version = "0.0.0" }
cargo-list-test-fixture-dependency = { optional = true , path = "../dependency", version = "0.0.0" }
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ name = "cargo-list-test-fixture"
version = "0.0.0"

[dependencies]
my-package1 = { version = "99999.0.0" }
my-package2 = { version = "0.4.1" }
my-package1 = { version = "99999.0.0"}
my-package2 = { version = "0.4.1"}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ default = []
other = ["your-face/nose"]

[dependencies]
your-face = { version = "99999.0.0" }
my-package2 = { version = "0.4.1" }
your-face = { version = "99999.0.0"}
my-package2 = { version = "0.4.1"}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ name = "cargo-list-test-fixture"
version = "0.0.0"

[dependencies]
your-face={ version = "99999.0.0", features = ["eyes", "nose"] } # Hello world
your-face={version="99999.0.0",features=["eyes", "nose"]} # Hello world
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ name = "cargo-list-test-fixture"
version = "0.0.0"

[dependencies]
versioned-package = { version = "0.3.0", optional = true, git = "[ROOTURL]/versioned-package" }
versioned-package = { version = "0.3.0", optional = true , git = "[ROOTURL]/versioned-package" }
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ name = "cargo-list-test-fixture"
version = "0.0.0"

[dependencies]
cargo-list-test-fixture-dependency = { version = "0.0.0", optional = true, path = "../dependency" }
cargo-list-test-fixture-dependency = { version = "0.0.0", optional = true , path = "../dependency" }
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ name = "bar"
version = "0.0.0"

[dependencies]
foo = { features = ["test"], path = "../dependency", version = "0.0.0" }
foo = { features = ["test"] , path = "../dependency", version = "0.0.0" }
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies.your-face]
version = "99999.0.0"
features = []
# Leading version
version = "99999.0.0" # Trailing version
# Leading features
features = [] # Trailing features
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
your-face = { version = "99999.0.0" }
your-face = { version = "99999.0.0"}

[features]
default = [
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/cargo_add/require_weak/out/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ version = "0.0.0"
eyes = ["your-face/eyes"]

[dependencies]
your-face = { version = "99999.0.0" }
your-face = { version = "99999.0.0"}

0 comments on commit 99e4847

Please sign in to comment.