Module std::encoding::yaml

YAML parsing and generation.

Parse YAML documents into values, build new values programmatically, and serialize back to YAML text.

Examples

import std::encoding::yaml;

fn main() {
    let doc = yaml.parse("name: Hew\nversion: 1");
    let name = doc.get_field("name");
    println(name.get_string());  // "Hew"
    name.free();
    doc.free();
}

Contents

Functions

Function parse

pub fn parse(s: string) -> Value

Parse a YAML string into a Value.

Preserves the legacy behavior: invalid YAML returns an invalid handle (type_of() == -1). Use try_parse for a structured error result.

Function try_parse

pub fn try_parse(s: string) -> Result<Value, ParseError>

Parse a YAML string into a Value.

Returns Err(ParseError::Invalid(message)) with the native parser message when parsing fails. Match on ParseError::Invalid(message) to recover the original parser message.

Function object

pub fn object() -> Value

Create a new empty YAML mapping.

Function array

pub fn array() -> Value

Create a new empty YAML sequence.

Function from_int

pub fn from_int(val: i64) -> Value

Create a YAML Value from an integer.

Function from_float

pub fn from_float(val: f64) -> Value

Create a YAML Value from a float.

Function from_string

pub fn from_string(val: string) -> Value

Create a YAML Value from a string.

Function from_bool

pub fn from_bool(val: bool) -> Value

Create a YAML Value from a boolean.

Function null

pub fn null() -> Value

Create a YAML null Value.

Types

Enum ParseError

Structured error type for YAML parsing failures.

Use Result<Value, ParseError> with try_parse for structured error handling. Import std::encoding::yaml when matching on the returned error value so you can use the existing unqualified ParseError::Invalid(message) constructor/pattern style.

Variants

Invalid(string)

The input was not valid YAML. Carries the native parser message.

Struct Value

An opaque YAML value.

Represents any YAML type: null, bool, integer, float, string, sequence, or mapping. Must be freed with free() when no longer needed.

Traits

Trait ValueMethods

Methods available on a YAML Value.

Extends the shared opaque-handle contract from std::encoding::wire::value_trait. Canonical methods are redeclared here with YAML-specific docstrings for documentation purposes.

Methods

fn stringify(self: Self) -> string

Serialize the value back to a YAML string.

fn type_of(self: Self) -> i32

Return the type tag (0=null, 1=bool, 2=i64, 3=float, 4=string, 5=sequence, 6=mapping).

fn get_bool(self: Self) -> i32

Extract the boolean value (0 or 1).

fn get_int(self: Self) -> i64

Extract the integer value.

fn get_float(self: Self) -> f64

Extract the floating-point value.

fn get_string(self: Self) -> string

Extract the string value.

fn get_field(self: Self, key: string) -> Value

Look up a field by key in a YAML mapping.

fn array_len(self: Self) -> i64

Return the number of elements in a YAML sequence.

fn array_get(self: Self, index: i64) -> Value

Return the element at the given index in a YAML sequence.

fn free(self: Self)

Release the value resources.

fn with_string(self: Self, key: string, val: string) -> Value

Set a string field on a mapping. Returns the mapping for chaining.

fn with_int(self: Self, key: string, val: i64) -> Value

Set an integer field. Returns the mapping for chaining.

fn with_float(self: Self, key: string, val: f64) -> Value

Set a float field. Returns the mapping for chaining.

fn with_bool(self: Self, key: string, val: bool) -> Value

Set a boolean field. Returns the mapping for chaining.

fn with_null(self: Self, key: string) -> Value

Set a null field. Returns the mapping for chaining.

fn with(self: Self, key: string, child: Value) -> Value

Set a Value child on a mapping. The child is consumed. Returns the mapping.

fn push(self: Self, child: Value) -> Value

Push a Value onto a sequence. The child is consumed. Returns the sequence.

fn push_int(self: Self, val: i64) -> Value

Push an integer onto a sequence. Returns the sequence.

fn push_string(self: Self, val: string) -> Value

Push a string onto a sequence. Returns the sequence.

fn push_float(self: Self, val: f64) -> Value

Push a float onto a sequence. Returns the sequence.

fn push_bool(self: Self, val: bool) -> Value

Push a boolean onto a sequence. Returns the sequence.

fn push_null(self: Self) -> Value

Push null onto a sequence. Returns the sequence.