diff options
Diffstat (limited to 'src/context.rs')
| -rw-r--r-- | src/context.rs | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/src/context.rs b/src/context.rs index e4cd5c2..7484a1f 100644 --- a/src/context.rs +++ b/src/context.rs | |||
| @@ -5,11 +5,12 @@ use crate::{Identifier,IdentifierValue}; | |||
| 5 | pub type Context = HashMap<String,Value>; | 5 | pub type Context = HashMap<String,Value>; |
| 6 | 6 | ||
| 7 | pub trait MappedStructure { | 7 | pub trait MappedStructure { |
| 8 | fn get_value(&self, id: &Identifier) -> Option<Value>; | 8 | fn get_value(&self, id: &[IdentifierValue]) -> Option<Value>; |
| 9 | |||
| 9 | } | 10 | } |
| 10 | 11 | ||
| 11 | impl MappedStructure for Context { | 12 | impl MappedStructure for Context { |
| 12 | fn get_value(&self, id: &Identifier) -> Option<Value> { | 13 | fn get_value(&self, id: &[IdentifierValue]) -> Option<Value> { |
| 13 | if id.len() == 0 { | 14 | if id.len() == 0 { |
| 14 | return None; | 15 | return None; |
| 15 | } | 16 | } |
| @@ -37,6 +38,39 @@ impl MappedStructure for Context { | |||
| 37 | } | 38 | } |
| 38 | } | 39 | } |
| 39 | 40 | ||
| 41 | pub struct StatefulContext<'a, T: MappedStructure> { | ||
| 42 | root: &'a T, | ||
| 43 | current: &'a T, | ||
| 44 | } | ||
| 45 | |||
| 46 | impl<'a, T: MappedStructure> StatefulContext<'a, T> { | ||
| 47 | pub fn root( root: &'a T ) -> Self { | ||
| 48 | Self { | ||
| 49 | root, current: root, | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | pub fn local(&self, current: &'a T ) -> Self { | ||
| 54 | Self { | ||
| 55 | root: self.root, | ||
| 56 | current | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | impl<'a, T: MappedStructure> MappedStructure for StatefulContext<'a,T> { | ||
| 62 | fn get_value(&self, id: &[IdentifierValue]) -> Option<Value> { | ||
| 63 | if id.is_empty() { | ||
| 64 | return None; | ||
| 65 | } | ||
| 66 | if id[0] == IdentifierValue::Root { | ||
| 67 | self.root.get_value( &id[1..] ) | ||
| 68 | } else { | ||
| 69 | self.current.get_value( id ) | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 40 | #[derive(Debug,Clone)] | 74 | #[derive(Debug,Clone)] |
| 41 | pub enum Value { | 75 | pub enum Value { |
| 42 | Dictionary(Context), | 76 | Dictionary(Context), |
