tomlify

Encodes a struct of type T into a TOML string.

Each field in the struct will be an entry in the resulting TOML string. If a field is itself a struct, then it will show up as a subtable in the TOML.

The struct may not contain any fields that are classes or pointers, because circular references are currently not checked for. This restriction may be lifted in the future.

string
tomlify
(
T
)
()
if (
is(T == struct)
)

Parameters

object T

The object to be converted into a TOML file.

T

The type of the given object.

Return Value

Type: string

A string containing TOML data representing the given object.

Examples

A simple example of tomlify with an array of tables.

    struct Forecast
    {
        struct Day
        {
            int min;
            int max;
        }

        struct Location
        {
            string name;
            real lat;
            real lon;
        }

        string temperatureUnit;
        Location location;
        Day[] days;
    }

    Forecast data = Forecast(
        "℃",
        Forecast.Location("Pedra Amarela", 38.76417, -9.436667),
        [
            Forecast.Day(18, 23),
            Forecast.Day(15, 21)
        ]
    );

    string toml = tomlify(data);

    toml.should.equalNoBlanks(`
temperatureUnit = "℃"

[location]
name = "Pedra Amarela"
lat = 38.76417
lon = -9.4366670

[[days]]
min = 18
max = 23

[[days]]
min = 15
max = 21
`
    );

A struct containing classes cannot be encoded.

class C {}

struct S
{
    C c;
}

S s;
static assert(
    !__traits(compiles, tomlify(s)),
    "Should not be able to compile when struct contains a class field."
);

A struct with a pointer to anything cannot be encoded.

struct S
{
    int* i;
}

S s;
static assert(
    !__traits(compiles, tomlify(s)),
    "Should not be able to compile when struct contains a pointer field."
);

Meta