saphyr-serde/parser/README.md

140 lines
3.7 KiB
Markdown
Raw Normal View History

2015-05-28 17:56:03 +00:00
# yaml-rust
2015-05-30 17:58:39 +00:00
The missing YAML 1.2 implementation for Rust.
2015-05-28 17:56:03 +00:00
2015-05-30 16:13:21 +00:00
[![Build Status](https://travis-ci.org/chyh1990/yaml-rust.svg?branch=master)](https://travis-ci.org/chyh1990/yaml-rust)
[![Build status](https://ci.appveyor.com/api/projects/status/scf47535ckp4ylg4?svg=true)](https://ci.appveyor.com/project/chyh1990/yaml-rust)
[![license](https://img.shields.io/crates/l/yaml-rust.svg)](https://crates.io/crates/yaml-rust/)
[![version](https://img.shields.io/crates/v/yaml-rust.svg)](https://crates.io/crates/yaml-rust/)
2015-05-30 16:13:21 +00:00
2015-05-30 17:58:39 +00:00
`yaml-rust` is a pure Rust YAML 1.2 implementation without
2016-10-21 23:25:03 +00:00
any external dependencies, which enjoys the memory safety
2015-05-30 17:58:39 +00:00
property and other benefits from the Rust language.
2015-05-31 04:56:45 +00:00
The parser is heavily influenced by `libyaml` and `yaml-cpp`.
2015-05-30 17:58:39 +00:00
2015-06-01 15:01:50 +00:00
This crate works on all Rust supported platforms and
Rust 1.0.0 and nightly!
2015-06-01 15:35:31 +00:00
See [Document](http://chyh1990.github.io/yaml-rust/doc/yaml_rust/)
2016-10-21 23:25:03 +00:00
> NOTE: This library is still under heavy development.
2015-06-01 16:53:32 +00:00
> WARNING: This library needs more tests and it is NOT ready for
> parsing arbitrary user input from *untrusted source*.
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
```toml
2015-06-04 08:39:53 +00:00
[dependencies]
yaml-rust = "*"
```
or
```toml
2015-05-30 17:58:39 +00:00
[dependencies.yaml-rust]
git = "https://github.com/chyh1990/yaml-rust.git"
```
and import using *extern crate*:
```rust
2015-05-30 17:58:39 +00:00
extern crate yaml_rust;
```
Use `yaml::YamlLoader` to load the YAML documents and access it
as Vec/HashMap:
```rust
2015-05-30 17:58:39 +00:00
extern crate yaml_rust;
2015-06-01 15:01:50 +00:00
use yaml_rust::{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);
// Chained key/array access is checked and won't panic,
// return BadValue if they are not exist.
assert!(doc["INVALID_KEY"][100].is_badvalue());
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
}
```
2016-10-02 01:41:28 +00:00
Note that `yaml_rust::Yaml` implements `Index<&'a str>` & `Index<usize>`:
2015-05-30 17:58:39 +00: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-28 17:56:03 +00:00
## Specification Compliance
2015-05-30 17:58:39 +00:00
This implementation aims to provide YAML parser fully compatible with
2015-06-23 14:09:41 +00:00
the YAML 1.2 specification. The parser can correctly parse almost all
2015-05-30 17:58:39 +00: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,
so it may not be a huge problem for most users.
2015-05-28 17:56:03 +00:00
2015-05-30 17:58:39 +00:00
## Goals
* Encoder
2015-05-28 17:56:03 +00:00
* Tag directive
2015-05-28 18:26:37 +00:00
* Alias while desearilization
2015-05-30 17:58:39 +00: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-30 17:58:39 +00:00
## Contribution
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.