Skip to content
Topaz6.0 · Before release
Download

Search documentation

Specification 6.0 · Not released · Tested product 5.21.3

Use arrow keys to choose a result. Press Enter to open.

Type an API name, diagnostic number, rule, or project topic.

    Library

    std.toml.stringify

    Serializes a TOMLValue as text.

    stringify(value: TOMLValue) -> Result<string, string>
    Module
    std.toml
    Receiver
    Not applicable
    Minimum arity
    1
    Maximum arity
    1

    Declaration and parameters

    Link

    Availability

    Included by default: Pending

    Import std.toml in language mode 5.21.

    The current compiler selects std/toml.tpz for this language mode. Availability in other released editions and inclusion in each installed distribution are Pending.

    Source evidence

    Receiver

    Parameter constraints

    stringify accepts a TOMLValue whose root is a table. A scalar or array root returns Err because it is not a TOML document. An empty table becomes empty text.

    Each table writes its scalar and array entries before child table sections. Both groups follow the decoded key order. Arrays retain element order. Table headers use the complete dotted path. A noninitial table section is separated by one blank line.

    Assignments use one space on either side of the equals sign. Inline arrays and tables use a comma followed by one space. Every nonempty document uses LF line endings and ends with one LF. Comments, indentation and source ordering are not reproduced.

    Bare keys use the TOML ASCII key alphabet. Other keys and string values use basic quoted strings with deterministic escaping. Integers use decimal digits without separators. Finite floats use the shortest binary64 roundtrip value and retain a decimal point or exponent to distinguish a float from an integer. Positive and negative zero are written as 0.0 and -0.0. Nonfinite values use inf, -inf or nan.

    Dates and times use their TOML kind, a capital T separator and a capital Z for a zero offset. Local kinds retain their lack of an offset. Fractional seconds omit redundant trailing zeroes.

    The value argument is evaluated once. Serialization performs no I/O, does not mutate the value and returns Err without partial output when the value cannot form a document. Parse the result with toml.parse to recover the same TOML kinds and values.

    Result

    The declared result type is Result<string, string>.

    Effects

    Failures

    Result and effects

    Link

    Write a TOML root table in deterministic key order with LF record endings.

    Related entries

    Link

    std.toml

    Write deterministic TOML documents

    Link
    Ruletopaz.stdlib.contract.toml-stringify.rule

    Write deterministic TOML documents

    stringify accepts a TOMLValue whose root is a table. A scalar or array root returns Err because it is not a TOML document. An empty table becomes empty text.

    Each table writes its scalar and array entries before child table sections. Both groups follow the decoded key order. Arrays retain element order. Table headers use the complete dotted path. A noninitial table section is separated by one blank line.

    Assignments use one space on either side of the equals sign. Inline arrays and tables use a comma followed by one space. Every nonempty document uses LF line endings and ends with one LF. Comments, indentation and source ordering are not reproduced.

    Bare keys use the TOML ASCII key alphabet. Other keys and string values use basic quoted strings with deterministic escaping. Integers use decimal digits without separators. Finite floats use the shortest binary64 roundtrip value and retain a decimal point or exponent to distinguish a float from an integer. Positive and negative zero are written as 0.0 and -0.0. Nonfinite values use inf, -inf or nan.

    Dates and times use their TOML kind, a capital T separator and a capital Z for a zero offset. Local kinds retain their lack of an offset. Fractional seconds omit redundant trailing zeroes.

    The value argument is evaluated once. Serialization performs no I/O, does not mutate the value and returns Err without partial output when the value cannot form a document. Parse the result with toml.parse to recover the same TOML kinds and values.

    Why

    A deterministic writer supports stable configuration diffs. Requiring a table root makes its successful output usable as a complete TOML document, and numeric normalization follows the stored numeric value.

    Source evidence

    Reject a scalar as a complete TOML document

    Link
    main.tpzReject a scalar as a complete TOML documentOpen in Playground
    1. import std.json
    2. import std.toml
    3. import std.test
    4. import std.io
    5. let tree = test.assertOk(json.parse("7"))
    6. let value = test.assertOk(toml.fromJson(tree))
    7. match (toml.stringify(value)) {
    8. case Ok(_) => io.writeLine("accepted")
    9. case Err(_) => io.writeLine("root table required")
    10. }
    Expected outputexit 0
    root table required
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. root table required
    Source evidence

    Write the binary64 value rather than its input spelling

    Link
    main.tpzWrite the binary64 value rather than its input spellingOpen in Playground
    1. import std.toml
    2. import std.test
    3. import std.io
    4. let value = test.assertOk(toml.parse("x = 0.10000000000000001\n"))
    5. io.writeLine(test.assertOk(toml.stringify(value)))
    Expected outputexit 0
    x = 0.1
    
    
    $ topaz run main.tpz · target product 5.21.3
    Expected result

    Expected result

    Execution completes. Exit code 0.

    Expected stdout
    1. x = 0.1
    Source evidence

    Menu

    Download