Skip to content

Commit

Permalink
Use Array::sort_by to sort the members list.
Browse files Browse the repository at this point in the history
Signed-off-by: David Calavera <[email protected]>
  • Loading branch information
calavera committed Oct 27, 2023
1 parent ebc1eab commit 41aa44b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ tempfile = "3.8.0"
thiserror = "1.0.49"
time = { version = "0.3", features = ["parsing", "formatting", "serde"] }
toml = "0.8.2"
toml_edit = { version = "0.20.2", features = ["serde"] }
toml_edit = { version = "0.20.7", features = ["serde"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
unicase = "2.7.0"
Expand Down
17 changes: 7 additions & 10 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,11 +996,11 @@ fn update_manifest_with_new_member(
// - Add the relative path if the members don't match the new package's path.
// - Create a new members array if there are no members element in the workspace yet.
if let Some(members) = workspace_document
.get("workspace")
.and_then(|workspace| workspace.get("members"))
.and_then(|members| members.as_array())
.get_mut("workspace")
.and_then(|workspace| workspace.get_mut("members"))
.and_then(|members| members.as_array_mut())
{
for member in members {
for member in members.iter() {
let pat = member
.as_str()
.with_context(|| format!("invalid non-string member `{}`", member))?;
Expand All @@ -1012,14 +1012,11 @@ fn update_manifest_with_new_member(
}
}

let was_sorted = is_sorted(members.iter().filter_map(Value::as_str));
let mut values: Vec<&str> = members.iter().filter_map(Value::as_str).collect();
values.push(&display_path);
let was_sorted = is_sorted(members.iter().map(Value::as_str));
members.push(&display_path);
if was_sorted {
values.sort();
members.sort_by(|lhs, rhs| lhs.as_str().cmp(&rhs.as_str()));
}
let array = Array::from_iter(values);
workspace_document["workspace"]["members"] = toml_edit::value(array);
} else {
let mut array = Array::new();
array.push(&display_path);
Expand Down

0 comments on commit 41aa44b

Please sign in to comment.