| 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/chyh1990/yaml-rust) and can be |
| 9 |
//! used by adding `yaml-rust` to the dependencies in your project's `Cargo.toml`. |
| 10 |
//! |
| 11 |
//! ```toml |
| 12 |
//! [dependencies.yaml-rust] |
| 13 |
//! git = "https://github.com/chyh1990/yaml-rust.git" |
| 14 |
//! ``` |
| 15 |
//! |
| 16 |
//! And this in your crate root: |
| 17 |
//! |
| 18 |
//! ```rust |
| 19 |
//! extern crate 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 yaml_rust::{YamlLoader, YamlEmitter}; |
| 28 |
//! |
| 29 |
//! let docs = YamlLoader::load_from_str("[1, 2, 3]").unwrap(); |
| 30 |
//! let doc = &docs[0]; // select the first document |
| 31 |
//! assert_eq!(doc[0].as_i64().unwrap(), 1); // access elements by index |
| 32 |
//! |
| 33 |
//! let mut out_str = String::new(); |
| 34 |
//! let mut emitter = YamlEmitter::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/yaml-rust/0.4.5")] |
| 40 |
#![cfg_attr(feature = "cargo-clippy", allow(renamed_and_removed_lints))] |
| 41 |
#![cfg_attr(feature = "cargo-clippy", warn(cyclomatic_complexity))] |
| 42 |
#![cfg_attr( |
| 43 |
feature = "cargo-clippy", |
| 44 |
allow(match_same_arms, should_implement_trait) |
| 45 |
)] |
| 46 |
|
| 47 |
extern crate linked_hash_map; |
| 48 |
|
| 49 |
pub mod emitter; |
| 50 |
pub mod parser; |
| 51 |
pub mod scanner; |
| 52 |
pub mod yaml; |
| 53 |
|
| 54 |
// reexport key APIs |
| 55 |
pub use crate::emitter::{EmitError, YamlEmitter}; |
| 56 |
pub use crate::parser::Event; |
| 57 |
pub use crate::scanner::ScanError; |
| 58 |
pub use crate::yaml::{Yaml, YamlLoader}; |
| 59 |
|
| 60 |
#[cfg(test)] |
| 61 |
mod tests { |
| 62 |
use super::*; |
| 63 |
|
| 64 |
#[test] |
| 65 |
fn test_api() { |
| 66 |
let s = " |
| 67 |
# from yaml-cpp example |
| 68 |
- name: Ogre |
| 69 |
position: [0, 5, 0] |
| 70 |
powers: |
| 71 |
- name: Club |
| 72 |
damage: 10 |
| 73 |
- name: Fist |
| 74 |
damage: 8 |
| 75 |
- name: Dragon |
| 76 |
position: [1, 0, 10] |
| 77 |
powers: |
| 78 |
- name: Fire Breath |
| 79 |
damage: 25 |
| 80 |
- name: Claws |
| 81 |
damage: 15 |
| 82 |
- name: Wizard |
| 83 |
position: [5, -3, 0] |
| 84 |
powers: |
| 85 |
- name: Acid Rain |
| 86 |
damage: 50 |
| 87 |
- name: Staff |
| 88 |
damage: 3 |
| 89 |
"; |
| 90 |
let docs = YamlLoader::load_from_str(s).unwrap(); |
| 91 |
let doc = &docs[0]; |
| 92 |
|
| 93 |
assert_eq!(doc[0]["name"].as_str().unwrap(), "Ogre"); |
| 94 |
|
| 95 |
let mut writer = String::new(); |
| 96 |
{ |
| 97 |
let mut emitter = YamlEmitter::new(&mut writer); |
| 98 |
emitter.dump(doc).unwrap(); |
| 99 |
} |
| 100 |
|
| 101 |
assert!(!writer.is_empty()); |
| 102 |
} |
| 103 |
|
| 104 |
fn try_fail(s: &str) -> Result<Vec<Yaml>, ScanError> { |
| 105 |
let t = YamlLoader::load_from_str(s)?; |
| 106 |
Ok(t) |
| 107 |
} |
| 108 |
|
| 109 |
#[test] |
| 110 |
fn test_fail() { |
| 111 |
let s = " |
| 112 |
# syntax error |
| 113 |
scalar |
| 114 |
key: [1, 2]] |
| 115 |
key1:a2 |
| 116 |
"; |
| 117 |
assert!(YamlLoader::load_from_str(s).is_err()); |
| 118 |
assert!(try_fail(s).is_err()); |
| 119 |
} |
| 120 |
|
| 121 |
} |