yaml
Read and write YAML, the format of many configuration files (CI pipelines, Docker Compose, Kubernetes). A document is read into a json.Json value, the same tree the json and toml modules use, so its accessors (get, at, string, int, …) work here too.
import yaml
let text = "services:\n web:\n image: nginx:1.27\n ports:\n - \"8080:80\"\n environment:\n DEBUG: false\n WORKERS: 4\nnotes: |\n first line\n second line\n"let doc = try yaml.parse(&text)let web = doc.get(&"services").unwrap().get(&"web").unwrap()print(web.get(&"image").unwrap().string())print(web.get(&"ports").unwrap().at(0).unwrap().string())print(web.get(&"environment").unwrap().get(&"WORKERS").unwrap().int())print(doc.get(&"notes").unwrap().string())Some("nginx:1.27")Some("8080:80")Some(4)Some("first line\nsecond line\n")What is read: block mappings and lists by indentation, flow collections ([a, b], {a: 1}), plain, single-quoted and double-quoted scalars with all of YAML’s escapes, block scalars (|, >, with - and +), comments, and --- / ... document markers.
Plain values are typed as YAML 1.2’s core schema says: null, ~ and nothing are Null; true and false are Bool; numbers (also 0x and 0o) are Num in decimal; everything else is text. yes, no, on and off stay text, as in YAML 1.2. Quoted values are always text, and keys are always text.
What is refused
Section titled “What is refused”Anchors (&a), aliases (*a), merge keys (<<) and tags (!!str) are errors. Aliases let a few lines expand into gigabytes (“billion laughs”), so Brass does not read them. Duplicate keys are errors too, as are tabs used for indentation, bad indentation, unclosed quotes and brackets, and nesting deeper than 256 levels. Every YamlError carries its line.
import jsonimport yaml
let doc = yaml.parse(&"base: &b {x: 1}\ncopy: *b\n") catch |e| { print(f"line {e.line}: {e.message}") json.Json.Null}print(doc.is_null())line 1: anchors (`&name`) are not supportedtrueWrite YAML
Section titled “Write YAML”yaml.text writes any value in block style, two spaces per level. Text is written plain only when it reads back as the same text; anything that would read as a number, a bool, null, or YAML syntax is quoted.
import jsonimport yaml
let doc = json.Json.Object([ ("name", json.Json.Text("brass")), ("version", json.Json.Text("1.0")), ("tags", json.Json.List([json.Json.Text("c"), json.Json.Text("yes")])), ("build", json.Json.Object([("jobs", json.num(8))])),])print(yaml.text(&doc))name: brassversion: "1.0"tags: - c - "yes"build: jobs: 8Reference
Section titled “Reference”import yamlFunctions
Section titled “Functions”pub fn parse(text: &Str) -> json.Json ! YamlErrorReads one YAML document; an empty one is null, and a stream of several is an error.
Reads one document; a stream with several (--- between them) is an error that points to parse_all. An empty document is Null.
parse_all
Section titled “parse_all”pub fn parse_all(text: &Str) -> Vec<json.Json> ! YamlErrorReads every document of a stream separated by ---.
Reads every document of a stream, as Kubernetes manifests often hold several in one file.
import yaml
let docs = try yaml.parse_all(&"kind: Service\n---\nkind: Deployment\n")for d in &docs { print(d.get(&"kind").unwrap().string().unwrap_or("?"))}ServiceDeploymentpub fn text(value: &json.Json) -> StrBlock-style YAML that parse reads back as the same value.
Multi-line text is written double-quoted with \n, not as a | block. Comments and the original layout of a parsed file are not kept.
YamlError
Section titled “YamlError”pub struct YamlError { pub line: i64 pub message: Str}