-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ser): Make pretty actually pretty
This includes porting tests over from `toml-rs` to ensure we match their behavior. We removed the non-pretty case and the customization since those aren't as important for our use case (cargo) and we'll want to iterate on what we want from the API. Our "pretty" is different than `toml-rs` in that we prefer `"` for strings that could be literals but don't need to be literals to avoid escaping. Note: there are some other discrepancies in our "pretty" string handling that should be looked into, like switching to tripple quotes to avoid escaping. Fixes #192
- Loading branch information
Showing
5 changed files
with
212 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
pub(crate) struct Pretty; | ||
|
||
impl crate::visit_mut::VisitMut for Pretty { | ||
fn visit_document_mut(&mut self, node: &mut crate::Document) { | ||
crate::visit_mut::visit_document_mut(self, node); | ||
if let Some((_, first)) = node.iter_mut().next() { | ||
remove_table_prefix(first); | ||
} | ||
} | ||
|
||
fn visit_item_mut(&mut self, node: &mut crate::Item) { | ||
node.make_item(); | ||
|
||
crate::visit_mut::visit_item_mut(self, node); | ||
} | ||
|
||
fn visit_table_mut(&mut self, node: &mut crate::Table) { | ||
node.decor_mut().clear(); | ||
|
||
crate::visit_mut::visit_table_mut(self, node); | ||
} | ||
|
||
fn visit_value_mut(&mut self, node: &mut crate::Value) { | ||
node.decor_mut().clear(); | ||
|
||
crate::visit_mut::visit_value_mut(self, node); | ||
} | ||
|
||
fn visit_array_mut(&mut self, node: &mut crate::Array) { | ||
crate::visit_mut::visit_array_mut(self, node); | ||
|
||
if (0..=1).contains(&node.len()) { | ||
node.set_trailing(""); | ||
node.set_trailing_comma(false); | ||
} else { | ||
for item in node.iter_mut() { | ||
item.decor_mut().set_prefix("\n "); | ||
} | ||
node.set_trailing("\n"); | ||
node.set_trailing_comma(true); | ||
} | ||
} | ||
} | ||
|
||
fn remove_table_prefix(node: &mut crate::Item) { | ||
match node { | ||
crate::Item::None => {} | ||
crate::Item::Value(_) => {} | ||
crate::Item::Table(t) => t.decor_mut().set_prefix(""), | ||
crate::Item::ArrayOfTables(a) => { | ||
if let Some(first) = a.values.iter_mut().next() { | ||
remove_table_prefix(first); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#![cfg(feature = "easy")] | ||
|
||
use pretty_assertions::assert_eq; | ||
|
||
const PRETTY_STD: &str = r#"[example] | ||
array = [ | ||
"item 1", | ||
"item 2", | ||
] | ||
empty = [] | ||
one = ["one"] | ||
oneline = "this has no newlines." | ||
text = """ | ||
this is the first line | ||
this is the second line | ||
""" | ||
"#; | ||
|
||
#[test] | ||
fn pretty_std() { | ||
let toml = PRETTY_STD; | ||
let value: toml_edit::easy::Value = toml_edit::easy::from_str(toml).unwrap(); | ||
let result = toml_edit::easy::to_string_pretty(&value).unwrap(); | ||
println!("EXPECTED:\n{}", toml); | ||
println!("\nRESULT:\n{}", result); | ||
assert_eq!(toml, &result); | ||
} | ||
|
||
const PRETTY_TRICKY: &str = r##"[example] | ||
f = "\f" | ||
glass = """ | ||
Nothing too unusual, except that I can eat glass in: | ||
- Greek: Μπορώ να φάω σπασμένα γυαλιά χωρίς να πάθω τίποτα. | ||
- Polish: Mogę jeść szkło, i mi nie szkodzi. | ||
- Hindi: मैं काँच खा सकता हूँ, मुझे उस से कोई पीडा नहीं होती. | ||
- Japanese: 私はガラスを食べられます。それは私を傷つけません。 | ||
""" | ||
r = "\r" | ||
r_newline = """ | ||
\r | ||
""" | ||
single = "this is a single line but has \"\" cuz it\"s tricky" | ||
single_tricky = "single line with ''' in it" | ||
tabs = """ | ||
this is pretty standard | ||
\texcept for some \ttabs right here | ||
""" | ||
text = """ | ||
this is the first line. | ||
This has a ''' in it and \"\"\" cuz it's tricky yo | ||
Also ' and \" because why not | ||
this is the fourth line | ||
""" | ||
"##; | ||
|
||
#[test] | ||
fn pretty_tricky() { | ||
let toml = PRETTY_TRICKY; | ||
let value: toml_edit::easy::Value = toml_edit::easy::from_str(toml).unwrap(); | ||
let result = toml_edit::easy::to_string_pretty(&value).unwrap(); | ||
println!("EXPECTED:\n{}", toml); | ||
println!("\nRESULT:\n{}", result); | ||
assert_eq!(toml, &result); | ||
} | ||
|
||
const PRETTY_TABLE_ARRAY: &str = r##"[[array]] | ||
key = "foo" | ||
[[array]] | ||
key = "bar" | ||
[abc] | ||
doc = "this is a table" | ||
[example] | ||
single = "this is a single line string" | ||
"##; | ||
|
||
#[test] | ||
fn pretty_table_array() { | ||
let toml = PRETTY_TABLE_ARRAY; | ||
let value: toml_edit::easy::Value = toml_edit::easy::from_str(toml).unwrap(); | ||
let result = toml_edit::easy::to_string_pretty(&value).unwrap(); | ||
println!("EXPECTED:\n{}", toml); | ||
println!("\nRESULT:\n{}", result); | ||
assert_eq!(toml, &result); | ||
} | ||
|
||
const TABLE_ARRAY: &str = r##"[[array]] | ||
key = "foo" | ||
[[array]] | ||
key = "bar" | ||
[abc] | ||
doc = "this is a table" | ||
[example] | ||
single = "this is a single line string" | ||
"##; | ||
|
||
#[test] | ||
fn table_array() { | ||
let toml = TABLE_ARRAY; | ||
let value: toml_edit::easy::Value = toml_edit::easy::from_str(toml).unwrap(); | ||
let result = toml_edit::easy::to_string_pretty(&value).unwrap(); | ||
println!("EXPECTED:\n{}", toml); | ||
println!("\nRESULT:\n{}", result); | ||
assert_eq!(toml, &result); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters