summaryrefslogtreecommitdiffstats
path: root/libimagentryfilter
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2016-02-02 15:53:49 +0100
committerMatthias Beyer <mail@beyermatthias.de>2016-02-03 16:39:04 +0100
commit0dc88877c6c27babaf2ece32e6a7145a40d17713 (patch)
tree017bee3b9387ec1882faacda2862a483e888ac0f /libimagentryfilter
parent012ca4a4278764b9f90587bb550e48e7ba184acd (diff)
Implement FieldPath::walk()
Diffstat (limited to 'libimagentryfilter')
-rw-r--r--libimagentryfilter/src/builtin/header/field_path/element.rs31
-rw-r--r--libimagentryfilter/src/builtin/header/field_path/mod.rs13
2 files changed, 41 insertions, 3 deletions
diff --git a/libimagentryfilter/src/builtin/header/field_path/element.rs b/libimagentryfilter/src/builtin/header/field_path/element.rs
index 044289aa..d6ad599a 100644
--- a/libimagentryfilter/src/builtin/header/field_path/element.rs
+++ b/libimagentryfilter/src/builtin/header/field_path/element.rs
@@ -1,11 +1,40 @@
use std::fmt::{Display, Formatter};
use std::fmt::Error as FmtError;
-#[derive(Debug)]
+use toml::Value;
+
+#[derive(Clone, Debug)]
pub struct FieldPathElement {
name: String
}
+impl FieldPathElement {
+
+ pub fn new(name: String) -> FieldPathElement {
+ FieldPathElement { name: name }
+ }
+
+ pub fn apply(&self, value: Value) -> Option<Value> {
+ use std::str::FromStr;
+ use std::ops::Index;
+
+ match value {
+ Value::Table(t) => {
+ t.get(&self.name).map(|a| a.clone())
+ },
+
+ Value::Array(a) => {
+ usize::from_str(&self.name[..])
+ .ok()
+ .and_then(|i| Some(a[i].clone()))
+ },
+
+ _ => None,
+ }
+ }
+
+}
+
impl Display for FieldPathElement {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
diff --git a/libimagentryfilter/src/builtin/header/field_path/mod.rs b/libimagentryfilter/src/builtin/header/field_path/mod.rs
index 0a8e61df..0760c596 100644
--- a/libimagentryfilter/src/builtin/header/field_path/mod.rs
+++ b/libimagentryfilter/src/builtin/header/field_path/mod.rs
@@ -20,7 +20,9 @@ pub struct FieldPath {
impl FieldPath {
pub fn new(elements: Vec<FieldPathElement>) -> FieldPath {
- unimplemented!()
+ FieldPath {
+ elements: elements,
+ }
}
pub fn compile(source: String) -> Result<FieldPath, FieldPathParsingError> {
@@ -28,7 +30,14 @@ impl FieldPath {
}
pub fn walk(&self, e: &EntryHeader) -> Option<Value> {
- unimplemented!()
+ let init_val : Value = Value::Table(e.toml().clone());
+
+ self.elements
+ .clone()
+ .into_iter()
+ .fold(Some(init_val), |acc: Option<Value>, token: FieldPathElement| {
+ acc.and_then(|element| token.apply(element))
+ })
}
}