parseToml

Decodes a TOML string

  1. void parseToml(string toml, T dest)
    void
    parseToml
    (
    T
    )
    (
    string toml
    ,
    ref T dest
    )
    if (
    is(T == struct)
    )
  2. T parseToml(string toml)

Parameters

toml string

A string containing TOML data.

dest T

The struct into which the parsed TOML data should be placed.

T

The type of struct to create and return.

Return Value

Type: void

The dest parameter, populated with data read from toml.

Throws

TomlSyntaxException if the given data is invalid TOML. TomlDuplicateNameException if the given data contains duplicate key or table names. TomlUnsupportedException if the given data contains TOML features not yet supported by the library. TomlInvalidValueException if the given data contains invalid values (e.g. a date with an invalid month). TomlTypeException if a declared key's value does not match the destination value.

Examples

A simple example of parseToml with an array of tables.

struct Configuration
{
    struct Account
    {
        string username;
        ulong id;
    }

    string serverAddress;
    int port;
    Account[] accounts;
}

string data = `

serverAddress = "127.0.0.1"
port = 11000

[[accounts]]
username = "Tom"
id = 0x827e7b52

[[accounts]]
username = "Jerry"
id = 0x99134cce

`;

Configuration config = parseToml!Configuration(data);

expect(config).toEqual(
    Configuration(
        "127.0.0.1",
        11_000,
        [
            Configuration.Account("Tom", 0x827e7b52),
            Configuration.Account("Jerry", 0x99134cce)
        ]
    )
);

Syntactically invalid TOML results in an exception.

struct S {}

try
{
    parseToml!S(`[[[bad`);
    assert(false, "Expected a TomlDecodingException to be thrown.");
}
catch (TomlDecodingException e)
{
    // As expected.
}

Duplicate key names result in an exception.

struct S
{
    int x;
}

try
{
    S s = parseToml!S(`
        x = 5
        x = 10
    `);
    assert(false, "Expected a TomlDecodingException to be thrown.");
}
catch (TomlDecodingException e)
{
    // As expected
}

Duplicate table names result in an exception.

struct S
{
    struct Fruit { string apple; string orange; }

    Fruit fruit;
}

try
{
    S s = parseToml!S(`
        [fruit]
        apple = "red"

        [fruit]
        orange = "orange"
    `);
    assert(false, "Expected a TomlDecodingException to be thrown.");
}
catch (TomlDecodingException e)
{
    // As expected
}

Meta