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>;
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>;
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;
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;
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;
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;
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>>;
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;
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;
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 };
| File | What it holds | Import |
|---|---|---|
error | YamlError, YamlErrorKind | import std::encoding::yaml::error; |
The reader and writer behind Yaml are internal to the module.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
namespace std::encoding::yaml;yaml: YAML 1.2 text with the core schema, read into and written from the shared data model std::encoding::value::Value.
type trait FromYaml {
static from_yaml(text: Str) -> Result<This, FromTextError<YamlError>>;
}A type read from YAML text: the one document the text holds, parsed as Yaml::parse parses it, then its value read as the type through FromValue. Every type with FromValue has it, a type of its own through with [FromYaml], which derives FromValue.
type trait ToYaml {
to_yaml(&this) -> String;
to_pretty_yaml(&this) -> String;
}A type written as YAML text: its Value, from ToValue, written as Yaml's writers write one. Every type with ToValue has it, a type of its own through with [ToYaml], which derives ToValue.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Server with [FromYaml, ToYaml]type struct Point with [Eq, Clone, Debug] { .. }Asks the compiler to generate implementations of the listed traits for the type, each working through its fields or variants in declaration order. The derivable traits are Eq, Ord, Hash, Clone, Debug, Default, ToValue and FromValue, and the text formats' FromJson, ToJson, FromYaml, ToYaml, FromToml and ToToml. The clause comes after the name and its parameters, before where.
public function name() { ... }
public:Visible to any module that imports this one - the default for top-level items and for struct fields, so it is mostly written to be explicit. public: opens a visibility block that groups the members after it. A module is always public, so public module is refused (E0117).
Server.host: Stringtype struct String<A = GlobalAllocator> {}Owned, growable UTF-8 text. It dereferences to Str, the read-only view most text functions take, so every Str method reads a String too (s.contains(...), s.split(...)); as_str hands the view out.
Server.port: u16function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function main() -> intmatch (subject) {
Pattern(x) => { ... }
_ => { ... }
}The primary discriminator: branches on enum variants, integers, ranges, constants, and tuple and destructuring patterns, such as (0, 1..8) or Option::Some((count, _)), and the compiler rejects a match that misses a case. There is no fallthrough. As an expression it evaluates to the chosen arm's value, so every arm must produce the same type.
type struct Server with [FromYaml, ToYaml]type enum Result<T, E> with [Eq, Clone, Debug] {
Ok(T);
Err(E);
}The outcome of an operation that can fail: Ok(value) on success, Err(error) on failure.
Result::Ok(T)serverpattern => { body }Separates a match arm's pattern from its body. Arms do not fall through.
const name: Type = value;An immutable binding: it cannot be reassigned after initialisation. The annotation may be omitted when an initialiser is present - the binding takes the initialiser's concrete type, inferred locally. Mutability is opt-in through mut.
const text: Stringserverreturn value;Leaves the current function with the given value. A function whose return type is not void must return one; a bare return; is for void. In an async function it completes the future with the value.
Result::Err(E)etype struct Yaml {}The YAML text format: reading a Value from YAML text and writing one as YAML text.
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
type trait FromYaml {
static from_yaml(text: Str) -> Result<This, FromTextError<YamlError>>;
}A type read from YAML text: the one document the text holds, parsed as Yaml::parse parses it, then its value read as the type through FromValue. Every type with FromValue has it, a type of its own through with [FromYaml], which derives FromValue.
static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static FromYaml::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.
(parameter) text: Strtype struct Str {}A read-only view of UTF-8 text owned elsewhere: a string literal, a String, a file's bytes. Copying a Str copies the view, not the text, and it is valid only while what it views is. Str::new("text") views a literal; String.as_str() views a String.
ThisThe implementing type, inside a trait or implement body. other: &This in a trait method is a reference to whichever type implements it; This::Item names an associated type.
type enum FromTextError<E> {
Parse(E);
Decode(ValueDecodeError);
}Why text could not be read as a type: it is not the format's text (Parse, holding the format's own error - a JsonError, YamlError or TomlError), or the value it holds is not the type (Decode).
type struct YamlError {
kind: YamlErrorKind;
line: u64;
column: u64;
offset: u64;
msg: String;
}Why YAML text could not be read, and where: the failure's kind, its position in the source, and a message for a person to read.
std::encoding::yaml::errortype trait ToYaml {
to_yaml(&this) -> String;
to_pretty_yaml(&this) -> String;
}A type written as YAML text: its Value, from ToValue, written as Yaml's writers write one. Every type with ToValue has it, a type of its own through with [ToYaml], which derives ToValue.
ToYaml::to_yaml(&this) -> StringThis value in flow style on one line, as Yaml::to_text writes it: {name: cryo, tags: [1, 2]}.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
ToYaml::to_pretty_yaml(&this) -> StringThis value in block style, one entry or item per line, as Yaml::to_pretty_text writes it.
module collections::array;
module { a::b; a::c; }
internal module a::detail;In a directory's _module.cryo aggregator, declares a submodule. A module is public; internal module keeps it and everything it declares to its own package. The line is structure only: reaching the child's names still takes an import or an export.
namespace std::encoding::yaml::error;YamlError: why YAML text could not be read, and where.
module pagenamespace std::encoding::yaml::parser;The reader behind Yaml::parse and Yaml::parse_all: YAML 1.2 text, core schema, read into the shared Value model.
namespace std::encoding::yaml::serializer;The writer behind Yaml's text writers.
export a::b::Name;
export a::b::{ X, Y as Z };Passes a name this module did not declare on to its importers, exactly as if they had imported the same path - how an aggregator presents a flat surface over its files. Every hop is explicit, and there is no glob form.
language referencetype enum YamlErrorKind with [Eq, Clone] {
UnexpectedEof;
UnexpectedChar;
BadIndentation;
InvalidEscape;
InvalidNumber;
DuplicateKey;
UndefinedAlias;
ExpansionLimit;
DepthExceeded;
UnsupportedTag;
ComplexKey;
MergeKey;
UnsupportedDirective;
MultipleDocuments;
}What stopped the reader. A YAML feature outside the subset this reader takes has a kind of its own, so a caller can tell "not YAML" from "YAML this reader refuses".
std::encoding::yaml::errortype struct Yaml {}The YAML text format: reading a Value from YAML text and writing one as YAML text.
static Yaml::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.
type enum Value {
Null;
Bool(boolean);
Number(Number);
String(String);
Array(Array<Value>);
Object(Object);
}One JSON value. Recursive through Array and Object.
static Yaml::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 Yaml::to_text(value: &Value) -> Stringvalue in flow style on one line: {name: cryo, tags: [1, 2]}.
function value<T>() -> TA uniform value of type T: any integer width over its whole range, boolean, f32/f64 in [0.0, 1.0), or a char that is any Unicode scalar value.
static Yaml::to_pretty_text(value: &Value) -> Stringvalue 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 Yaml::write_text(out: mut &String, value: &Value) -> voidAppend value in flow style to out, as to_text writes it.
static Yaml::write_pretty_text(out: mut &String, value: &Value) -> voidAppend value in block style to out, as to_pretty_text writes it.
type struct Server with [FromYaml, ToYaml]Value::NullYamlErrorKind::MultipleDocumentsMore than one document where exactly one was asked for.
std::encoding::yaml::errortype struct Array<T, A = GlobalAllocator> where A: Allocator {}A growable sequence of Ts stored contiguously, owning its elements; A is the allocator its storage comes from. Indexing out of range panics; get answers None instead.
(parameter) value: &Value(parameter) out: mut &Stringmut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
static FromYaml::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.
FromTextError::Parse(E)The text is not the format's: what its reader refused, and where.
std::encoding::value::convertFromTextError::Decode(ValueDecodeError)The text's value is not the type: where in it, and what was wanted there.
std::encoding::value::convertToYaml::to_yaml(&this) -> StringThis value in flow style on one line, as Yaml::to_text writes it: {name: cryo, tags: [1, 2]}.
ToYaml::to_pretty_yaml(&this) -> StringThis value in block style, one entry or item per line, as Yaml::to_pretty_text writes it.