blob: 64c62366f2837f4f1763f433ce5bef2055bde537 (
plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
use serde_json;
use source::Source;
use std::error::Error;
use std::collections::HashMap;
use value::Value;
pub struct Content {
// Root table of the TOML document
root: serde_json::Value,
}
impl Content {
pub fn parse(text: &str, namespace: Option<&String>) -> Result<Box<Source>, Box<Error>> {
// Parse
let mut root: serde_json::Value = serde_json::from_str(text)?;
// Limit to namespace
if let Some(namespace) = namespace {
if let serde_json::Value::Object(mut root_map) = root {
if let Some(value) = root_map.remove(namespace) {
root = value;
} else {
// TODO: Warn?
root = serde_json::Value::Object(serde_json::Map::new());
}
}
}
Ok(Box::new(Content { root: root }))
}
}
fn from_json_value(value: &serde_json::Value) -> Value {
match *value {
serde_json::Value::String(ref value) => Value::String(value.clone()),
serde_json::Value::Number(ref value) => {
if let Some(value) = value.as_i64() {
Value::Integer(value)
} else if let Some(value) = value.as_f64() {
Value::Float(value)
} else {
unreachable!();
}
}
serde_json::Value::Bool(value) => Value::Boolean(value),
serde_json::Value::Object(ref table) => {
let mut m = HashMap::new();
for (key, value) in table {
m.insert(key.clone(), from_json_value(value));
}
Value::Table(m)
}
serde_json::Value::Array(ref array) => {
let mut l = Vec::new();
for value in array {
l.push(from_json_value(value));
}
Value::Array(l)
}
// TODO: What's left is JSON Null; how should we handle that?
_ => {
unimplemented!();
}
}
}
impl Source for Content {
fn collect(&self) -> HashMap<String, Value> {
if let Value::Table(table) = from_json_value(&self.root) {
table
} else {
// TODO: Better handle a non-object at root
// NOTE: I never want to support that but a panic is bad
panic!("expected object at JSON root");
}
}
}
|