yaml_rust_olidacombe/
lib.rs

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