2 unstable releases

0.2.0 Aug 15, 2022
0.1.0 Oct 17, 2019

#2600 in Parser implementations


Used in yolol-yaml-deserializer

MIT license

39KB
774 lines

crates.io docs.rs

libyaml

This Rust crate provides high-level bindings for the LibYAML library via the unsafe-libyaml crate. It has the following limitations:

  • the token and document APIs are currently not implemented.

Installation

Just add this crate to your Cargo.toml:

[dependencies]
libyaml = "0.2"

You do not need to install the LibYAML library on the target system. Instead, unsafe-libyaml provides a transpiled version.

License

This crate is licensed under the MIT license.


lib.rs:

High-level bindings for the LibYAML library.

Reading YAML

To read a YAML stream, use Parser. This example counts the number of alias events in a stream.

#
let alias_count = Parser::new(reader)?.into_iter().filter(|e| {
    if let Ok(Event::Alias { .. }) = e { true } else { false }
}).count();

Writing YAML

To write a YAML stream, use Emitter. This example writes a stream with a single document consisting of a single scalar.

#
let mut emitter = Emitter::new(writer)?;

emitter.emit(Event::StreamStart { encoding: None })?;
emitter.emit(Event::DocumentStart { implicit: true })?;
emitter.emit(Event::Scalar {
    anchor: None,
    tag: Some(tag::INT.to_string()),
    value: "42".to_string(),
    plain_implicit: false,
    quoted_implicit: false,
    style: None,
})?;
emitter.emit(Event::DocumentEnd { implicit: true })?;
emitter.emit(Event::StreamEnd)?;

Dependencies

~440KB
~11K SLoC