saphyr-serde/saphyr
Ethiraric d2caaf2ab3 Prepare the ground for annotated parsing.
* Make `YamlLoader` generic on the type of the `Node`. This is required
   because deeper node need to have annotations too.
 * Add a `LoadableYamlNode` trait, required for YAML node types to be
   loaded by `YamlLoader`. It contains methods required by `YamlLoader`
   during loading.
 * Implement `LoadableYamlNode` for `Yaml`.
 * Take `load_from_str` out of `YamlLoader` for parsing non-annotated
   nodes. This avoids every user to specify the generics in
   `YamlLoader::<Yaml>::load_from_str`.
2024-07-03 00:55:41 +02:00
..
.cargo Add bench_compare tool. 2024-03-15 18:29:54 +01:00
.github/workflows yaml-rust2 -> saphyr 2024-04-02 18:49:52 +02:00
.licenses Update licence, readme, doc. 2024-02-08 07:12:14 +01:00
documents maint: version-up and minimize where we mention the version 2024-03-25 22:25:46 -07:00
examples Prepare the ground for annotated parsing. 2024-07-03 00:55:41 +02:00
src Prepare the ground for annotated parsing. 2024-07-03 00:55:41 +02:00
tests Prepare the ground for annotated parsing. 2024-07-03 00:55:41 +02:00
.gitattributes Add linguist attributes for tests/*.rs.inc files 2024-03-25 20:11:04 +01:00
.gitignore Ignore untracked files 2016-05-27 13:42:17 +08:00
appveyor.yml Fix build 2020-05-27 14:36:22 +08:00
Cargo.toml yaml-rust2 -> saphyr 2024-04-02 18:49:52 +02:00
CHANGELOG.md Prepare the ground for annotated parsing. 2024-07-03 00:55:41 +02:00
garden.yaml garden: yaml-rust2 -> saphyr 2024-04-08 23:37:22 -07:00
justfile yaml-rust2 -> saphyr 2024-04-02 18:49:52 +02:00
LICENSE Update licence, readme, doc. 2024-02-08 07:12:14 +01:00
README.md Add base support for annotated YAML objects. 2024-07-03 00:55:41 +02:00

saphyr

saphyr is a fully compliant YAML 1.2 library written in pure Rust.

This work is based on yaml-rust with fixes towards being compliant to the YAML test suite. yaml-rust's parser is heavily influenced by libyaml and yaml-cpp.

saphyr is a pure Rust YAML 1.2 implementation that benefits from the memory safety and other benefits from the Rust language.

Quick Start

Installing

Add the following to your Cargo.toml:

[dependencies]
saphyr = "0.0.1"

or use cargo add to get the latest version automatically:

cargo add saphyr

Example

Use saphyr::YamlLoader to load YAML documents and access them as Yaml objects:

use saphyr::{YamlLoader, YamlEmitter};

fn main() {
    let s =
"
foo:
    - list1
    - list2
bar:
    - 1
    - 2.0
";
    let docs = YamlLoader::load_from_str(s).unwrap();

    // Multi document support, doc is a yaml::Yaml
    let doc = &docs[0];

    // Debug support
    println!("{:?}", doc);

    // Index access for map & array
    assert_eq!(doc["foo"][0].as_str().unwrap(), "list1");
    assert_eq!(doc["bar"][1].as_f64().unwrap(), 2.0);

    // Array/map-like accesses are checked and won't panic.
    // They will return `BadValue` if the access is invalid.
    assert!(doc["INVALID_KEY"][100].is_badvalue());

    // Dump the YAML object
    let mut out_str = String::new();
    {
        let mut emitter = YamlEmitter::new(&mut out_str);
        emitter.dump(doc).unwrap(); // dump the YAML object to a String
    }
    println!("{}", out_str);
}

Note that saphyr::Yaml implements Index<&'a str> and Index<usize>:

  • Index<usize> assumes the container is an array
  • Index<&'a str> assumes the container is a string to value map
  • otherwise, Yaml::BadValue is returned

Note that annotated::YamlData cannot return BadValue and will panic.

If your document does not conform to this convention (e.g. map with complex type key), you can use the Yaml::as_XXX family API of functions to access your objects.

Features

  • Pure Rust
  • Vec/HashMap access API

Security

This library does not try to interpret any type specifiers in a YAML document, so there is no risk of, say, instantiating a socket with fields and communicating with the outside world just by parsing a YAML document.

Specification Compliance

This implementation is fully compatible with the YAML 1.2 specification. The parser behind this library (saphyr-parser) tests against (and passes) the YAML test suite.

License

Licensed under either of

at your option.

Since this repository was originally maintained by chyh1990, there are 2 sets of licenses. A license of each set must be included in redistributions. See the LICENSE file for more details.

You can find licences in the .licenses subfolder.

Contribution

Fork this repository and Create a Pull Request on Github. You may need to click on "compare across forks" and select your fork's branch.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.