Rust

yaml

Module

yaml_parse() (ext-yaml 2.3 over libyaml 0.2.5) with the default ini settings: timestamps, !!binary and !php/object stay strings.

Documents are split into events by libyaml itself (unsafe-libyaml, the C library translated to Rust), so exactly the same inputs are accepted and refused; the events are turned into PHP values as ext-yaml's parse.c and detect.c do: YAML 1.1 booleans (yes, off, y), integers in binary, octal (0755), hexadecimal, sexagesimal (1:20) and with _ or , separators, floats whose exponent has a sign, merge keys (<<), and keys converted like array offsets.

PHP: anchors are PHP references. An alias of a node that is still being built (a recursive structure) has no value here: it is refused as an unknown alias.

Contents

use php_std::yaml::parse;
Rust
pub fn parse(input: &[u8]) -> Option<Value>

yaml_parse($input): the first document of input as a PHP value, or None where PHP returns false (and warns): a malformed document or an unknown alias. An input without a document is null.

PHP: yaml_parse($input) with yaml.decode_timestamp, decode_binary and decode_php off (their defaults). Only the first document is read: errors after it are not reported.

Rust
use php_std::{Value, yaml};

let Some(Value::Array(config)) = yaml::parse(b"enabled: yes\nmode: 0755\nratio: 1.5\n") else { panic!() };
assert_eq!(config.get_str("enabled"), Some(&Value::Bool(true)));
assert_eq!(config.get_str("mode"), Some(&Value::Int(493)));
assert_eq!(config.get_str("ratio"), Some(&Value::Float(1.5)));
assert_eq!(yaml::parse(b"a: *missing"), None);