pub struct Formatting<'a> { /* private fields */ }to-toml only.Expand description
Controls how TOML output is formatted when serializing.
Formatting::preserved_from preserves formatting from a previously
parsed document, use Formatting::default() for standard formatting.
§Examples
use toml_spanner::{Arena, Formatting};
use std::collections::BTreeMap;
let arena = Arena::new();
let source = "key = \"value\"\n";
let doc = toml_spanner::parse(source, &arena).unwrap();
let mut map = BTreeMap::new();
map.insert("key", "updated");
let output = Formatting::preserved_from(&doc).format(&map).unwrap();
assert!(output.contains("key = \"updated\""));Implementations§
Source§impl<'a> Formatting<'a>
impl<'a> Formatting<'a>
Sourcepub fn preserved_from(doc: &'a Document<'a>) -> Self
pub fn preserved_from(doc: &'a Document<'a>) -> Self
Creates a formatting template from a parsed document.
Indent style is auto-detected from the source text, defaulting to 4 spaces when no indentation is found.
Sourcepub fn with_span_projection_identity(self) -> Self
pub fn with_span_projection_identity(self) -> Self
Enables span projection identity for array reprojection.
By default, no assumptions are made about the spans of the format target. With span projection identity enabled, spans of the target are assumed to correspond to spans of the formatting reference. This allows precise identity tracking of array elements through reordering, removal, and deep mutation instead of the default best-effort content-based matching.
Intended for the lower-level Table mutation APIs where the
target was produced by parsing the same text as the formatting
reference. When round-tripping through FromToml and ToToml,
spans are not preserved and this flag should not be used.
Breaking the span correspondence assumption leads to unspecified behavior, including panics or invalid TOML generation.
Sourcepub fn with_indentation(self, indent: Indent) -> Self
pub fn with_indentation(self, indent: Indent) -> Self
Sets the indentation style for expanded inline arrays. Overrides auto-detection.
Sourcepub fn format(&self, value: &dyn ToToml) -> Result<String, ToTomlError>
pub fn format(&self, value: &dyn ToToml) -> Result<String, ToTomlError>
Serializes a ToToml value into a TOML string.
The value must serialize to a table at the top level.
§Errors
Returns ToTomlError if serialization fails or the top-level value
is not a table.
Sourcepub fn format_table_to_bytes(&self, table: Table<'_>, arena: &Arena) -> Vec<u8> ⓘ
pub fn format_table_to_bytes(&self, table: Table<'_>, arena: &Arena) -> Vec<u8> ⓘ
Formats a Table directly into bytes.
Low-level primitive that normalizes and (when a source document is set) reprojects the table before emission. The provided arena is used for temporary allocations during emission.