Silk

silk/toml_value

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc.

Owned TOML documents with ordered tables and distinct scalar kinds.

When to use

Use Toml.parse to validate and own a complete TOML document, then borrow fields from its root table. Every allocation uses the ambient Allocator and partial trees drop on failure.

Details

Tables keep key order. Numeric and date/time values keep their validated source text. String values contain decoded UTF-8 text.

Gotchas

A borrowed field cannot outlive its document. Use silk.toml_output to write the owned tree.

Examples

Read a nested string

import silk.allocator as Allocators {Allocator, OutOfMemoryError}
import silk.effect as Effects {Effect}
import silk.option as Options {Option}
import silk.toml_scanner as Scanners {TomlError}
import silk.toml_value as Values {Toml}

effect fn read() -> i32 ! TomlError | OutOfMemoryError {
  let mut allocator = Allocator.systemAllocatorProvider()
  let document = run Toml.parse(b"[owner]\nname = \"Ada\"\n")
    |> Effect.provideMut<Allocator>(&mut allocator)
  let owner = Toml.field(&document, "owner")
  let name = Toml.fieldAt(move owner, "name")
    |> Toml.asString
  return match move name {
    Option<string>.Some {value} => {
      if value == "Ada" {
        return 0
      }
      return 1
    }
    Option<string>.None => {
      return 2
    }
  }
}

effect fn recover(error: TomlError | OutOfMemoryError) -> i32 {
  return 3
}

pub fn main() -> i32 {
  return run Effect.catchAll(read(), recover)
}

Import as Toml with import silk.toml_value { Toml }.

Public declarations: 5.

TableKind

pub enum TableKind

How a table was first defined. An inline table is closed to later document definitions.

Implicit

Implicit = 0

A table created by a later header path.

Dotted

Dotted = 1

A table created by a dotted key.

Explicit

Explicit = 2

A table defined by a header.

Inline

Inline = 3

A table defined inside braces and closed to later definitions.

TomlEntry

pub struct TomlEntry

One ordered table member.

Field key

pub key: String

The decoded member key.

Field value

pub value: Value

The owned member value.

TomlChild

pub union TomlChild

Recursive storage shared by tables and arrays.

Item

TomlChild.Item { value: Value }: TomlChild

An array element without a key.

Field value

pub value: Value

The owned element value.

Member

TomlChild.Member { entry: TomlEntry }: TomlChild

A table member with its key.

Field entry

pub entry: TomlEntry

The owned key and value.

Value

pub union Value

An owned TOML value. Numeric and temporal spellings retain their validated source precision.

Integer

Value.Integer { text: String }: Value

A validated integer in its source spelling.

Field text

pub text: String

The validated integer text.

Float

Value.Float { text: String }: Value

A validated float in its source spelling.

Field text

pub text: String

The validated float text.

Boolean

Value.Boolean { value: bool }: Value

A TOML boolean.

Field value

pub value: bool

The boolean value.

OffsetDateTime

Value.OffsetDateTime { text: String }: Value

A date and time with an explicit offset and retained precision.

Field text

pub text: String

The validated source text.

LocalDateTime

Value.LocalDateTime { text: String }: Value

A date and time without an offset and with retained precision.

Field text

pub text: String

The validated source text.

LocalDate

Value.LocalDate { text: String }: Value

A calendar date without a time.

Field text

pub text: String

The validated source text.

LocalTime

Value.LocalTime { text: String }: Value

A clock time without a date and with retained precision.

Field text

pub text: String

The validated source text.

String

Value.String { text: String }: Value

Decoded UTF-8 text.

Field text

pub text: String

The decoded text.

Array

Value.Array { values: silk/box.Box<silk/vector.Vector<silk/toml_value.TomlChild>>, tables: bool }: Value

Ordered values or an array of tables.

Field values

pub values: silk/box.Box<silk/vector.Vector<silk/toml_value.TomlChild>>

The owned elements in source order.

Field tables

pub tables: bool

Whether the array came from [[table]] headers.

Table

Value.Table { entries: silk/box.Box<silk/vector.Vector<silk/toml_value.TomlChild>>, kind: TableKind }: Value

Ordered key/value members.

Field entries

pub entries: silk/box.Box<silk/vector.Vector<silk/toml_value.TomlChild>>

The owned members in source order.

Field kind

pub kind: TableKind

How the table was defined.

Toml

pub struct Toml

Operations on complete owned TOML documents.

Associated function Toml.parse

pub effect<'life0> fn parse<'life0>(bytes: &'life0 [u8]) -> Value ! TomlError | OutOfMemoryError ? &mut Allocator

Parses one complete UTF-8 document into an owned root table.

Details

All allocations use the ambient allocator. Syntax errors include a reason and byte offset. If parsing fails, the partial tree is released.

Associated function Toml.field

pub fn field<'a, 'life1>(self: &'a silk/toml_value.Value, name: string<'life1>) -> silk/option.Option<&'a silk/toml_value.Value>

Borrows a table member by decoded key, or returns None for absence or another value kind.

Associated function Toml.fieldAt

pub fn fieldAt<'a, 'life1>(self: silk/option.Option<&'a silk/toml_value.Value>, name: string<'life1>) -> silk/option.Option<&'a silk/toml_value.Value>

Borrows a nested table member through an optional value, or returns None.

Associated function Toml.item

pub fn item<'a>(self: &'a silk/toml_value.Value, index: usize) -> silk/option.Option<&'a silk/toml_value.Value>

Borrows an array element, or returns None for an absent index or another value kind.

Associated function Toml.asString

pub fn asString<'a>(self: silk/option.Option<&'a silk/toml_value.Value>) -> silk/option.Option<string<'a>>

Borrows decoded UTF-8 string text, or returns None for another value kind.

On this page