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_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.

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.

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.