Skip to content

Commit

Permalink
Sort workspace dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
nipunn1313 committed Nov 6, 2022
1 parent 8a5d3fd commit 8606671
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 @@ -102,6 +108,14 @@ fn sort_by_group(table: &mut Table) {
}
}

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

/// Returns a sorted toml `Document`.
pub fn sort_toml(
input: &str,
Expand All @@ -119,8 +133,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] {
arr.sort();
match &mut table[key] {
Item::Value(Value::Array(arr)) => {
arr.sort();
}
Item::Table(table) => {
sort_table(table, group);
}
_ => unreachable!("{}.{} must be value or table", heading, key),
}
}
}
Expand Down Expand Up @@ -151,11 +171,7 @@ pub fn sort_toml(

gather_headings(table, headings, 1);
headings.sort();
if group {
sort_by_group(table);
} else {
table.sort_values();
}
sort_table(table, group);
}
Item::None => continue,
_ => unreachable!("Top level toml must be tables"),
Expand Down Expand Up @@ -258,12 +274,7 @@ fn walk_tables_set_position(table: &mut Table, idx: &mut usize) {
mod test {
use std::fs;

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 @@ -273,6 +284,14 @@ mod test {
// println!("{}", sorted.to_string_in_original_order());
}

#[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_in_original_order());
// println!("{}", sorted.to_string_in_original_order());
}

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

0 comments on commit 8606671

Please sign in to comment.