summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/context.rs46
-rw-r--r--src/lib.rs2
2 files changed, 46 insertions, 2 deletions
diff --git a/src/context.rs b/src/context.rs
index d322411..2f12ea9 100644
--- a/src/context.rs
+++ b/src/context.rs
@@ -6,7 +6,10 @@ pub type Context = HashMap<String,Value>;
6 6
7pub trait MappedStructure { 7pub trait MappedStructure {
8 fn get_value(&self, id: &[IdentifierValue]) -> Option<Value>; 8 fn get_value(&self, id: &[IdentifierValue]) -> Option<Value>;
9 9 fn get_iterable<T>(&self, id: &[IdentifierValue]) -> Option<T>
10 where
11 T: IntoIterator,
12 T::Item: MappedStructure;
10} 13}
11 14
12impl MappedStructure for Context { 15impl MappedStructure for Context {
@@ -36,6 +39,40 @@ impl MappedStructure for Context {
36 39
37 return cur_val.cloned(); 40 return cur_val.cloned();
38 } 41 }
42
43 fn get_iter<T>(&self, id: &[IdentifierValue]) -> Option<T>
44 where
45 T: IntoIterator,
46 T::Item: MappedStructure, {
47 if id.len() == 0 {
48 return None;
49 }
50 let mut cur_val : Option<&Value> = if let IdentifierValue::Name(s) = &id[0] {
51 self.get(s)
52 } else {
53 return None;
54 };
55 if cur_val.is_none() {
56 return None;
57 }
58
59 for idx in 1..id.len() {
60 if let IdentifierValue::Name(s) = &id[idx] &&
61 let Some(Value::Dictionary(d)) = &cur_val {
62 cur_val.replace( if let Some(v) = d.get(s) {
63 v
64 } else {
65 return None;
66 });
67 }
68 }
69
70 if let Some(Value::List(l)) = cur_val {
71 return l.into
72 }
73
74 return cur_val.cloned();
75 }
39} 76}
40 77
41pub struct StatefulContext<'a, T: MappedStructure> { 78pub struct StatefulContext<'a, T: MappedStructure> {
@@ -75,6 +112,13 @@ impl<'a, T: MappedStructure> MappedStructure for StatefulContext<'a,T> {
75 self.local.get_value( id ) 112 self.local.get_value( id )
76 } 113 }
77 } 114 }
115
116 fn get_iter<U>(&self, id: &[IdentifierValue]) -> Option<U>
117 where
118 U: IntoIterator,
119 U::Item: MappedStructure, {
120 None
121 }
78} 122}
79 123
80#[derive(Debug,Clone)] 124#[derive(Debug,Clone)]
diff --git a/src/lib.rs b/src/lib.rs
index 1ecd3b8..362e017 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -208,7 +208,7 @@ fn exec<T: MappedStructure>(input: &StatefulContext<T>, tokens: &Vec<Token>, buf
208 println!("Doesn't match."); 208 println!("Doesn't match.");
209 209
210 } 210 }
211 exec( &StatefulContext::full(input.root,d), code, buf )? 211 exec( &StatefulContext::root(d), code, buf )?
212 } 212 }
213 } 213 }
214 } 214 }