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
|
use toml;
use source::Source;
use std::collections::{HashMap, BTreeMap};
use std::error::Error;
use value::Value;
pub fn parse(uri: Option<&String>, text: &str, namespace: Option<&String>) -> Result<Value, Box<Error>> {
// Parse a TOML value from the provided text
let mut root: toml::Value = toml::from_str(text)?;
// Limit to namespace
if let Some(namespace) = namespace {
root = toml::Value::Table(match root {
toml::Value::Table(ref mut table) => {
if let Some(toml::Value::Table(table)) = table.remove(namespace) {
table
} else {
BTreeMap::new()
}
}
_ => {
BTreeMap::new()
}
});
}
Ok(from_toml_value(uri, &root))
}
// TODO: Extend value origin with line/column numbers when able
fn from_toml_value(uri: Option<&String>, value: &toml::Value) -> Value {
match *value {
toml::Value::String(ref value) => Value::new(uri, value.to_string()),
toml::Value::Float(value) => Value::new(uri, value),
toml::Value::Integer(value) => Value::new(uri, value),
toml::Value::Boolean(value) => Value::new(uri, value),
toml::Value::Table(ref table) => {
let mut m = HashMap::new();
for (key, value) in table {
m.insert(key.clone(), from_toml_value(uri, value));
}
Value::new(uri, m)
}
toml::Value::Array(ref array) => {
let mut l = Vec::new();
for value in array {
l.push(from_toml_value(uri, value));
}
Value::new(uri, l)
}
_ => {
// TODO: DateTime
unimplemented!();
}
}
}
|