silk.schema.toml.

Read and write schema types as TOML.

When to use#

Use SchemaToml.decode for typed configuration files and SchemaToml.encode to write them. The type's schema controls field names, defaults, validation, and unknown fields.

Details#

TOML tables become schema objects, and arrays become lists. Strings remain text without scalar coercion. Encoding writes root assignments and nested inline tables in declaration order, with bare keys where TOML allows them. Absent optional fields are omitted. TOML has no null value; a null inside an array cannot be encoded.

Import as SchemaToml with import silk.schema.toml { SchemaToml }.

Public declarations: 3.

SchemaToml#

silk
pub struct SchemaToml

The entry points for reading and writing schema types as TOML.

Implementation SchemaToml#

silk
impl SchemaToml

Associated function decode#

silk
pub effect fn decode<T: Schema>(bytes: &[u8]) -> T
    ! SchemaError | OutOfMemoryError
    ? &mut Allocator

Reads a typed TOML document, stopping at the first problem.

Errors#

  • SchemaError if TOML syntax or schema validation fails. The path identifies the failing field.

  • OutOfMemoryError if the allocator cannot supply the document or value.

Associated function decodeAll#

silk
pub effect fn decodeAll<T: Schema>(bytes: &[u8]) -> T
    ! SchemaErrors | OutOfMemoryError
    ? &mut Allocator

Reads a typed TOML document and reports all schema problems, up to 64.

Details#

A field, element or variant that fails is recorded and skipped, and reading goes on with the next one, so one call lists every problem, in schema order: a missing key, a wrong type, a number out of range and a failed rule alike. Text that is not valid TOML is the one problem reported. At most MAX_PROBLEMS (64) are listed.

Errors#

  • SchemaErrors with every problem found.

  • OutOfMemoryError if the Allocator cannot supply the document or the value.

Associated function encode#

silk
pub effect fn encode<T: Schema>(value: &T) -> ()
    ! SchemaError | WriterError | OutOfMemoryError
    ? &mut Writer | &mut Allocator

Writes a typed value as a TOML document, without flushing the writer.

Details#

The root must be an object. Nested objects use inline tables. Absent optional fields are omitted.

Errors#

  • SchemaError if the root is not an object, a list contains null, or nesting exceeds 64 levels.

  • WriterError if output fails; already accepted bytes remain written.

  • OutOfMemoryError if a custom codec cannot allocate.

TomlSchemaReader#

silk
pub struct TomlSchemaReader<'document>

A schema reader over a parsed TOML value. Semantic errors have field paths; syntax errors also have byte spans.

Implementation TomlSchemaReader<'document>#

silk
impl<'document> TomlSchemaReader<'document>

Associated function make#

