2015-05-24 18:16:28 +00:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::ops::Index;
|
2015-05-24 06:27:42 +00:00
|
|
|
use std::string;
|
2015-05-24 19:29:52 +00:00
|
|
|
use std::str::FromStr;
|
2015-05-24 06:27:42 +00:00
|
|
|
|
2015-05-24 17:34:18 +00:00
|
|
|
#[derive(Clone, PartialEq, PartialOrd, Debug, Eq, Ord)]
|
2015-05-24 06:27:42 +00:00
|
|
|
pub enum Yaml {
|
2015-05-25 11:31:33 +00:00
|
|
|
/// number types are stored as String, and parsed on demand.
|
2015-05-25 05:54:39 +00:00
|
|
|
Number(string::String),
|
2015-05-24 06:27:42 +00:00
|
|
|
String(string::String),
|
|
|
|
Boolean(bool),
|
|
|
|
Array(self::Array),
|
|
|
|
Hash(self::Hash),
|
|
|
|
Null,
|
2015-05-24 19:21:53 +00:00
|
|
|
/// Access non-exist node by Index trait will return BadValue.
|
|
|
|
/// This simplifies error handling of user.
|
2015-05-24 18:16:28 +00:00
|
|
|
BadValue,
|
2015-05-24 06:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type Array = Vec<Yaml>;
|
2015-05-24 17:34:18 +00:00
|
|
|
pub type Hash = BTreeMap<Yaml, Yaml>;
|
2015-05-24 06:27:42 +00:00
|
|
|
|
2015-05-24 17:34:18 +00:00
|
|
|
macro_rules! define_as (
|
|
|
|
($name:ident, $t:ident, $yt:ident) => (
|
|
|
|
pub fn $name(&self) -> Option<$t> {
|
|
|
|
match *self {
|
|
|
|
Yaml::$yt(v) => Some(v),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
macro_rules! define_as_ref (
|
|
|
|
($name:ident, $t:ty, $yt:ident) => (
|
|
|
|
pub fn $name(&self) -> Option<$t> {
|
|
|
|
match *self {
|
|
|
|
Yaml::$yt(ref v) => Some(v),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
impl Yaml {
|
|
|
|
define_as!(as_bool, bool, Boolean);
|
|
|
|
|
|
|
|
define_as_ref!(as_str, &str, String);
|
|
|
|
define_as_ref!(as_hash, &Hash, Hash);
|
|
|
|
define_as_ref!(as_vec, &Array, Array);
|
|
|
|
|
|
|
|
pub fn is_null(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
Yaml::Null => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-24 18:16:28 +00:00
|
|
|
pub fn is_badvalue(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
Yaml::BadValue => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-25 05:54:39 +00:00
|
|
|
pub fn as_number<T: FromStr>(&self) -> Option<T> {
|
2015-05-24 17:34:18 +00:00
|
|
|
match *self {
|
2015-05-25 05:54:39 +00:00
|
|
|
Yaml::Number(ref v) => {
|
2015-05-24 19:29:52 +00:00
|
|
|
v.parse::<T>().ok()
|
2015-05-24 17:34:18 +00:00
|
|
|
},
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
2015-05-24 18:16:28 +00:00
|
|
|
|
|
|
|
pub fn from_str(s: &str) -> Yaml {
|
|
|
|
Yaml::String(s.to_string())
|
|
|
|
}
|
2015-05-24 17:34:18 +00:00
|
|
|
}
|
|
|
|
|
2015-05-24 18:16:28 +00:00
|
|
|
static BAD_VALUE: Yaml = Yaml::BadValue;
|
|
|
|
impl<'a> Index<&'a str> for Yaml {
|
|
|
|
type Output = Yaml;
|
|
|
|
|
|
|
|
fn index(&self, idx: &'a str) -> &Yaml {
|
|
|
|
let key = Yaml::String(idx.to_string());
|
|
|
|
match self.as_hash() {
|
|
|
|
Some(h) => h.get(&key).unwrap_or(&BAD_VALUE),
|
|
|
|
None => &BAD_VALUE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Index<usize> for Yaml {
|
|
|
|
type Output = Yaml;
|
|
|
|
|
|
|
|
fn index(&self, idx: usize) -> &Yaml {
|
|
|
|
match self.as_vec() {
|
|
|
|
Some(v) => v.get(idx).unwrap_or(&BAD_VALUE),
|
|
|
|
None => &BAD_VALUE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-24 17:34:18 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use parser::Parser;
|
|
|
|
use yaml::Yaml;
|
2015-05-24 18:16:28 +00:00
|
|
|
#[test]
|
2015-05-24 17:34:18 +00:00
|
|
|
fn test_coerce() {
|
|
|
|
let s = "---
|
|
|
|
a: 1
|
|
|
|
b: 2.2
|
|
|
|
c: [1, 2]
|
|
|
|
";
|
|
|
|
let mut parser = Parser::new(s.chars());
|
|
|
|
let out = parser.load().unwrap();
|
2015-05-25 05:54:39 +00:00
|
|
|
assert_eq!(out["a"].as_number::<i32>().unwrap(), 1);
|
|
|
|
assert_eq!(out["b"].as_number::<f32>().unwrap(), 2.2f32);
|
|
|
|
assert_eq!(out["c"][1].as_number::<i32>().unwrap(), 2);
|
2015-05-24 18:16:28 +00:00
|
|
|
assert!(out["d"][0].is_badvalue());
|
2015-05-24 17:34:18 +00:00
|
|
|
//assert_eq!(out.as_hash().unwrap()[&Yaml::String("a".to_string())].as_i64().unwrap(), 1i64);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|