#yaml-parser #serde-yaml

no-std yaml-peg

A YAML 1.2 parser using a greedy parsing algorithm with PEG atoms

41 releases (9 stable)

1.0.8 May 10, 2024
1.0.7 Jan 12, 2024
1.0.6 Sep 18, 2023
1.0.5 Aug 31, 2022
0.12.0 Jul 26, 2021

#260 in Parser implementations

Download history 6/week @ 2024-06-23 119/week @ 2024-06-30 21/week @ 2024-07-07 121/week @ 2024-07-28 1/week @ 2024-09-08 61/week @ 2024-09-22 63/week @ 2024-09-29 2/week @ 2024-10-06

126 downloads per month
Used in 2 crates

MIT license

120KB
2.5K SLoC

yaml-peg

dependency status documentation

A YAML 1.2 parser using a greedy parsing algorithm with PEG atoms. Support anchors, directives, positions, tags, serde, and no-std.

Inspired from yaml-rust and serde-yaml.

This parser is not ensure about YAML spec but almost functions are well-implemented. The buffer reader has also not yet been implemented, but the chunks can be read by the sub-parsers.

The anchors can be visited by the loader after parsing.

use yaml_peg::{parse, node};

let doc = "
---
name: Bob
married: true
age: 46
";
let root = parse(doc).unwrap();
assert_eq!(root, vec![node!({
    "name" => "Bob",
    "married" => true,
    "age" => 46,
})]);

See the API doc for more information.

Features

  • Support no standard library #![no_std].

  • Different data holder Rc / Arc provides parallel visiting and less copy cost.

  • Provide document positions and tags on the nodes.

  • Support anchors / alias.

    • Direct mode: Embed the alias directly.
    • Cyclic mode: Keep the alias placeholder, for cyclic data.
  • Support YAML directives YAML and TAG.

    WARNING: %YAML 1.1 will still be treated as 1.2.

    %YAML 1.2
    %TAG !x! tag:my.prefix:
    ---
    
  • Support serde to help you serialize and deserialize a specific type. (as well as the non-cyclic anchors)

    use serde::Deserialize;
    use yaml_peg::serde::from_str;
    
    #[derive(Deserialize)]
    struct Member {
       name: String,
       married: bool,
       age: u8,
    }
    
    let doc = "
    ---
    name: Bob
    married: true
    age: 46
    ";
    // Return Vec<Member>, use `.remove(0)` to get the first one
    let officer = from_str::<Member>(doc).unwrap().remove(0);
    assert_eq!("Bob", officer.name);
    assert!(officer.married);
    assert_eq!(46, officer.age);
    

Dependencies

~1MB
~17K SLoC