silk.toml.format.

Format TOML documents without changing comments, value spellings, or declaration order.

When to use#

Use TomlFormat before saving edited TOML text. Use silk.toml.output to serialize a value tree.

Import as TomlFormat with import silk.toml.format { TomlFormat }.

Public declarations: 1.

TomlFormat#

silk
pub struct TomlFormat

A comment-preserving TOML formatter with two-space indentation for nested values.

Implementation TomlFormat#

silk
impl TomlFormat

Associated function format#

silk
pub effect fn format(source: string) -> String
    ! TomlError | OutOfMemoryError
    ? &mut Allocator

Returns formatted TOML text, preserving comments, literal spellings, and declaration order.

Details#

Spaces surround = and follow commas. Inline tables have spaces inside their braces. Nested values use two-space indentation. Existing line breaks inside arrays and inline tables remain. Dotted keys have no spaces around dots. Blank runs become one blank line, and nonempty output ends with LF. String contents remain byte-for-byte unchanged, including embedded line endings. Other line endings become LF. Repeated formatting returns the same text. Empty or whitespace-only input produces an empty string.

Errors#

  • TomlError if the input is invalid TOML, including duplicate keys and conflicting tables.

  • OutOfMemoryError if validation or output allocation fails. The input remains unchanged.

Associated function formatBytes#

silk
pub effect fn formatBytes(source: &[u8]) -> Bytes
    ! TomlError | OutOfMemoryError
    ? &mut Allocator

Returns formatted TOML file bytes, preserving comments, literal spellings, and declaration order.

Details#

Spaces surround = and follow commas; inline tables have spaces inside braces. Nested values use two-space indentation. Existing array and inline-table line breaks remain. Dotted keys have no spaces around dots. Blank runs become one blank line; nonempty output ends with LF. String contents remain byte-for-byte unchanged. Other line endings become LF. Repeated formatting returns the same bytes. Empty or whitespace-only input produces empty output.

Errors#

  • TomlError if the bytes are invalid UTF-8 or invalid TOML, including duplicate keys and conflicting tables.

  • OutOfMemoryError if validation or output allocation fails. The input remains unchanged.

Category: Data formats.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page