#yaml-parser #original #typing #tags #key #style #strict-yaml

strict-yaml-rust

通过残酷地切割原始 yaml-rust 包获得的严格 YAML 解析器

3 个版本

使用旧的 Rust 2015

0.1.2 2022年5月27日
0.1.1 2018年12月19日
0.1.0 2018年12月19日

#667 in 解析器实现

Download history 950/week @ 2024-03-13 976/week @ 2024-03-20 656/week @ 2024-03-27 936/week @ 2024-04-03 273/week @ 2024-04-10 354/week @ 2024-04-17 405/week @ 2024-04-24 506/week @ 2024-05-01 170/week @ 2024-05-08 208/week @ 2024-05-15 137/week @ 2024-05-22 53/week @ 2024-05-29 67/week @ 2024-06-05 121/week @ 2024-06-12 701/week @ 2024-06-19 158/week @ 2024-06-26

1,064 每月下载量
用于 mast-build

MIT/Apache

105KB
3K SLoC

strict-yaml-rust

yaml-rust 中提取代码获得的 Rust StrictYAML 实现。

Build Status Build status license version

这个包最初是从原始的作为功能门控的(#[cfg(feature)])分支开始的。

将其独立出来,可以同时在同一个应用程序中使用这两种实现(完整和严格),并确信预期遵守 StrictYAML 的文档不会错误地解析为完整的 YAML。

向原始包作者 Chen Yuheng 致敬。

什么是 StrictYAML?

StrictYAML 是 YAML 格式的一个子集,移除了规范中的一些麻烦部分

  • 无类型。StrictYAML 只知道字符串、数组和字典(映射)
  • 无重复键
  • 无标签
  • 无锚点或引用
  • 无“流”样式(嵌入 JSON)

简而言之,只保留我们所有人都知道和喜爱的 YAML 部分。

有关更多详细信息,请参阅原始文档和实现

注意事项

此实现中缺少一个 程序性模式,该模式将启用文档验证和类型值解析。似乎您仍然需要从字符串中解析整数和日期,或者 提交一个 PR

快速入门

将以下内容添加到您的项目 Cargo.toml 中

[dependencies]
strict-yaml-rust = "0.1"

[dependencies.yaml-rust]
git = "https://github.com/fralalonde/strict-yaml-rust.git"

并导入

extern crate strict_yaml_rust;

使用 yaml::StrictYamlLoader 加载 YAML 文档并将其作为 Vec/HashMap 访问

extern crate strict_yaml_rust;
use strict_yaml_rust::{StrictYamlLoader, StrictYamlEmitter};

fn main() {
    let s =
"
foo:
    - list1
    - list2
bar:
    - 1
    - 2.0
";
    let docs = StrictYamlLoader::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_str().unwrap(), "2.0");

    // Chained key/array access is checked and won't panic,
    // return BadValue if they are not exist.
    assert!(doc["INVALID_KEY"][100].is_badvalue());

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

请注意 strict_yaml_rust::StrictYaml 实现了 Index<&'a str> AND Index<usize>

  • Index<usize> 假设容器是数组
  • Index<&'a str> 假设容器是字符串到值映射
  • 否则,返回 Yaml::BadValue

特性

  • 纯Rust
  • 类似Ruby的数组/哈希访问API
  • 底层StrictYAML事件发射

符合规范

本实现旨在提供与StrictYAML规范完全兼容的StrictYAML解析器。

许可证

根据您的选择,许可为以下之一

贡献

在Github上Fork和PR。

除非您明确声明,否则根据Apache-2.0许可证定义的任何有意提交以包含在您的工作中的贡献,都将根据上述方式双重许可,不附加任何额外条款或条件。

依赖关系

~49KB