1 use std::error::Error;
2 use std::fmt;
3 use std::mem;
4
5 use yaml_rust as yaml;
6
7 use crate::map::Map;
8 use crate::value::{Value, ValueKind};
9
parse( uri: Option<&String>, text: &str, ) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>>10 pub fn parse(
11 uri: Option<&String>,
12 text: &str,
13 ) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
14 // Parse a YAML object from file
15 let mut docs = yaml::YamlLoader::load_from_str(text)?;
16 let root = match docs.len() {
17 0 => yaml::Yaml::Hash(yaml::yaml::Hash::new()),
18 1 => mem::replace(&mut docs[0], yaml::Yaml::Null),
19 n => {
20 return Err(Box::new(MultipleDocumentsError(n)));
21 }
22 };
23
24 // TODO: Have a proper error fire if the root of a file is ever not a Table
25 let value = from_yaml_value(uri, &root)?;
26 match value.kind {
27 ValueKind::Table(map) => Ok(map),
28
29 _ => Ok(Map::new()),
30 }
31 }
32
from_yaml_value( uri: Option<&String>, value: &yaml::Yaml, ) -> Result<Value, Box<dyn Error + Send + Sync>>33 fn from_yaml_value(
34 uri: Option<&String>,
35 value: &yaml::Yaml,
36 ) -> Result<Value, Box<dyn Error + Send + Sync>> {
37 match *value {
38 yaml::Yaml::String(ref value) => Ok(Value::new(uri, ValueKind::String(value.clone()))),
39 yaml::Yaml::Real(ref value) => {
40 // TODO: Figure out in what cases this can panic?
41 value
42 .parse::<f64>()
43 .map_err(|_| {
44 Box::new(FloatParsingError(value.to_string())) as Box<(dyn Error + Send + Sync)>
45 })
46 .map(ValueKind::Float)
47 .map(|f| Value::new(uri, f))
48 }
49 yaml::Yaml::Integer(value) => Ok(Value::new(uri, ValueKind::I64(value))),
50 yaml::Yaml::Boolean(value) => Ok(Value::new(uri, ValueKind::Boolean(value))),
51 yaml::Yaml::Hash(ref table) => {
52 let mut m = Map::new();
53 for (key, value) in table {
54 if let Some(k) = key.as_str() {
55 m.insert(k.to_owned(), from_yaml_value(uri, value)?);
56 }
57 // TODO: should we do anything for non-string keys?
58 }
59 Ok(Value::new(uri, ValueKind::Table(m)))
60 }
61 yaml::Yaml::Array(ref array) => {
62 let mut l = Vec::new();
63
64 for value in array {
65 l.push(from_yaml_value(uri, value)?);
66 }
67
68 Ok(Value::new(uri, ValueKind::Array(l)))
69 }
70
71 // 1. Yaml NULL
72 // 2. BadValue – It shouldn't be possible to hit BadValue as this only happens when
73 // using the index trait badly or on a type error but we send back nil.
74 // 3. Alias – No idea what to do with this and there is a note in the lib that its
75 // not fully supported yet anyway
76 _ => Ok(Value::new(uri, ValueKind::Nil)),
77 }
78 }
79
80 #[derive(Debug, Copy, Clone)]
81 struct MultipleDocumentsError(usize);
82
83 impl fmt::Display for MultipleDocumentsError {
fmt(&self, format: &mut fmt::Formatter) -> fmt::Result84 fn fmt(&self, format: &mut fmt::Formatter) -> fmt::Result {
85 write!(format, "Got {} YAML documents, expected 1", self.0)
86 }
87 }
88
89 impl Error for MultipleDocumentsError {
description(&self) -> &str90 fn description(&self) -> &str {
91 "More than one YAML document provided"
92 }
93 }
94
95 #[derive(Debug, Clone)]
96 struct FloatParsingError(String);
97
98 impl fmt::Display for FloatParsingError {
fmt(&self, format: &mut fmt::Formatter) -> fmt::Result99 fn fmt(&self, format: &mut fmt::Formatter) -> fmt::Result {
100 write!(format, "Parsing {} as floating point number failed", self.0)
101 }
102 }
103
104 impl Error for FloatParsingError {
description(&self) -> &str105 fn description(&self) -> &str {
106 "Floating point number parsing failed"
107 }
108 }
109