Esc

from_toml

procedureextensionsince 0.36.1Source
  • macOS: from_toml works on macOS
  • Linux: from_toml works on Linux
  • Windows: from_toml works on Windows
  • Wasm: from_toml works on Wasm

Decodes text into a T, which must be a struct, without building a document. The result takes its strings, arrays and pointers from context.allocator; free_toml_value releases them. After a failure everything was freed already and the value is a default T.

Signature

from_toml :: (
    $T: Type,
    text: string,
    options := Toml_Options.{}
) -> (T, Toml_Error)

Fields are matched by name (after @toml(rename=...) and rename_all=), and TOML's rules are enforced as parse_toml does: duplicate keys, redefined tables, syntax, UTF-8 and the limits in options. The notes are rename=, alias=, rename_all= (also for enum names, default snake_case), skip, skip_serializing, skip_deserializing, skip_serializing_if=empty|zero|null, default, flatten, as_number, range=lo..hi, message="..." (the text of the type and range errors of the field), unknown (a [..] Toml_Unknown field that collects ignored keys) and, on the struct, rename_all=, default and deny_unknown_fields. A field without default and without an initializer of its own is required.

Supported field types: integers, floats (integers are accepted), bool, string, enums (by name), Toml_Datetime, structs (tables), pointers (optional values), [..] T, [] T and [N] T (arrays, and arrays of tables with [[x]] for arrays of structs), Table(string, V) (a table with any keys) and Toml_Value (any value, as an untyped tree).

Example↗
Server :: struct {
    host: string;
    port: int; @toml(default)
    tags: [..] string; @toml(default)
}

server, error := from_toml(Server, "host = 'a'\nport = 80\n");
assert(error.kind == .NONE && server.host == "a" && server.port == 80);
free_toml_value(*server);

See also