Skip to content
CryoCryo home
Stdlibencoding

yaml

YAML 1.2 text with the core schema, read into and written from the shared data model Value. Not auto-imported.

Yaml::parse(text) reads the one document a Str holds into a Result<Value, YamlError>; Yaml::parse_all(text) reads every document of a stream separated by ---. Yaml::to_text(&value) writes flow style on one line and Yaml::to_pretty_text(&value) block style, indented by two spaces; write_text and write_pretty_text append to a String the caller owns instead.

The reader takes a safe subset of YAML 1.2: block and flow collections, plain, quoted and block scalars, comments, several documents, and anchors and aliases, whose expansion is bounded. Tags outside the core schema, complex keys, the merge key << and directives are refused, each with a YamlErrorKind of its own. A plain scalar's type is the core schema's, so yes and no are text, not booleans. A key is text whatever its spelling.

A type that reads and writes Value through ToValue and FromValue reads and writes YAML too, in one step through FromYaml and ToYaml, which every such type has: Server::from_yaml(text) parses the text and reads its value as a Server, and server.to_pretty_yaml() writes one in block style.

import std::encoding::yaml::{ FromYaml, ToYaml };

type struct Server with [FromYaml, ToYaml] {
    public host: String;
    public port: u16;
}

function main() -> int {
    match (Server::from_yaml("host: example.com\nport: 8080\n")) {
        Result::Ok(server) => {
            const text: String = server.to_pretty_yaml();
            return 0;
        }
        Result::Err(e) => { return 1; }
    }
}

Types

Yaml

type struct Yaml {}

Static functions

static parse(text: Str) -> Result<Value, YamlError>;

Read the one document text holds: Value::Null for text with no document, and YamlErrorKind::MultipleDocuments for text with more than one. The YamlError points at the first byte the reader could not take.

static parse_all(text: Str) -> Result<Array<Value>, YamlError>;

Read every document text holds, in order: those separated by --- and ended by .... Each document's anchors are its own.

static to_text(value: &Value) -> String;

value in flow style on one line: {name: cryo, tags: [1, 2]}.

static to_pretty_text(value: &Value) -> String;

value in block style: a mapping's entries and a sequence's items one per line, nested ones indented by two spaces, the text ending with a line break. An empty collection is written {} or [].

static write_text(out: mut &String, value: &Value) -> void;

Append value in flow style to out, as to_text writes it.

static write_pretty_text(out: mut &String, value: &Value) -> void;

Append value in block style to out, as to_pretty_text writes it.

FromYaml

type trait FromYaml {
    static from_yaml(text: Str) -> Result<This, FromTextError<YamlError>>;
}

Constructors

static from_yaml(text: Str) -> Result<This, FromTextError<YamlError>>;

text read as a value of this type, or FromTextError::Parse with the YamlError where it is not one YAML document, or FromTextError::Decode with where its value is not this type.

ToYaml

type trait ToYaml {
    to_yaml(&this) -> String;
    to_pretty_yaml(&this) -> String;
}

to_yaml writes flow style and to_pretty_yaml block style.

Methods

to_yaml(&this) -> String;

This value in flow style on one line, as Yaml::to_text writes it: {name: cryo, tags: [1, 2]}.

to_pretty_yaml(&this) -> String;

This value in block style, one entry or item per line, as Yaml::to_pretty_text writes it.

module encoding::yaml::error;
module encoding::yaml::parser;
module encoding::yaml::serializer;

export std::encoding::yaml::error::{ YamlError, YamlErrorKind };
FileWhat it holdsImport
errorYamlError, YamlErrorKindimport std::encoding::yaml::error;

The reader and writer behind Yaml are internal to the module.