Yaml
Simplified YAML parser (pure March, no FFI).
Supports: block mappings, block sequences, flow mappings {}, flow sequences [], plain scalars (string/int/bool/null), single-quoted and double-quoted strings, comments (#), document separator (---), inline comments.
Does NOT support: anchors (&), aliases (*), explicit tags (!!), multi-document streams, merge keys (<<), block scalars (| and >).
Public API: Yaml.parse(src) : Result(YamlValue, YamlError) Yaml.parse_exn(src) : YamlValue (panics on error) Yaml.to_string(v) : String Yaml.get(v, key) : Option(YamlValue) Yaml.get_in(v, keys) : Option(YamlValue) Yaml.get_str(v, key) : Option(String) Yaml.get_int(v, key) : Option(Int) Yaml.get_bool(v, key) : Option(Bool) Yaml.get_seq(v) : Option(List(YamlValue))
Types
Functions
Get a key from a YMap value (the first entry with that key). Returns None if the key is absent or v is not a YMap.
Examples
march> Yaml.get(Yaml.parse_exn("a: 1\na: 2"), "a")
Some(YInt(1))
march> Yaml.get(Yaml.parse_exn("- a"), "a")
NoneGet a boolean value from a YMap, or None if the key is absent or its value is not a YBool. YAML 1.1 spellings such as yes/no parse as booleans.
Examples
march> Yaml.get_bool(Yaml.parse_exn("tls: yes"), "tls")
Some(true)
march> Yaml.get_bool(Yaml.parse_exn("tls: maybe"), "tls")
NoneNavigate nested YMap values by a list of keys. An empty key list returns Some(v); None if any step is missing or not a YMap.
Examples
march> let cfg = Yaml.parse_exn("server:\n host: example.com\n port: 8080")
march> Yaml.get_in(cfg, ["server", "port"])
Some(YInt(8080))
march> let cfg = Yaml.parse_exn("server:\n host: example.com\n port: 8080")
march> Yaml.get_in(cfg, ["server", "user"])
NoneGet an integer value from a YMap, or None if the key is absent or its value is not a YInt.
Examples
march> Yaml.get_int(Yaml.parse_exn("name: alice\nage: 30"), "age")
Some(30)
march> Yaml.get_int(Yaml.parse_exn("ratio: 0.5"), "ratio")
NoneGet the elements of a YSeq value, or None if v is not a YSeq.
Examples
march> Yaml.get_seq(Yaml.parse_exn("- 1\n- 2"))
Some([YInt(1), YInt(2)])
march> Yaml.get_seq(Yaml.parse_exn("a: 1"))
NoneGet a string value from a YMap, or None if the key is absent or its value is not a YStr (a plain scalar that parsed as a number or boolean is not converted back).
Examples
march> Yaml.get_str(Yaml.parse_exn("name: alice\nage: 30"), "name")
Some("alice")
march> Yaml.get_str(Yaml.parse_exn("name: alice\nage: 30"), "age")
NoneParse a YAML string. Returns Ok(YamlValue) or Err(YamlError(message, line, col)). Supports block mappings, block sequences, scalars, flow {}/[], quoted strings and comments; an optional leading --- is skipped. Plain scalars are typed: integers become YInt, floats YFloat; true/True/TRUE/yes/Yes/on and false/False/FALSE/no/No/off become YBool; null/Null/NULL/~/empty become YNull; anything else YStr. Duplicate keys are kept; lookups return the first. An empty (or comment-only) document is an error.
Examples
march> Yaml.parse("name: alice\nage: 30")
Ok(YMap([("name", YStr("alice")), ("age", YInt(30))]))
march> Yaml.parse("---\n- 1\n- two\n- ~")
Ok(YSeq([YInt(1), YStr("two"), YNull]))
march> Yaml.parse("")
Err(YamlError("empty document", 1, 0))Parse YAML like parse, returning the value directly. Panics with YAML parse error at LINE:COL: MESSAGE if the input is invalid (or empty), so use it only for trusted, known-good input.
Examples
march> Yaml.parse_exn("a: {b: 1, c: [x, y]}")
YMap([("a", YMap([("b", YInt(1)), ("c", YSeq([YStr("x"), YStr("y")]))]))])Serialize a YamlValue to a block-style YAML string, two spaces per nesting level, with no trailing newline. Strings that would otherwise re-parse as another type, or that contain flow/comment characters, are double-quoted and escaped; empty sequences and maps render as [] and {}. Known limitations: a sequence nested under a key is emitted at the key's own indentation, which parse does not read back; a YMap inside a YSeq is misindented; a top-level YSeq starts with a newline; and a string containing a newline but no other special character is emitted unquoted.
Examples
march> Yaml.to_string(Yaml.parse_exn("name: alice\nage: 30"))
"name: alice\nage: 30"
march> Yaml.to_string(YMap([("a", YMap([("b", YInt(1))])), ("k", YStr("true"))]))
"a:\n b: 1\nk: \"true\""