Skip to main content

Formatting

Struct Formatting 

Source
pub struct Formatting<'a> { /* private fields */ }
Available on crate feature 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>

Source

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.

Source

pub fn with_indentation(self, indent: Indent) -> Self

Sets the indentation style for expanded inline arrays. Overrides auto-detection.

Source

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.

Source

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.

Source

pub fn with_span_projection_identity(self) -> Self

Matches dest items to the formatting reference by span identity.

By default, Formatting pairs dest items with reference items by content equality. Content matching cannot distinguish items carrying identical values, so mutations that swap, remove, or reorder such items can silently reattach comments and numeric formatting to the wrong items.

With span identity enabled, each candidate pair’s spans are compared. When the spans match, the reference item’s formatting is projected onto the dest item. When they differ, the dest item is treated as if Item::set_ignore_source_formatting_recursively had been called on it: its subtree is emitted from scratch rather than pulling bytes from the reference text.

Intended for the lower-level Table mutation APIs where the dest tree was produced by cloning a parsed document. Not suitable for round-trips through FromToml and ToToml, which do not preserve spans.

The caller must ensure that every dest item carrying a non-empty span points into the reference document. Items replaced with fresh values, or otherwise stripped of their original spans, are not projected even when their content matches the reference.

§Examples
use toml_spanner::{Arena, Formatting};

let arena = Arena::new();
let source = "\
a = 1 # first
b = 1 # second
";
let doc = toml_spanner::parse(source, &arena).unwrap();

// Swap the two entries while keeping each item's original span.
let mut table = doc.table().clone_in(&arena);
let entries = table.entries_mut();
let (left, right) = entries.split_at_mut(1);
std::mem::swap(&mut left[0].1, &mut right[0].1);

// Span identity catches the swap: content matching would leave
// each comment stuck to its original key, misattributing them.
let bytes = Formatting::preserved_from(&doc)
    .with_span_projection_identity()
    .format_table_to_bytes(table, &arena);
let output = String::from_utf8(bytes).unwrap();
assert!(!output.contains("# first"));
assert!(!output.contains("# second"));

Trait Implementations§

Source§

impl<'a> Default for Formatting<'a>

Source§

fn default() -> Formatting<'a>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Formatting<'a>

§

impl<'a> !RefUnwindSafe for Formatting<'a>

§

impl<'a> !Send for Formatting<'a>

§

impl<'a> !Sync for Formatting<'a>

§

impl<'a> Unpin for Formatting<'a>

§

impl<'a> UnsafeUnpin for Formatting<'a>

§

impl<'a> !UnwindSafe for Formatting<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.