2024-02-08 06:12:14 +00:00
|
|
|
# yaml-rust2
|
2015-05-28 17:56:03 +00:00
|
|
|
|
2024-02-08 06:12:14 +00:00
|
|
|
A fully compliant YAML 1.2 implementation written in pure Rust.
|
|
|
|
This work is based on [`yaml-rust`](https://github.com/chyh1990/yaml-rust) with
|
|
|
|
fixes towards being compliant to the [YAML test
|
|
|
|
suite](https://github.com/yaml/yaml-test-suite/). `yaml-rust`'s parser is
|
|
|
|
heavily influenced by `libyaml` and `yaml-cpp`.
|
2015-05-28 17:56:03 +00:00
|
|
|
|
2024-03-17 09:24:09 +00:00
|
|
|
`yaml-rust2` is a pure Rust YAML 1.2 implementation that benefits from the
|
|
|
|
memory safety and other benefits from the Rust language.
|
2015-05-30 17:58:39 +00:00
|
|
|
|
|
|
|
## Quick Start
|
|
|
|
|
2016-10-21 23:25:03 +00:00
|
|
|
Add the following to the Cargo.toml of your project:
|
2015-05-30 17:58:39 +00:00
|
|
|
|
2017-05-10 21:09:30 +00:00
|
|
|
```toml
|
2015-06-04 08:39:53 +00:00
|
|
|
[dependencies]
|
2024-03-15 19:14:26 +00:00
|
|
|
yaml-rust2 = "0.6"
|
2015-06-04 08:39:53 +00:00
|
|
|
```
|
|
|
|
|
2024-02-08 06:12:14 +00:00
|
|
|
Use `yaml_rust2::YamlLoader` to load YAML documents and access them as `Yaml` objects:
|
2015-05-30 17:58:39 +00:00
|
|
|
|
2017-05-10 21:09:30 +00:00
|
|
|
```rust
|
2024-02-08 06:12:14 +00:00
|
|
|
use yaml_rust2::{YamlLoader, YamlEmitter};
|
2015-05-30 17:58:39 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let s =
|
|
|
|
"
|
|
|
|
foo:
|
|
|
|
- list1
|
|
|
|
- list2
|
|
|
|
bar:
|
|
|
|
- 1
|
|
|
|
- 2.0
|
|
|
|
";
|
2015-06-01 15:01:50 +00:00
|
|
|
let docs = YamlLoader::load_from_str(s).unwrap();
|
2015-05-30 17:58:39 +00: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);
|
|
|
|
|
2024-02-08 06:12:14 +00:00
|
|
|
// Array/map-like accesses are checked and won't panic.
|
|
|
|
// They will return `BadValue` if the access is invalid.
|
2015-05-30 17:58:39 +00:00
|
|
|
assert!(doc["INVALID_KEY"][100].is_badvalue());
|
2017-06-26 10:32:52 +00:00
|
|
|
|
2015-06-01 15:01:50 +00: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-30 17:58:39 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-02-08 06:12:14 +00:00
|
|
|
Note that `yaml_rust2::Yaml` implements `Index<&'a str>` and `Index<usize>`:
|
2015-05-30 17:58:39 +00:00
|
|
|
|
2024-02-08 06:12:14 +00:00
|
|
|
* `Index<usize>` assumes the container is an array
|
|
|
|
* `Index<&'a str>` assumes the container is a string to value map
|
2015-05-30 17:58:39 +00:00
|
|
|
* otherwise, `Yaml::BadValue` is returned
|
|
|
|
|
2024-02-08 06:12:14 +00:00
|
|
|
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.
|
2015-05-30 17:58:39 +00:00
|
|
|
|
|
|
|
## Features
|
|
|
|
|
|
|
|
* Pure Rust
|
2024-02-08 06:12:14 +00:00
|
|
|
* `Vec`/`HashMap` access API
|
2015-05-30 17:58:39 +00:00
|
|
|
* Low-level YAML events emission
|
|
|
|
|
2023-08-19 14:41:51 +00:00
|
|
|
## 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.
|
|
|
|
|
2015-05-28 17:56:03 +00:00
|
|
|
## Specification Compliance
|
|
|
|
|
2024-02-08 06:12:14 +00:00
|
|
|
This implementation is fully compatible with the YAML 1.2 specification. In
|
|
|
|
order to help with compliance, `yaml-rust2` tests against (and passes) the [YAML
|
|
|
|
test suite](https://github.com/yaml/yaml-test-suite/).
|
2019-07-24 16:25:10 +00:00
|
|
|
|
2024-03-20 06:34:17 +00:00
|
|
|
## Upgrading from yaml-rust
|
|
|
|
|
|
|
|
You can use `yaml-rust2` as a drop-in replacement for the original `yaml-rust` crate.
|
|
|
|
|
|
|
|
```toml
|
|
|
|
[dependencies]
|
|
|
|
yaml-rust = { version = "#.#", package = "yaml-rust2" }
|
|
|
|
```
|
|
|
|
|
|
|
|
This `Cargo.toml` declaration allows you to refer to this crate as `yaml_rust` in your code.
|
|
|
|
|
|
|
|
```rust
|
|
|
|
use yaml_rust::{YamlLoader, YamlEmitter};
|
|
|
|
```
|
|
|
|
|
2016-01-11 04:53:19 +00:00
|
|
|
## License
|
|
|
|
|
|
|
|
Licensed under either of
|
|
|
|
|
2024-02-08 06:12:14 +00:00
|
|
|
* Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
* MIT license (http://opensource.org/licenses/MIT)
|
2016-01-11 04:53:19 +00:00
|
|
|
|
|
|
|
at your option.
|
|
|
|
|
2024-02-08 06:12:14 +00:00
|
|
|
Since this repository was originally maintained by
|
|
|
|
[chyh1990](https://github.com/chyh1990), there are 2 sets of licenses.
|
|
|
|
A license of each set must be included in redistributions. See the
|
|
|
|
[LICENSE](LICENSE) file for more details.
|
|
|
|
|
|
|
|
You can find licences in the [`.licenses`](.licenses) subfolder.
|
|
|
|
|
2015-05-30 17:58:39 +00:00
|
|
|
## Contribution
|
|
|
|
|
|
|
|
Fork & PR on Github.
|
2016-01-11 04:53:19 +00:00
|
|
|
|
|
|
|
Unless you explicitly state otherwise, any contribution intentionally submitted
|
2024-02-08 06:12:14 +00:00
|
|
|
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.
|