Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort workspace dependencies #55

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Author

@nipunn1313 nipunn1313 Nov 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified that if I leave out this new line of code (sort_table), the new test fails - so the test is covering this correctly.

}
_ => 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 @@ -282,12 +298,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 @@ -297,6 +308,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