Skip to content

Commit

Permalink
Sort workspace dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
nipunn1313 authored and Calsign committed Dec 20, 2024
1 parent 64fb9e2 commit 9a387be
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
5 changes: 5 additions & 0 deletions examp/workspace_deps.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]

[workspace.dependencies]
b = "1"
a = "1"
47 changes: 33 additions & 14 deletions src/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ pub struct Matcher<'a> {

pub const MATCHER: Matcher<'_> = Matcher {
heading: &["dependencies", "dev-dependencies", "build-dependencies"],
heading_key: &[("workspace", "members"), ("workspace", "exclude")],
heading_key: &[
("workspace", "members"),
("workspace", "exclude"),
("workspace", "dependencies"),
("workspace", "dev-dependencies"),
("workspace", "build-dependencies"),
],
};

/// A state machine to track collection of headings.
Expand Down Expand Up @@ -45,8 +51,14 @@ pub fn sort_toml(
if toml.as_table().contains_key(heading) {
if let Item::Table(table) = &mut toml[heading] {
if table.contains_key(key) {
if let Item::Value(Value::Array(arr)) = &mut table[key] {
sort_array(arr);
match &mut table[key] {
Item::Value(Value::Array(arr)) => {
sort_array(arr);
}
Item::Table(table) => {
sort_table(table, group);
}
_ => unreachable!("{}.{} must be value or table", heading, key),
}
}
}
Expand Down Expand Up @@ -77,11 +89,7 @@ pub fn sort_toml(

gather_headings(table, headings, 1);
headings.sort();
if group {
sort_by_group(table);
} else {
table.sort_values();
}
sort_by_group(table);
}
Item::None => continue,
_ => unreachable!("Top level toml must be tables"),
Expand Down Expand Up @@ -112,6 +120,14 @@ fn sort_array(arr: &mut Array) {
}
}

fn sort_table(table: &mut Table, group: bool) {
if group {
sort_by_group(table);
} else {
table.sort_values();
}
}

fn gather_headings(table: &Table, keys: &mut Vec<Heading>, depth: usize) {
if table.is_empty() && !table.is_implicit() {
let next = match keys.pop().unwrap() {
Expand Down Expand Up @@ -306,12 +322,7 @@ mod test {

use similar_asserts::assert_eq;

use super::Matcher;

const MATCHER: Matcher<'_> = Matcher {
heading: &["dependencies", "dev-dependencies", "build-dependencies"],
heading_key: &[("workspace", "members"), ("workspace", "exclude")],
};
use super::MATCHER;

#[test]
fn toml_edit_check() {
Expand All @@ -321,6 +332,14 @@ mod test {
// println!("{}", sorted.to_string());
}

#[test]
fn toml_workspace_deps_edit_check() {
let input = fs::read_to_string("examp/workspace_deps.toml").unwrap();
let sorted = super::sort_toml(&input, MATCHER, false, &[]);
assert_ne!(input, sorted.to_string());
// println!("{}", sorted.to_string());
}

#[test]
fn grouped_check() {
let input = fs::read_to_string("examp/ruma.toml").unwrap();
Expand Down

0 comments on commit 9a387be

Please sign in to comment.