moontoml TOML for MoonBit

Reference

API

Every public type, function and value. Signatures are the ones the compiler writes; the notes are the documentation comments in the source.

moonbitstack/moontoml

TOML 1.0.0: read a document into a Json tree, and write a Json tree back as TOML.

error Refused

Why a document was refused.

Redefined is the rule that matters most: §1 says a key may be defined only once, and so may a table, so a second definition is an error rather than a value that wins. It is the rule that makes a TOML file safe to append to.

pub(all) suberror Refused {
  Unexpected(At, Char)
  Truncated(At)
  BadEscape(At)
  BadNumber(At)
  BadDate(At)
  Redefined(At, String)
  Trailing(At)
  TooDeep(At)
  BadUtf8(At)
} derive(Eq, @debug.Debug)
pub fn Refused::at(Self) -> At

Where the refusal happened.

Every variant carries a position, so a caller reporting a line does not have to match all nine of them to get at it.

struct At

Where in the source something is: the offset in code units, and the line and column a person counts from one.

pub(all) struct At {
  at : Int32
  line : Int32
  column : Int32
} derive(Eq, @debug.Debug)

struct Flavor

How a document is read.

The preset is strict. Build another with Flavor::new, or update one in place with { ..strict, depth: 64 }.

There is no knob for a repeated key. TOML 1.0.0 §1 makes redefinition an error, not a policy, so making it configurable would be offering to read a document the format says is not one.

pub(all) struct Flavor {
  depth : Int32
} derive(Eq, @debug.Debug)
pub fn Flavor::new(depth? : Int32) -> Self

A flavor by name, every knob with the preset's value.

Functions

pub fn dump(Json, sort? : Bool) -> Bytes

The same, as UTF-8 bytes.

pub fn dumps(Json, sort? : Bool) -> String

Write a Json tree as a TOML document.

Tables go under their own headers and scalars stay at the level they are on, so the output reads like a configuration file someone wrote rather than one a program emitted. An array of tables is written as [[…]] when every one of its elements is a table, and inline otherwise, which is the only honest choice: TOML has no way to spell an array mixing tables with scalars across headers.

sort orders keys by name; off, they keep the order the tree has. A document read by this module and written again carries the same values, but not necessarily the same layout — TOML can spell the same tree several ways.

pub fn load(BytesView, flavor? : Flavor, bom? : Bool) -> Json raise Refused

The document in src, decoded as UTF-8.

TOML 1.0.0 §1 says a file is UTF-8 and does not allow a byte-order mark; bom skips one anyway, because an editor on Windows writes it and refusing the file helps nobody. Pass bom=false to hold the format to its word.

pub fn loads(StringView, flavor? : Flavor) -> Json raise Refused

The document in src.

TOML defines one document rather than a stream, so there is no plural form of this.

Values

pub let depth : Int32

How deep a document may nest.

500 is the preset: deep enough for real configuration, shallow enough that a hostile document cannot run the stack out.

pub let strict : Flavor

The preset.