silk
pub fn make(value: &'document Value) -> Self

Returns a reader that stops at the first schema problem.

Associated function collectingAll#

silk
pub fn collectingAll(value: &'document Value) -> Self

Returns a reader that collects up to 64 schema problems.

Method intoErrors#

silk
pub fn intoErrors(self: Self) -> List<SchemaError>

Returns the recorded schema problems in traversal order.

Implementation SchemaReader for TomlSchemaReader<'document>#

silk
impl<'document> SchemaReader for TomlSchemaReader<'document>

Operation path#

silk
fn path(self: &mut Self) -> &mut SchemaPath

Operation collecting#

silk
fn collecting(self: &Self) -> bool

Operation lastKind#

silk
fn lastKind(self: &Self) -> ValueKind

Operation report#

silk
effect fn report(self: &mut Self, issue: Issue) -> ()
    ! Issue | OutOfMemoryError
    ? &mut Allocator

Operation mark#

silk
effect fn mark(self: &mut Self) -> usize
    ! OutOfMemoryError
    ? &mut Allocator

Operation recover#

silk
effect fn recover(self: &mut Self, mark: usize, skip: bool) -> ()
    ! Issue | OutOfMemoryError
    ? &mut Allocator

Operation release#

silk
fn release(self: &mut Self, mark: usize) -> ()

Operation boolean#

silk
effect fn boolean(self: &mut Self) -> bool
    ! Issue | OutOfMemoryError
    ? &mut Allocator

Operation signed#

silk
effect fn signed(self: &mut Self) -> i64
    ! Issue | OutOfMemoryError
    ? &mut Allocator

Operation unsigned#

silk
effect fn unsigned(self: &mut Self) -> u64
    ! Issue | OutOfMemoryError
    ? &mut Allocator

Operation float#

silk
effect fn float(self: &mut Self) -> f64
    ! Issue | OutOfMemoryError
    ? &mut Allocator

Operation text#

silk
effect fn text(self: &mut Self) -> String
    ! Issue | OutOfMemoryError
    ? &mut Allocator

Operation null#

silk
effect fn null(self: &mut Self) -> bool
    ! Issue | OutOfMemoryError
    ? &mut Allocator

Operation beginList#

silk
effect fn beginList(self: &mut Self) -> ()
    ! Issue | OutOfMemoryError
    ? &mut Allocator

Operation nextElement#

silk
effect fn nextElement(self: &mut Self) -> bool
    ! Issue | OutOfMemoryError
    ? &mut Allocator

Operation beginObject#

silk
effect fn beginObject(self: &mut Self) -> ()
    ! Issue | OutOfMemoryError
    ? &mut Allocator

Operation member#

silk
effect fn member(self: &mut Self, key: string) -> bool
    ! Issue | OutOfMemoryError
    ? &mut Allocator

Operation nextMember#

silk
effect fn nextMember(self: &mut Self) -> Option<String>
    ! Issue | OutOfMemoryError ? &mut Allocator

Operation rejectUnread#

silk
effect fn rejectUnread(self: &mut Self, expected: string<'static>) -> ()
    ! Issue | OutOfMemoryError
    ? &mut Allocator

Operation endObject#

silk
effect fn endObject(self: &mut Self) -> ()
    ! Issue | OutOfMemoryError
    ? &mut Allocator

TomlSchemaWriter#

silk
pub struct TomlSchemaWriter

A SchemaWriter that writes TOML assignments and inline tables to the ambient Writer.

When to use#

SchemaToml.encode uses it. The root must be an object. Optional fields with no value are omitted by the schema.

Implementation TomlSchemaWriter#

silk
impl TomlSchemaWriter

Associated function make#

silk
pub fn make() -> TomlSchemaWriter

Returns a writer for one top-level value.

Implementation SchemaWriter for TomlSchemaWriter#

silk
impl SchemaWriter for TomlSchemaWriter

Operation boolean#

silk
effect fn boolean(self: &mut Self, value: bool) -> ()
    ! Issue | WriterError
    ? &mut Writer

Writes true or false.

Operation signed#

silk
effect fn signed(self: &mut Self, value: i64) -> ()
    ! Issue | WriterError
    ? &mut Writer

Writes the integer in decimal.

Operation unsigned#

silk
effect fn unsigned(self: &mut Self, value: u64) -> ()
    ! Issue | WriterError
    ? &mut Writer

Writes the integer in decimal.

Operation float#

silk
effect fn float(self: &mut Self, value: f64) -> ()
    ! Issue | WriterError
    ? &mut Writer

Writes a TOML float, including infinities and NaN.

Operation text#

silk
effect fn text(self: &mut Self, value: string) -> ()
    ! Issue | WriterError
    ? &mut Writer

Writes a quoted TOML string.

Operation null#

silk
effect fn null(self: &mut Self) -> ()
    ! Issue | WriterError
    ? &mut Writer

Rejects null, which TOML cannot represent.

Operation beginList#

silk
effect fn beginList(self: &mut Self) -> ()
    ! Issue | WriterError
    ? &mut Writer

Writes [.

Operation element#

silk
effect fn element(self: &mut Self) -> ()
    ! Issue | WriterError
    ? &mut Writer

Writes , before every element but the first.

Operation endList#

silk
effect fn endList(self: &mut Self) -> ()
    ! Issue | WriterError
    ? &mut Writer

Writes ].

Operation beginObject#

silk
effect fn beginObject(self: &mut Self) -> ()
    ! Issue | WriterError
    ? &mut Writer

Starts the root document or a nested inline table.

Operation key#

silk
effect fn key(self: &mut Self, name: string) -> ()
    ! Issue | WriterError
    ? &mut Writer

Separates assignments and writes the key followed by =. A key of ASCII letters, digits, - and _ is written bare, any other key quoted.

Operation endObject#

silk
effect fn endObject(self: &mut Self) -> ()
    ! Issue | WriterError
    ? &mut Writer

Ends the root document or a nested inline table.

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