Skip to content
CryoCryo home
Stdlibencoding

toml

TOML 1.0 text, read into and written from the shared data model Value. Not auto-imported.

Toml::parse(text) reads a document from a Str into a Result<Value, TomlError>: its root table as a Value::Object. Toml::to_text(&value) writes a document as an owned String, and Toml::write_text appends one to a String the caller owns; both refuse a value TOML cannot spell (TomlErrorKind::NotRepresentable).

The reader takes all of TOML 1.0: bare, quoted and dotted keys, tables, arrays of tables and inline tables, every string form with every escape, integers in every base with _ between digits, floats with inf and nan, booleans, and dates and times. A table is an Object, an array (of values or of tables) an Array, an integer a Number::Int, a float a Number::Float. A date or time is a Value::String holding its RFC 3339 text — 1979-05-27T07:32:00Z, 1979-05-27, 07:32:00 — the date and the time joined by T. TOML's rules on defining tables are kept: a key or a table defined twice is refused, each with a TomlErrorKind of its own.

The writer puts a table's values first and its tables after them, under [header]s; an array of tables under [[header]]s; anything else on one line. TOML has no null, so a table's null is left out and a null in an array is refused; a date read as text is written back as text.

A type that reads and writes Value through ToValue and FromValue reads and writes TOML too, in one step through FromToml and ToToml, which every such type has: Server::from_toml(text) parses the text and reads its root table as a Server, and server.to_toml() writes one, refusing a value TOML cannot spell.

import std::encoding::toml::{ FromToml, ToToml };

type struct Server with [FromToml, ToToml] {
    public host: String;
    public port: u16;
}

function main() -> int {
    match (Server::from_toml("host = \"example.com\"\nport = 8080\n")) {
        Result::Ok(server) => { return if (server.port == 8080) { 0 } else { 1 }; }
        Result::Err(e)     => { return 1; }
    }
}

Types

Toml

type struct Toml {}

Static functions

static parse(text: Str) -> Result<Value, TomlError>;

Read a TOML document: its root table as a Value::Object, or a TomlError pointing at the first byte that could not be read. Arrays, inline tables and tables nest at most 256 deep (TomlErrorKind::DepthExceeded).

static to_text(value: &Value) -> Result<String, TomlError>;

value, which must be an object, as a TOML document: its values as key = value lines, then its tables under their headers, each line ending with a line break.

static write_text(out: mut &String, value: &Value) -> Result<(), TomlError>;

Append value, which must be an object, to out as to_text writes it. On a refusal out may hold part of the document.

FromToml

type trait FromToml {
    static from_toml(text: Str) -> Result<This, FromTextError<TomlError>>;
}

Constructors

static from_toml(text: Str) -> Result<This, FromTextError<TomlError>>;

text read as a value of this type, or FromTextError::Parse with the TomlError where it is not TOML, or FromTextError::Decode with where its value is not this type.

ToToml

type trait ToToml {
    to_toml(&this) -> Result<String, TomlError>;
}

to_toml returns a Result, since TOML cannot spell every value.

Methods

to_toml(&this) -> Result<String, TomlError>;

This value as a TOML document, or the TomlError for a value TOML cannot spell: one that is not a table, or a null in an array.

module encoding::toml::error;
module encoding::toml::parser;
module encoding::toml::serializer;

export std::encoding::toml::error::{ TomlError, TomlErrorKind };
FileWhat it holdsImport
errorTomlError, TomlErrorKindimport std::encoding::toml::error;

The reader and writer behind Toml are internal to the module.