2015-05-29 01:56:03 +08:00
# yaml-rust
2015-05-31 01:58:39 +08:00
The missing YAML 1.2 implementation for Rust.
2015-05-29 01:56:03 +08:00
2018-09-15 12:28:45 -07:00
[](https://travis-ci.org/chyh1990/yaml-rust)
[](https://ci.appveyor.com/project/chyh1990/yaml-rust)
[](https://crates.io/crates/yaml-rust)
[](https://docs.rs/yaml-rust)
2015-05-31 00:13:21 +08:00
2017-06-26 12:32:52 +02:00
`yaml-rust` is a pure Rust YAML 1.2 implementation,
which enjoys the memory safety
property and other benefits from the Rust language.
2015-05-31 12:56:45 +08:00
The parser is heavily influenced by `libyaml` and `yaml-cpp` .
2015-05-31 01:58:39 +08:00
## Quick Start
2016-10-22 01:25:03 +02:00
Add the following to the Cargo.toml of your project:
2015-05-31 01:58:39 +08:00
2017-05-10 22:09:30 +01:00
```toml
2015-06-04 16:39:53 +08:00
[dependencies]
2018-01-06 15:28:14 +08:00
yaml-rust = "0.4"
2015-06-04 16:39:53 +08:00
```
2017-06-26 12:32:52 +02:00
and import:
2015-05-31 01:58:39 +08:00
2017-05-10 22:09:30 +01:00
```rust
2015-05-31 01:58:39 +08:00
extern crate yaml_rust;
```
Use `yaml::YamlLoader` to load the YAML documents and access it
as Vec/HashMap:
2017-05-10 22:09:30 +01:00
```rust
2015-05-31 01:58:39 +08:00
extern crate yaml_rust;
2015-06-01 23:01:50 +08:00
use yaml_rust::{YamlLoader, YamlEmitter};
2015-05-31 01:58:39 +08:00
fn main() {
let s =
"
foo:
- list1
- list2
bar:
- 1
- 2.0
";
2015-06-01 23:01:50 +08:00
let docs = YamlLoader::load_from_str(s).unwrap();
2015-05-31 01:58:39 +08:00
// 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,
// return BadValue if they are not exist.
assert!(doc["INVALID_KEY"][100].is_badvalue());
2017-06-26 12:32:52 +02:00
2015-06-01 23:01:50 +08:00
// 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);
2015-05-31 01:58:39 +08:00
}
```
2016-10-02 03:41:28 +02:00
Note that `yaml_rust::Yaml` implements `Index<&'a str>` & `Index<usize>` :
2015-05-31 01:58:39 +08:00
* `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
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 to access your
documents.
## Features
* Pure Rust
* Ruby-like Array/Hash access API
* Low-level YAML events emission
2015-05-29 01:56:03 +08:00
## Specification Compliance
2015-05-31 01:58:39 +08:00
This implementation aims to provide YAML parser fully compatible with
2015-06-23 17:09:41 +03:00
the YAML 1.2 specification. The parser can correctly parse almost all
2015-05-31 01:58:39 +08:00
examples in the specification, except for the following known bugs:
* Empty plain scalar in certain contexts
However, the widely used library `libyaml` also fails to parse these examples,
2017-06-26 12:32:52 +02:00
so it may not be a huge problem for most users.
2015-05-29 01:56:03 +08:00
2015-05-31 01:58:39 +08:00
## Goals
* Encoder
2015-05-29 01:56:03 +08:00
* Tag directive
2020-06-01 22:59:27 +10:00
* Alias while deserialization
2015-05-31 01:58:39 +08:00
2019-07-24 18:25:10 +02:00
## Minimum Rust version policy
2020-06-01 20:18:27 +08:00
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` ](https://crates.io/crates/regex#minimum-rust-version-policy ) as well.
2019-07-24 18:25:10 +02:00
2016-01-11 12:53:19 +08:00
## License
Licensed under either of
* Apache License, Version 2.0 ([LICENSE-APACHE ](LICENSE-APACHE ) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT ](LICENSE-MIT ) or http://opensource.org/licenses/MIT)
at your option.
2015-05-31 01:58:39 +08:00
## Contribution
Fork & PR on Github.
2016-01-11 12:53:19 +08:00
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.