2024-04-02 16:49:52 +00:00
|
|
|
# saphyr
|
2015-05-28 17:56:03 +00:00
|
|
|
|
2024-04-02 16:49:52 +00:00
|
|
|
[saphyr](https://github.com/saphyr-rs/saphyr) is a fully compliant YAML 1.2
|
|
|
|
library written in pure Rust.
|
2024-03-25 00:00:19 +00:00
|
|
|
|
2024-02-08 06:12:14 +00:00
|
|
|
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-04-02 16:49:52 +00:00
|
|
|
`saphyr` is a pure Rust YAML 1.2 implementation that benefits from the
|
2024-03-17 09:24:09 +00:00
|
|
|
memory safety and other benefits from the Rust language.
|
2015-05-30 17:58:39 +00:00
|
|
|
|
|
|
|
## Quick Start
|
2024-04-02 16:49:52 +00:00
|
|
|
### Installing
|
|
|
|
Add the following to your Cargo.toml:
|
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-04-02 16:49:52 +00:00
|
|
|
saphyr = "0.0.1"
|
|
|
|
```
|
|
|
|
or use `cargo add` to get the latest version automatically:
|
|
|
|
```sh
|
|
|
|
cargo add saphyr
|
2015-06-04 08:39:53 +00:00
|
|
|
```
|
|
|
|
|
2024-04-02 16:49:52 +00:00
|
|
|
### Example
|
|
|
|
Use `saphyr::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-04-02 16:49:52 +00:00
|
|
|
use saphyr::{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-04-02 16:49:52 +00:00
|
|
|
Note that `saphyr::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-06-10 20:39:13 +00:00
|
|
|
Note that `annotated::YamlData` cannot return `BadValue` and will panic.
|
|
|
|
|
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
|
|
|
|
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-04-02 16:49:52 +00:00
|
|
|
This implementation is fully compatible with the YAML 1.2 specification. The
|
|
|
|
parser behind this library
|
|
|
|
([`saphyr-parser`](https://github.com/saphyr-rs/saphyr-parser)) tests against
|
|
|
|
(and passes) the [YAML test suite](https://github.com/yaml/yaml-test-suite/).
|
2024-03-20 06:34:17 +00:00
|
|
|
|
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
|
|
|
|
|
2024-04-02 16:49:52 +00:00
|
|
|
[Fork this repository](https://github.com/saphyr-rs/saphyr/fork) and
|
|
|
|
[Create a Pull Request on Github](https://github.com/saphyr-rs/saphyr/compare/master...saphyr-rs:saphyr:master).
|
2024-03-25 00:00:19 +00:00
|
|
|
You may need to click on "compare across forks" and select your fork's branch.
|
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.
|
2024-03-25 00:00:19 +00:00
|
|
|
|
|
|
|
## Links
|
|
|
|
|
2024-04-02 16:49:52 +00:00
|
|
|
* [saphyr source code repository](https://github.com/saphyr-rs/saphyr)
|
|
|
|
|
|
|
|
* [saphyr releases on crates.io](https://crates.io/crates/saphyr)
|
2024-03-25 00:00:19 +00:00
|
|
|
|
2024-04-02 16:49:52 +00:00
|
|
|
* [saphyr documentation on docs.rs](https://docs.rs/saphyr/latest/saphyr/)
|
2024-03-25 00:00:19 +00:00
|
|
|
|
2024-04-02 16:49:52 +00:00
|
|
|
* [saphyr-parser releases on crates.io](https://crates.io/crates/saphyr-parser)
|
2024-03-25 00:00:19 +00:00
|
|
|
|
|
|
|
* [yaml-test-suite](https://github.com/yaml/yaml-test-suite)
|