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>;
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>;
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>;
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>>;
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>;
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 };
| File | What it holds | Import |
|---|---|---|
error | TomlError, TomlErrorKind | import std::encoding::toml::error; |
The reader and writer behind Toml are internal to the module.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
namespace std::encoding::toml;toml: TOML 1.0 text, read into and written from the shared data model std::encoding::value::Value.
type trait FromToml {
static from_toml(text: Str) -> Result<This, FromTextError<TomlError>>;
}A type read from TOML text: the document parsed as Toml::parse parses it, then its root table read as the type through FromValue. Every type with FromValue has it, a type of its own through with [FromToml], which derives FromValue.
type trait ToToml {
to_toml(&this) -> Result<String, TomlError>;
}A type written as TOML text: its Value, from ToValue, written as Toml::to_text writes one. Every type with ToValue has it, a type of its own through with [ToToml], which derives ToValue.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Server with [FromToml, ToToml]type struct Point with [Eq, Clone, Debug] { .. }Asks the compiler to generate implementations of the listed traits for the type, each working through its fields or variants in declaration order. The derivable traits are Eq, Ord, Hash, Clone, Debug, Default, ToValue and FromValue, and the text formats' FromJson, ToJson, FromYaml, ToYaml, FromToml and ToToml. The clause comes after the name and its parameters, before where.
public function name() { ... }
public:Visible to any module that imports this one - the default for top-level items and for struct fields, so it is mostly written to be explicit. public: opens a visibility block that groups the members after it. A module is always public, so public module is refused (E0117).
Server.host: Stringtype struct String<A = GlobalAllocator> {}Owned, growable UTF-8 text. It dereferences to Str, the read-only view most text functions take, so every Str method reads a String too (s.contains(...), s.split(...)); as_str hands the view out.
Server.port: u16function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function main() -> intmatch (subject) {
Pattern(x) => { ... }
_ => { ... }
}The primary discriminator: branches on enum variants, integers, ranges, constants, and tuple and destructuring patterns, such as (0, 1..8) or Option::Some((count, _)), and the compiler rejects a match that misses a case. There is no fallthrough. As an expression it evaluates to the chosen arm's value, so every arm must produce the same type.
type struct Server with [FromToml, ToToml]type enum Result<T, E> with [Eq, Clone, Debug] {
Ok(T);
Err(E);
}The outcome of an operation that can fail: Ok(value) on success, Err(error) on failure.
Result::Ok(T)serverpattern => { body }Separates a match arm's pattern from its body. Arms do not fall through.
return value;Leaves the current function with the given value. A function whose return type is not void must return one; a bare return; is for void. In an async function it completes the future with the value.
if (condition) { ... } else if (condition) { ... } else { ... }Branches on a parenthesised boolean condition. Bodies are always braced; there is no single-statement form. Used as an expression, both arms are required and must produce the same type: const sign = if (n < 0) { -1 } else { 1 };.
serverif (condition) { ... } else { ... }The branch taken when every preceding if condition is false; else if chains another condition. In expression form the else arm is mandatory, so the expression always has a value.
Result::Err(E)etype struct Toml {}The TOML text format: reading a Value from TOML text and writing one as TOML text.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
type trait FromToml {
static from_toml(text: Str) -> Result<This, FromTextError<TomlError>>;
}A type read from TOML text: the document parsed as Toml::parse parses it, then its root table read as the type through FromValue. Every type with FromValue has it, a type of its own through with [FromToml], which derives FromValue.
static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static FromToml::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.
(parameter) text: Strtype struct Str {}A read-only view of UTF-8 text owned elsewhere: a string literal, a String, a file's bytes. Copying a Str copies the view, not the text, and it is valid only while what it views is. Str::new("text") views a literal; String.as_str() views a String.
ThisThe implementing type, inside a trait or implement body. other: &This in a trait method is a reference to whichever type implements it; This::Item names an associated type.
type enum FromTextError<E> {
Parse(E);
Decode(ValueDecodeError);
}Why text could not be read as a type: it is not the format's text (Parse, holding the format's own error - a JsonError, YamlError or TomlError), or the value it holds is not the type (Decode).
type struct TomlError {
kind: TomlErrorKind;
line: u64;
column: u64;
offset: u64;
msg: String;
}Why TOML text could not be read or a value written, and where: the failure's kind, its position in the source (0 for the writer's), and a message for a person to read.
std::encoding::toml::errortype trait ToToml {
to_toml(&this) -> Result<String, TomlError>;
}A type written as TOML text: its Value, from ToValue, written as Toml::to_text writes one. Every type with ToValue has it, a type of its own through with [ToToml], which derives ToValue.
ToToml::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.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
module collections::array;
module { a::b; a::c; }
internal module a::detail;In a directory's _module.cryo aggregator, declares a submodule. A module is public; internal module keeps it and everything it declares to its own package. The line is structure only: reaching the child's names still takes an import or an export.
namespace std::encoding::toml::error;TomlError: why TOML text could not be read or a value could not be written as TOML, and where.
module pagenamespace std::encoding::toml::parser;The reader behind Toml::parse: TOML 1.0 text read into the shared Value model.
namespace std::encoding::toml::serializer;The writer behind Toml::to_text and Toml::write_text.
export a::b::Name;
export a::b::{ X, Y as Z };Passes a name this module did not declare on to its importers, exactly as if they had imported the same path - how an aggregator presents a flat surface over its files. Every hop is explicit, and there is no glob form.
language referencetype enum TomlErrorKind with [Eq, Clone] {
UnexpectedEof;
UnexpectedChar;
InvalidEscape;
InvalidNumber;
InvalidDateTime;
DuplicateKey;
TableRedefined;
DepthExceeded;
NotRepresentable;
}What stopped the reader, or the writer.
std::encoding::toml::errortype struct Toml {}The TOML text format: reading a Value from TOML text and writing one as TOML text.
static Toml::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).
type enum Value {
Null;
Bool(boolean);
Number(Number);
String(String);
Array(Array<Value>);
Object(Object);
}One JSON value. Recursive through Array and Object.
Value::Object(Object)static Toml::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.
function value<T>() -> TA uniform value of type T: any integer width over its whole range, boolean, f32/f64 in [0.0, 1.0), or a char that is any Unicode scalar value.
static Toml::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.
TomlErrorKind::NotRepresentableA value the writer cannot write as TOML: a document that is not a table, or a null in an array.
std::encoding::toml::errortype struct Object {}JSON object. Insertion-ordered keys + parallel value array; a HashMap from String to slot index gives O(1) lookup. Owns its keys and values, and releases them when it goes out of scope.
type struct Array<T, A = GlobalAllocator> where A: Allocator {}A growable sequence of Ts stored contiguously, owning its elements; A is the allocator its storage comes from. Indexing out of range panics; get answers None instead.
type enum Number with [Debug, Clone] {
Int(i64);
UInt(u64);
Float(f64);
}JSON's single number type, split by source representation so integers round-trip exactly. Int covers signed values that fit i64; UInt carries values in (i64::MAX, u64::MAX]; everything else (decimals, exponents, NaN-shaped inputs the parser accepts as floats) is Float.
Number::Int(i64)Number::Float(f64)Value::String(String)type struct Server with [FromToml, ToToml]TomlErrorKind::DepthExceededArrays, inline tables and tables nested deeper than parser::MAX_DEPTH.
(parameter) value: &Value(parameter) out: mut &Stringmut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
primitive ()The unit type: a value that carries no information, and the empty tuple. Distinct from void, which is the absence of a value - Result<(), E> succeeds with nothing to return. 0 bytes.
static FromToml::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.
FromTextError::Parse(E)The text is not the format's: what its reader refused, and where.
std::encoding::value::convertFromTextError::Decode(ValueDecodeError)The text's value is not the type: where in it, and what was wanted there.
std::encoding::value::convertToToml::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.