strict_yaml_rust/
lib.rs

1// Copyright 2015, Yuheng Chen. See the LICENSE file at the top-level
2// directory of this distribution.
3
4//! Strict YAML implementation in pure Rust.
5//!
6//! # Usage
7//!
8//! This crate is [on github](https://github.com/fralalonde/strict-yaml-rust) and can be
9//! used by adding `strict-yaml` to the dependencies in your project's `Cargo.toml`.
10//!
11//! ```toml
12//! [dependencies.strict-yaml]
13//! git = "https://github.com/fralalonde/strict-yaml-rust.git"
14//! ```
15//!
16//! And this in your crate root:
17//!
18//! ```rust
19//! extern crate strict_yaml_rust;
20//! ```
21//!
22//! Parse a string into `Vec<Yaml>` and then serialize it as a YAML string.
23//!
24//! # Examples
25//!
26//! ```
27//! use strict_yaml_rust::{StrictYamlLoader, StrictYamlEmitter};
28//!
29//! let docs = StrictYamlLoader::load_from_str("zug: [1, 2, 3]").unwrap();
30//! let doc = &docs[0]; // select the first document
31//! assert_eq!(doc["zug"].as_str(), Some("[1, 2, 3]")); // access elements by key
32//!
33//! let mut out_str = String::new();
34//! let mut emitter = StrictYamlEmitter::new(&mut out_str);
35//! emitter.dump(doc).unwrap(); // dump the YAML object to a String
36//!
37//! ```
38
39#![doc(html_root_url = "https://docs.rs/strict-yaml-rust/0.1.0")]
40#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))]
41#![cfg_attr(
42    feature = "cargo-clippy",
43    allow(match_same_arms, should_implement_trait)
44)]
45
46extern crate linked_hash_map;
47
48pub mod emitter;
49pub mod parser;
50pub mod scanner;
51pub mod strict_yaml;
52
53// reexport key APIs
54pub use emitter::{EmitError, StrictYamlEmitter};
55pub use parser::Event;
56pub use scanner::ScanError;
57pub use strict_yaml::{StrictYaml, StrictYamlLoader};
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_api() {
65        let s = "
66# from yaml-cpp example
67- name: Ogre
68  position: [0, 5, 0]
69  powers:
70    - name: Club
71      damage: 10
72    - name: Fist
73      damage: 8
74- name: Dragon
75  position: [1, 0, 10]
76  powers:
77    - name: Fire Breath
78      damage: 25
79    - name: Claws
80      damage: 15
81- name: Wizard
82  position: [5, -3, 0]
83  powers:
84    - name: Acid Rain
85      damage: 50
86    - name: Staff
87      damage: 3
88";
89        let docs = StrictYamlLoader::load_from_str(s).unwrap();
90        let doc = &docs[0];
91
92        assert_eq!(doc[0]["name"].as_str().unwrap(), "Ogre");
93
94        let mut writer = String::new();
95        {
96            let mut emitter = StrictYamlEmitter::new(&mut writer);
97            emitter.dump(doc).unwrap();
98        }
99
100        assert!(!writer.is_empty());
101    }
102
103    fn try_fail(s: &str) -> Result<Vec<StrictYaml>, ScanError> {
104        let t = StrictYamlLoader::load_from_str(s)?;
105        Ok(t)
106    }
107
108    #[test]
109    fn test_fail() {
110        let s = "
111# syntax error
112scalar
113key: [1, 2]]
114key1:a2
115";
116        assert!(StrictYamlLoader::load_from_str(s).is_err());
117        assert!(try_fail(s).is_err());
118    }
119}