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