| 1 |
pub(crate) struct Pretty { |
| 2 |
in_value: bool, |
| 3 |
} |
| 4 |
|
| 5 |
impl Pretty { |
| 6 |
pub(crate) fn new() -> Self { |
| 7 |
Self { in_value: false } |
| 8 |
} |
| 9 |
} |
| 10 |
|
| 11 |
impl crate::visit_mut::VisitMut for Pretty { |
| 12 |
fn visit_document_mut(&mut self, node: &mut crate::DocumentMut) { |
| 13 |
crate::visit_mut::visit_document_mut(self, node); |
| 14 |
} |
| 15 |
|
| 16 |
fn visit_item_mut(&mut self, node: &mut crate::Item) { |
| 17 |
if !self.in_value { |
| 18 |
node.make_item(); |
| 19 |
} |
| 20 |
|
| 21 |
crate::visit_mut::visit_item_mut(self, node); |
| 22 |
} |
| 23 |
|
| 24 |
fn visit_table_mut(&mut self, node: &mut crate::Table) { |
| 25 |
node.decor_mut().clear(); |
| 26 |
|
| 27 |
// Empty tables could be semantically meaningful, so make sure they are not implicit |
| 28 |
if !node.is_empty() { |
| 29 |
node.set_implicit(true); |
| 30 |
} |
| 31 |
|
| 32 |
crate::visit_mut::visit_table_mut(self, node); |
| 33 |
} |
| 34 |
|
| 35 |
fn visit_value_mut(&mut self, node: &mut crate::Value) { |
| 36 |
node.decor_mut().clear(); |
| 37 |
|
| 38 |
let old_in_value = self.in_value; |
| 39 |
self.in_value = true; |
| 40 |
crate::visit_mut::visit_value_mut(self, node); |
| 41 |
self.in_value = old_in_value; |
| 42 |
} |
| 43 |
|
| 44 |
fn visit_array_mut(&mut self, node: &mut crate::Array) { |
| 45 |
crate::visit_mut::visit_array_mut(self, node); |
| 46 |
|
| 47 |
if (0..=1).contains(&node.len()) { |
| 48 |
node.set_trailing(""); |
| 49 |
node.set_trailing_comma(false); |
| 50 |
} else { |
| 51 |
for item in node.iter_mut() { |
| 52 |
item.decor_mut().set_prefix("\n "); |
| 53 |
} |
| 54 |
node.set_trailing("\n"); |
| 55 |
node.set_trailing_comma(true); |
| 56 |
} |
| 57 |
} |
| 58 |
} |