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 TableKindHow a table was first defined. An inline table is closed to later document definitions.
Implicit
Implicit = 0A table created by a later header path.
Dotted
Dotted = 1A table created by a dotted key.
Explicit
Explicit = 2A table defined by a header.
Inline
Inline = 3A table defined inside braces and closed to later definitions.
TomlEntry
pub struct TomlEntryOne ordered table member.
Field key
pub key: StringThe decoded member key.
Field value
pub value: ValueThe owned member value.
TomlChild
pub union TomlChildRecursive storage shared by tables and arrays.
Item
TomlChild.Item { value: Value }: TomlChildAn array element without a key.
Field value
pub value: ValueThe owned element value.
Member
TomlChild.Member { entry: TomlEntry }: TomlChildA table member with its key.
Field entry
pub entry: TomlEntryThe owned key and value.
Value
pub union ValueAn owned TOML value. Numeric and temporal spellings retain their validated source precision.
Integer
Value.Integer { text: String }: ValueA validated integer in its source spelling.
Field text
pub text: StringThe validated integer text.
Float
Value.Float { text: String }: ValueA validated float in its source spelling.
Field text
pub text: StringThe validated float text.
Boolean
Value.Boolean { value: bool }: ValueA TOML boolean.
Field value
pub value: boolThe boolean value.
OffsetDateTime
Value.OffsetDateTime { text: String }: ValueA date and time with an explicit offset and retained precision.
Field text
pub text: StringThe validated source text.
LocalDateTime
Value.LocalDateTime { text: String }: ValueA date and time without an offset and with retained precision.
Field text
pub text: StringThe validated source text.
LocalDate
Value.LocalDate { text: String }: ValueA calendar date without a time.
Field text
pub text: StringThe validated source text.
LocalTime
Value.LocalTime { text: String }: ValueA clock time without a date and with retained precision.
Field text
pub text: StringThe validated source text.
String
Value.String { text: String }: ValueDecoded UTF-8 text.
Field text
pub text: StringThe decoded text.
Array
Value.Array { values: silk/box.Box<silk/vector.Vector<silk/toml_value.TomlChild>>, tables: bool }: ValueOrdered 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: boolWhether the array came from [[table]] headers.
Table
Value.Table { entries: silk/box.Box<silk/vector.Vector<silk/toml_value.TomlChild>>, kind: TableKind }: ValueOrdered 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: TableKindHow the table was defined.
Toml
pub struct TomlOperations on complete owned TOML documents.
Associated function Toml.parse
pub effect<'life0> fn parse<'life0>(bytes: &'life0 [u8]) -> Value ! TomlError | OutOfMemoryError ? &mut AllocatorParses 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.