Skip to content

Commit

Permalink
feat(toml)!: Update generator by using toml_edit
Browse files Browse the repository at this point in the history
This is the other main half of #340.  Still have deprecations and tests left

Note that strings are rendered differently, see #287

By extension this also finishes up #396.

BREAKING CHANGES
- `impl Display for toml::Value` now renders as values, not documents, see instead `Table`
- `toml::ser::Serializer` only serializes documents, instead see `toml::ser::ValueSerializer`
- `toml::ser::tables_last` is removed, no longer needed
- `toml::ser::to_vec` is removed to mirror the loss of `from_slice`
- atm `toml::ser::to_string_pretty` just causes larger arrays to be indented
- `toml::ser::Error` is now opaque
- `toml::ser::Serializer::pretty_string` was removed
- `toml::ser::Serializer::pretty_string_literal` was removed
- `toml::ser::Serializer::pretty_array` was removed
- `toml::ser::Serializer::pretty_array_indent` was removed
- `toml::ser::Serializer::pretty_array_trailing_comma` was removed
- `toml::ser::Serializer` is now used used by value, rather than `&mut`

Fixes #396
  • Loading branch information
epage committed Jan 19, 2023
1 parent 6fe3305 commit 6e99df4
Show file tree
Hide file tree
Showing 8 changed files with 790 additions and 1,728 deletions.
68 changes: 68 additions & 0 deletions crates/toml/src/fmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#[derive(Copy, Clone)]
pub(crate) struct DocumentFormatter {
pub(crate) multiline_array: bool,
}

impl Default for DocumentFormatter {
fn default() -> Self {
Self {
multiline_array: false,
}
}
}

impl toml_edit::visit_mut::VisitMut for DocumentFormatter {
fn visit_document_mut(&mut self, node: &mut toml_edit::Document) {
toml_edit::visit_mut::visit_document_mut(self, node);
}

fn visit_item_mut(&mut self, node: &mut toml_edit::Item) {
let other = std::mem::take(node);
let other = match other.into_table().map(toml_edit::Item::Table) {
Ok(i) => i,
Err(i) => i,
};
let other = match other
.into_array_of_tables()
.map(toml_edit::Item::ArrayOfTables)
{
Ok(i) => i,
Err(i) => i,
};
*node = other;

toml_edit::visit_mut::visit_item_mut(self, node);
}

fn visit_table_mut(&mut self, node: &mut toml_edit::Table) {
node.decor_mut().clear();

// Empty tables could be semantically meaningful, so make sure they are not implicit
if !node.is_empty() {
node.set_implicit(true);
}

toml_edit::visit_mut::visit_table_mut(self, node);
}

fn visit_value_mut(&mut self, node: &mut toml_edit::Value) {
node.decor_mut().clear();

toml_edit::visit_mut::visit_value_mut(self, node);
}

fn visit_array_mut(&mut self, node: &mut toml_edit::Array) {
toml_edit::visit_mut::visit_array_mut(self, node);

if !self.multiline_array || (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);
}
}
}
4 changes: 3 additions & 1 deletion crates/toml/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,16 @@ pub use crate::value::Value;

pub mod ser;
#[doc(no_inline)]
pub use crate::ser::{to_string, to_string_pretty, to_vec, Serializer};
pub use crate::ser::{to_string, to_string_pretty, Serializer};
pub mod de;
#[doc(no_inline)]
pub use crate::de::{from_str, Deserializer, ValueDeserializer};

#[doc(hidden)]
pub mod macros;

mod fmt;

pub use serde_spanned::Spanned;

// Shortcuts for the module doc-comment
Expand Down
Loading

0 comments on commit 6e99df4

Please sign in to comment.