Skip to content

Commit

Permalink
Upgrade toml_edit to 0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
DevinR528 authored and jplatte committed Dec 7, 2023
1 parent 8a5d3fd commit cb8fe43
Show file tree
Hide file tree
Showing 37 changed files with 223 additions and 4,496 deletions.
65 changes: 64 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ default-run = "cargo-sort"
clap = { version = "4.0.10", features = ["wrap_help", "cargo"] }
glob = "0.3"
termcolor = "1.1"
toml_edit = "0.9"

# toml_edit's dependencies
chrono = "0.4"
Expand Down
78 changes: 41 additions & 37 deletions src/fmt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::str::FromStr;

use crate::toml_edit::{Document, Item, Table, Value};
use toml_edit::{Document, Item, Table, Value};

/// The config file for formatting toml after sorting.
///
Expand Down Expand Up @@ -129,17 +129,19 @@ impl FromStr for Config {
fn fmt_value(value: &mut Value, config: &Config) {
match value {
Value::Array(arr) => {
arr.trailing_comma |= config.always_trailing_comma;
arr.fmt(config.compact_arrays, config.multiline_trailing_comma);
// TODO if multi line trailing comma and compact array
arr.set_trailing_comma(config.always_trailing_comma);
arr.fmt();
}
Value::InlineTable(table) => {
table.fmt(config.compact_inline_tables);
table.fmt();
}
// Since the above variants have fmt methods we can only ever
// get here from a headed table (`[header] key = val`)
val => {
if config.space_around_eq && val.decor().prefix().is_empty() {
val.decor_mut().prefix.push(' ');
if config.space_around_eq && val.decor().prefix().map_or(false, str::is_empty)
{
val.decor_mut().set_prefix(" ");
}
}
}
Expand All @@ -151,45 +153,50 @@ fn fmt_table(table: &mut Table, config: &Config) {
#[cfg(not(target_os = "windows"))]
const NEWLINE_PATTERN: &str = "\n";
// Checks the header decor for blank lines
let blank_header_lines =
table.header_decor().prefix().lines().filter(|l| !l.starts_with('#')).count();
let blank_header_lines = table
.decor()
.prefix()
.unwrap_or("")
.lines()
.filter(|l| !l.starts_with('#'))
.count();
if config.allowed_blank_lines < blank_header_lines {
let dec = table.header_decor_mut();
dec.prefix = dec.prefix().replacen(
let dec = table.decor_mut();
dec.set_prefix(dec.prefix().unwrap_or("").replacen(
NEWLINE_PATTERN,
"",
blank_header_lines - config.allowed_blank_lines,
);
));
}

for (_, item) in table.iter_mut() {
let keys: Vec<_> = table.iter().map(|(k, _)| k.to_owned()).collect();
for key in keys {
let dec = table.key_decor_mut(&key).unwrap();
let blank_lines =
item.decor().prefix().lines().filter(|l| !l.starts_with('#')).count();
dec.prefix().unwrap_or("").lines().filter(|l| !l.starts_with('#')).count();

// Check each item in the table for blank lines
if config.key_value_newlines {
if config.allowed_blank_lines < blank_lines {
let dec = item.decor_mut();
dec.prefix = dec.prefix().replacen(
dec.set_prefix(dec.prefix().unwrap_or("").replacen(
NEWLINE_PATTERN,
"",
blank_lines - config.allowed_blank_lines,
);
));
}
} else {
let dec = item.decor_mut();
dec.prefix = if dec.prefix.contains('#') {
dec.prefix().replacen(NEWLINE_PATTERN, "", blank_lines)
dec.set_prefix(if dec.prefix().is_some_and(|pre| pre.contains('#')) {
dec.prefix().unwrap_or("").replacen(NEWLINE_PATTERN, "", blank_lines)
} else {
"".to_string()
};
});
}

if config.space_around_eq && item.decor().suffix.is_empty() {
item.decor_mut().suffix.push(' ');
if config.space_around_eq && dec.suffix().is_some_and(str::is_empty) {
dec.set_suffix(format!("{}{}", dec.suffix().unwrap_or(""), ' '));
}

match item.value_mut() {
match table.get_mut(&key).unwrap() {
Item::Table(table) => {
// stuff
fmt_table(table, config);
Expand All @@ -206,7 +213,7 @@ fn fmt_table(table: &mut Table, config: &Config) {
/// Formats a toml `Document` according to `tomlfmt.toml`.
pub fn fmt_toml(toml: &mut Document, config: &Config) {
for (_key, item) in toml.as_table_mut().iter_mut() {
match item.value_mut() {
match item {
Item::ArrayOfTables(table) => {
for tab in table.iter_mut() {
fmt_table(tab, config);
Expand All @@ -225,8 +232,8 @@ pub fn fmt_toml(toml: &mut Document, config: &Config) {
// TODO:
// This is TERRIBLE!! Convert the Document to a string only to check it ends with a
// newline
if config.trailing_newline && !toml.to_string_in_original_order().ends_with('\n') {
toml.trailing.push('\n');
if config.trailing_newline && !toml.to_string().ends_with('\n') {
toml.decor_mut().set_suffix("\n");
}
}

Expand All @@ -241,8 +248,8 @@ mod test {
let input = fs::read_to_string("examp/ruma.toml").unwrap();
let mut toml = input.parse::<Document>().unwrap();
fmt_toml(&mut toml, &Config::new());
assert_ne!(input, toml.to_string_in_original_order());
// println!("{}", toml.to_string_in_original_order());
assert_ne!(input, toml.to_string());
// println!("{}", toml.to_string());
}

#[test]
Expand All @@ -251,12 +258,9 @@ mod test {
let mut toml = input.parse::<Document>().unwrap();
fmt_toml(&mut toml, &Config::new());
#[cfg(target_os = "windows")]
assert_eq!(
input.replace("\r\n", "\n"),
toml.to_string_in_original_order().replace("\r\n", "\n")
);
assert_eq!(input.replace("\r\n", "\n"), toml.to_string().replace("\r\n", "\n"));
#[cfg(not(target_os = "windows"))]
assert_eq!(input, toml.to_string_in_original_order());
assert_eq!(input, toml.to_string());
}

#[cfg(target_os = "windows")]
Expand All @@ -278,16 +282,16 @@ mod test {
let input = fs::read_to_string("examp/clippy.toml").unwrap();
let mut toml = input.parse::<Document>().unwrap();
fmt_toml(&mut toml, &Config::new());
assert_ne!(input, toml.to_string_in_original_order());
// println!("{}", toml.to_string_in_original_order());
assert_ne!(input, toml.to_string());
// println!("{}", toml.to_string());
}

#[test]
fn trailing() {
let input = fs::read_to_string("examp/trailing.toml").unwrap();
let mut toml = input.parse::<Document>().unwrap();
fmt_toml(&mut toml, &Config::new());
assert_ne!(input, toml.to_string_in_original_order());
// println!("{}", toml.to_string_in_original_order());
assert_ne!(input, toml.to_string());
// println!("{}", toml.to_string());
}
}
3 changes: 1 addition & 2 deletions src/fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use afl::fuzz;

mod fmt;
mod sort;
mod toml_edit;

use fmt::Config;
use toml_edit::Document;
Expand All @@ -27,7 +26,7 @@ fn main() {
],
);
fmt::fmt_toml(&mut toml, &Config::new());
let s = toml.to_string_in_original_order();
let s = toml.to_string();
assert!(s.parse::<Document>().is_ok())
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use toml_edit::{Document, Item};

mod fmt;
mod sort;
mod toml_edit;

const EXTRA_HELP: &str = "\
NOTE: formatting is applied after the check for sorting so \
Expand Down Expand Up @@ -72,15 +71,15 @@ fn check_toml(path: &str, matches: &ArgMatches, config: &Config) -> IoResult<boo
flag_set("grouped", matches),
&config.table_order,
);
let mut sorted_str = sorted.to_string_in_original_order();
let mut sorted_str = sorted.to_string();
let is_sorted = toml_raw == sorted_str;

let is_formatted =
// if no-format is not found apply formatting
if !flag_set("no-format", matches) || flag_set("check-format", matches) {
let original = sorted_str.clone();
fmt::fmt_toml(&mut sorted, config);
sorted_str = sorted.to_string_in_original_order();
sorted_str = sorted.to_string();
original == sorted_str
} else {
true
Expand Down
Loading

0 comments on commit cb8fe43

Please sign in to comment.