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
use types::{PropertyKeyInfo, ParseError, SourceFormat};
use erased_serde::Deserializer;
use std::collections::HashMap;
use serde_json;
use serde_yaml;

pub type StylePropertyField<'a> = (PropertyKeyInfo, Box<Deserializer<'a>>);

pub fn map_property_key_info(field: (String, Box<Deserializer>)) -> Result<StylePropertyField, ParseError> {
    use self::ParseError::*;

    let key_info = PropertyKeyInfo::new(field.0.as_str()).map_err(|error| PropertyError {
        error,
    })?;

    Ok((key_info, field.1))
}

/// Parser that return erased deserialize object, by different source type
pub fn generic_erase(source: &str, source_type: SourceFormat) -> Result<Vec<(String, Box<Deserializer>)>, ParseError> {
    use self::SourceFormat::*;
    use self::ParseError::*;

    match source_type {
        Yaml => serde_yaml::from_str::<HashMap<String, serde_yaml::Value>>(source)
            .map_err(|error| InvalidSource {
                source_type,
                error: error.into(),
            })
            .and_then(|yaml| {
                let mut erased = vec![];
                for (key, value) in yaml.into_iter() {
                    let value: Box<Deserializer> = Box::new(Deserializer::erase(value));
                    erased.push((key, value));
                }

                Ok(erased)
            }),
        Json => serde_json::from_str::<HashMap<String, serde_json::Value>>(source)
            .map_err(|error| InvalidSource {
                source_type,
                error: error.into(),
            })
            .and_then(|json| {
                let mut erased = vec![];
                for (key, value) in json.into_iter() {
                    let value: Box<Deserializer> = Box::new(Deserializer::erase(value));
                    erased.push((key, value));
                }

                Ok(erased)
            }),
    }
}