Skip to content

yaml

The yaml module provides YAML parsing and serialization. It supports single-document and multi-document YAML, converting between YAML text and Shape values.

use std::core::yaml

yaml::parse(text: string) -> Result<HashMap, string>

Section titled “yaml::parse(text: string) -> Result<HashMap, string>”

Parse a YAML string into Shape values. Mappings become HashMaps, sequences become arrays, and scalars map to their Shape equivalents. YAML null maps to None.

let config = yaml::parse("""
server:
host: localhost
port: 8080
""")?
print(config["server"]["host"]) // "localhost"

yaml::parse_all(text: string) -> Result<Array, string>

Section titled “yaml::parse_all(text: string) -> Result<Array, string>”

Parse a multi-document YAML string (documents separated by ---) into an array of values, one per document.

let docs = yaml::parse_all("""
---
name: doc1
---
name: doc2
---
name: doc3
""")?
print(docs.length) // 3

yaml::stringify(value) -> Result<string, string>

Section titled “yaml::stringify(value) -> Result<string, string>”

Serialize a Shape value to a YAML string.

let text = yaml::stringify({
name: "my-project",
version: 42,
active: true
})?
print(text)

Check whether a string is valid YAML. Returns true if the string parses successfully, false otherwise.

yaml::is_valid("key: value") // true
  • json — JSON parsing and serialization
  • toml — TOML parsing and serialization