Crates.io | yaml-rust-davvid |
lib.rs | yaml-rust-davvid |
version | 0.6.4 |
source | src |
created_at | 2021-03-21 02:51:04.130567 |
updated_at | 2024-03-20 09:27:08.329446 |
description | The missing YAML 1.2 parser for rust |
homepage | https://davvid.github.io/yaml-rust |
repository | https://github.com/davvid/yaml-rust |
max_upload_size | |
id | 371547 |
size | 326,527 |
IMPORTANT The enhancements from this fork have been merged into yaml-rust2, the de-facto Rust YAML implementation.
All future development has moved to yaml-rust2
.
Users are advised to upgrade to yaml-rust2
.
You can use yaml-rust2
as a drop-in replacement for yaml-rust
by setting the package
option
to yaml-rust2
in your project's Cargo.toml
.
[dependencies]
yaml-rust = { version = "0.7", package = "yaml-rust2" }
The original README from yaml-rust-davvid follows below:
The missing YAML 1.2 implementation for Rust.
yaml-rust
is a pure Rust YAML 1.2 implementation,
which enjoys the memory safety
property and other benefits from the Rust language.
The parser is heavily influenced by libyaml
and yaml-cpp
.
Add the following to the Cargo.toml of your project:
[dependencies]
yaml-rust = { version = "0.6", package = "yaml-rust-davvid" }
Use yaml::YamlLoader
to load the YAML documents and access it
as Vec/HashMap:
use yaml_rust::{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);
// Chained key/array access is checked and won't panic,
// returns BadValue if the key does not exist.
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 yaml_rust::Yaml
implements Index<&'a str>
& Index<usize>
:
Index<usize>
assumes the container is an ArrayIndex<&'a str>
assumes the container is a string to value MapYaml::BadValue
is returnedIf your document does not conform to this convention (e.g. map with
complex type key), you can use the Yaml::as_XXX
family API to access your
documents.
This implementation aims to provide YAML parser fully compatible with the YAML 1.2 specification. The parser can correctly parse almost all examples in the specification, except for the following known bugs:
However, the widely used library libyaml
also fails to parse these examples,
so it may not be a huge problem for most users.
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.
This crate's minimum supported rustc
version is 1.31 (released with Rust 2018, after v0.4.3), as this is the currently known minimum version for regex
as well.
Licensed under either of
at your option.
Fork & PR on Github.
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.
yaml-rust-davvid
is a fork of the original, and now unmaintained
yaml-rust parser by chyh1990.