1 use std::error::Error;
2
3 use crate::map::Map;
4 use crate::value::{Value, ValueKind};
5
parse( uri: Option<&String>, text: &str, ) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>>6 pub fn parse(
7 uri: Option<&String>,
8 text: &str,
9 ) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
10 // Parse a TOML value from the provided text
11 // TODO: Have a proper error fire if the root of a file is ever not a Table
12 let value = from_toml_value(uri, &toml::from_str(text)?);
13 match value.kind {
14 ValueKind::Table(map) => Ok(map),
15
16 _ => Ok(Map::new()),
17 }
18 }
19
from_toml_value(uri: Option<&String>, value: &toml::Value) -> Value20 fn from_toml_value(uri: Option<&String>, value: &toml::Value) -> Value {
21 match *value {
22 toml::Value::String(ref value) => Value::new(uri, value.to_string()),
23 toml::Value::Float(value) => Value::new(uri, value),
24 toml::Value::Integer(value) => Value::new(uri, value),
25 toml::Value::Boolean(value) => Value::new(uri, value),
26
27 toml::Value::Table(ref table) => {
28 let mut m = Map::new();
29
30 for (key, value) in table {
31 m.insert(key.clone(), from_toml_value(uri, value));
32 }
33
34 Value::new(uri, m)
35 }
36
37 toml::Value::Array(ref array) => {
38 let mut l = Vec::new();
39
40 for value in array {
41 l.push(from_toml_value(uri, value));
42 }
43
44 Value::new(uri, l)
45 }
46
47 toml::Value::Datetime(ref datetime) => Value::new(uri, datetime.to_string()),
48 }
49 }
50