summaryrefslogtreecommitdiffstats
path: root/src/file/yaml.rs
blob: 95a64b407c52e1a4dd96c90c12f29d6fa13fe734 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use yaml_rust as yaml;

use source::Source;
use std::error::Error;
use std::fmt;
use std::collections::{BTreeMap, HashMap};
use std::mem;
use value::Value;

pub struct Content {
    // Root table of the YAML document
    root: yaml::Yaml,
}

impl Content {
    pub fn parse(text: &str, namespace: Option<&String>) -> Result<Box<Source + Send + Sync>, Box<Error>> {
        let mut docs = yaml::YamlLoader::load_from_str(text)?;

        // Designate root
        let mut root = match docs.len() {
            0 => yaml::Yaml::Hash(BTreeMap::new()),
            1 => mem::replace(&mut docs[0], yaml::Yaml::Null),
            n => {
                return Err(Box::new(MultipleDocumentsError(n)));
            }
        };

        // Limit to namespace
        if let Some(namespace) = namespace {
            if let yaml::Yaml::Hash(mut root_map) = root {
                if let Some(value) = root_map.remove(&yaml::Yaml::String(namespace.clone())) {
                    root = value;
                } else {
                    // TODO: Warn?
                    root = yaml::Yaml::Hash(BTreeMap::new());
                }
            }
        }

        Ok(Box::new(Content { root: root }))
    }

    pub fn from_yaml(doc: yaml::Yaml) -> Content {
        Content { root: doc }
    }
}

fn from_yaml_value<'a>(value: &yaml::Yaml) -> Value {
    match *value {
        yaml::Yaml::String(ref value) => Value::String(value.clone()),
        yaml::Yaml::Real(ref value) => Value::Float(value.parse::<f64>().unwrap()),
        yaml::Yaml::Integer(value) => Value::Integer(value),
        yaml::Yaml::Boolean(value) => Value::Boolean(value),
        yaml::Yaml::Hash(ref table) => {
            let mut m = HashMap::new();
            for (key, value) in table {
                if let Some(k) = key.as_str() {
                    m.insert(k.to_owned(), from_yaml_value(value));
                }
                // TODO: should we do anything for non-string keys?
            }
            Value::Table(m)
        }
        yaml::Yaml::Array(ref array) => {
            let l: Vec<Value> = array.iter().map(from_yaml_value).collect();
            Value::Array(l)
        }
        // TODO: how should we handle Null and BadValue?
        _ => {
            unimplemented!();
        }

    }
}

impl Source for Content {
    fn collect(&self) -> HashMap<String, Value> {
        if let Value::Table(table) = from_yaml_value(&self.root) {
            table
        } else {
            // TODO: Better handle a non-object at root
            // NOTE: I never want to support that but a panic is bad
            panic!("expected object at YAML root");
        }
    }
}

#[derive(Debug, Copy, Clone)]
struct MultipleDocumentsError(usize);

impl fmt::Display for MultipleDocumentsError {
    fn fmt(&self, format: &mut fmt::Formatter) -> fmt::Result {
        write!(format, "Got {} YAML documents, expected 1", self.0)
    }
}

impl Error for MultipleDocumentsError {
    fn description(&self) -> &str {
        "More than one YAML document provided"
    }
}