Skip to content

Commit d548ffe

Browse files
Merge pull request #193 from matthiasbeyer/misc
Misc changes
2 parents 41a87ad + 6e17b50 commit d548ffe

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

src/source.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,15 @@ pub trait Source: Debug {
1414
fn collect(&self) -> Result<HashMap<String, Value>>;
1515

1616
fn collect_to(&self, cache: &mut Value) -> Result<()> {
17-
let props = match self.collect() {
18-
Ok(props) => props,
19-
Err(error) => {
20-
return Err(error);
21-
}
22-
};
23-
24-
for (key, val) in &props {
25-
match path::Expression::from_str(key) {
17+
self.collect()?
18+
.iter()
19+
.for_each(|(key, val)| match path::Expression::from_str(key) {
2620
// Set using the path
2721
Ok(expr) => expr.set(cache, val.clone()),
2822

2923
// Set diretly anyway
3024
_ => path::Expression::Identifier(key.clone()).set(cache, val.clone()),
31-
}
32-
}
25+
});
3326

3427
Ok(())
3528
}

src/value.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,8 @@ where
7272
T: Into<Value>,
7373
{
7474
fn from(values: HashMap<String, T>) -> Self {
75-
let mut r = HashMap::new();
76-
77-
for (k, v) in values {
78-
r.insert(k.clone(), v.into());
79-
}
80-
81-
ValueKind::Table(r)
75+
let t = values.into_iter().map(|(k, v)| (k, v.into())).collect();
76+
ValueKind::Table(t)
8277
}
8378
}
8479

@@ -87,13 +82,7 @@ where
8782
T: Into<Value>,
8883
{
8984
fn from(values: Vec<T>) -> Self {
90-
let mut l = Vec::new();
91-
92-
for v in values {
93-
l.push(v.into());
94-
}
95-
96-
ValueKind::Array(l)
85+
ValueKind::Array(values.into_iter().map(T::into).collect())
9786
}
9887
}
9988

@@ -105,10 +94,15 @@ impl Display for ValueKind {
10594
ValueKind::Integer(value) => write!(f, "{}", value),
10695
ValueKind::Float(value) => write!(f, "{}", value),
10796
ValueKind::Nil => write!(f, "nil"),
108-
109-
// TODO: Figure out a nice Display for these
110-
ValueKind::Table(ref table) => write!(f, "{:?}", table),
111-
ValueKind::Array(ref array) => write!(f, "{:?}", array),
97+
ValueKind::Table(ref table) => write!(f, "{{ {} }}", {
98+
table
99+
.iter()
100+
.map(|(k, v)| format!("{} => {}, ", k, v))
101+
.collect::<String>()
102+
}),
103+
ValueKind::Array(ref array) => write!(f, "{:?}", {
104+
array.iter().map(|e| format!("{}, ", e)).collect::<String>()
105+
}),
112106
}
113107
}
114108
}

0 commit comments

Comments
 (0)