diff options
Diffstat (limited to 'src/context.rs')
| -rw-r--r-- | src/context.rs | 91 |
1 files changed, 51 insertions, 40 deletions
diff --git a/src/context.rs b/src/context.rs index 4de5b45..4501a0a 100644 --- a/src/context.rs +++ b/src/context.rs | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | use std::collections::HashMap; | 1 | use std::collections::HashMap; |
| 2 | use std::fmt; | 2 | use std::fmt; |
| 3 | 3 | ||
| 4 | use crate::error::CrimResult; | ||
| 5 | |||
| 4 | use crate::{Identifier,IdentifierValue}; | 6 | use crate::{Identifier,IdentifierValue}; |
| 5 | 7 | ||
| 6 | pub type Context = HashMap<String,Value>; | 8 | pub type Context = HashMap<String,Value>; |
| @@ -10,42 +12,32 @@ pub trait MappedStructure<'a> { | |||
| 10 | } | 12 | } |
| 11 | 13 | ||
| 12 | impl<'a> MappedStructure<'a> for Context { | 14 | impl<'a> MappedStructure<'a> for Context { |
| 13 | fn get_value(&'a self, _id: &str) -> Option<MappedValue<'a>> { | 15 | fn get_value(&'a self, id: &str) -> Option<MappedValue<'a>> { |
| 14 | /* | 16 | self.get(id).map(|v|Into::<MappedValue<'a>>::into(v)) |
| 15 | if id.len() == 0 { | ||
| 16 | return None; | ||
| 17 | } | ||
| 18 | let mut cur_val : Option<&MappedValue> = if let IdentifierValue::Name(s) = &id[0] { | ||
| 19 | self.get(s) | ||
| 20 | } else { | ||
| 21 | return None; | ||
| 22 | }; | ||
| 23 | if cur_val.is_none() { | ||
| 24 | return None; | ||
| 25 | } | ||
| 26 | |||
| 27 | for idx in 1..id.len() { | ||
| 28 | if let IdentifierValue::Name(s) = &id[idx] && | ||
| 29 | let Some(MappedValue::Dictionary(d)) = &cur_val { | ||
| 30 | cur_val.replace( if let Some(v) = d.get(s) { | ||
| 31 | v | ||
| 32 | } else { | ||
| 33 | return None; | ||
| 34 | }); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | return cur_val.cloned(); | ||
| 39 | */ | ||
| 40 | |||
| 41 | None | ||
| 42 | } | 17 | } |
| 43 | } | 18 | } |
| 44 | 19 | ||
| 45 | pub trait MappedList<'a> { | 20 | pub trait MappedList<'a> { |
| 46 | fn get_index(&'a self, id: usize) -> Option<MappedValue<'a>>; | 21 | fn get_index(&'a self, id: usize) -> Option<MappedValue<'a>>; |
| 47 | // fn get_iterator(&'a self) -> MappedListIterator<'a>; | 22 | fn for_each(&'a self, func: &dyn Fn(&MappedValue<'a>, &mut String) -> CrimResult<()>) -> CrimResult<String>; |
| 48 | fn for_each(&'a self, func: &dyn Fn(&MappedValue<'a>)); | 23 | } |
| 24 | |||
| 25 | impl<'a> MappedList<'a> for Vec<Value> { | ||
| 26 | fn get_index(&'a self, id: usize) -> Option<MappedValue<'a>> { | ||
| 27 | if let Some(x) = self.get(id) { | ||
| 28 | Some(x.into()) | ||
| 29 | } else { | ||
| 30 | None | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | fn for_each(&'a self, func: &dyn Fn(&MappedValue<'a>, &mut String) -> CrimResult<()>) -> CrimResult<String> { | ||
| 35 | let mut buf = String::new(); | ||
| 36 | for i in self { | ||
| 37 | func( &Into::<MappedValue<'a>>::into( i ), &mut buf )?; | ||
| 38 | } | ||
| 39 | Ok(buf) | ||
| 40 | } | ||
| 49 | } | 41 | } |
| 50 | 42 | ||
| 51 | pub enum MappedValue<'a> { | 43 | pub enum MappedValue<'a> { |
| @@ -90,37 +82,42 @@ impl<'a> fmt::Debug for MappedValue<'a> { | |||
| 90 | 82 | ||
| 91 | } | 83 | } |
| 92 | 84 | ||
| 93 | pub struct StatefulContext<'a, T: MappedStructure<'a>> { | 85 | pub struct StatefulContext<'a> { |
| 94 | root: &'a T, | 86 | root: &'a dyn MappedStructure<'a>, |
| 95 | local: &'a T, | 87 | local: &'a dyn MappedStructure<'a>, |
| 96 | } | 88 | } |
| 97 | 89 | ||
| 98 | impl<'a, T: MappedStructure<'a>> StatefulContext<'a, T> { | 90 | impl<'a> StatefulContext<'a> { |
| 99 | pub fn root( root: &'a T ) -> Self { | 91 | pub fn root( root: &'a dyn MappedStructure<'a> ) -> Self { |
| 100 | Self { | 92 | Self { |
| 101 | root, local: root, | 93 | root, local: root, |
| 102 | } | 94 | } |
| 103 | } | 95 | } |
| 104 | 96 | ||
| 105 | pub fn local(&self, local: &'a T ) -> Self { | 97 | pub fn local(&self, local: &'a dyn MappedStructure<'a> ) -> Self { |
| 106 | Self { | 98 | Self { |
| 107 | root: self.root, | 99 | root: self.root, |
| 108 | local | 100 | local |
| 109 | } | 101 | } |
| 110 | } | 102 | } |
| 111 | 103 | ||
| 112 | pub fn full( root: &'a T, local: &'a T ) -> Self { | 104 | pub fn full( root: &'a dyn MappedStructure<'a>, local: &'a dyn MappedStructure<'a> ) -> Self { |
| 113 | Self { | 105 | Self { |
| 114 | root, local | 106 | root, local |
| 115 | } | 107 | } |
| 116 | } | 108 | } |
| 117 | 109 | ||
| 118 | pub fn get_value(&self, id: &[IdentifierValue]) -> Option<MappedValue<'a>> { | 110 | pub fn get_value(&self, id: &[IdentifierValue]) -> Option<MappedValue<'a>> { |
| 119 | let mut value : Option<MappedValue> = Some(MappedValue::Struct(self.root)); | 111 | let mut value : Option<MappedValue> = |
| 112 | if id[0] == IdentifierValue::Root { | ||
| 113 | Some(MappedValue::Struct(self.root)) | ||
| 114 | } else { | ||
| 115 | Some(MappedValue::Struct(self.local)) | ||
| 116 | }; | ||
| 120 | for idpart in id { | 117 | for idpart in id { |
| 121 | match idpart { | 118 | match idpart { |
| 122 | IdentifierValue::Root => { | 119 | IdentifierValue::Root => { |
| 123 | return None | 120 | continue; |
| 124 | } | 121 | } |
| 125 | IdentifierValue::Name(name) => { | 122 | IdentifierValue::Name(name) => { |
| 126 | if let Some(MappedValue::Struct(s)) = value && | 123 | if let Some(MappedValue::Struct(s)) = value && |
| @@ -139,6 +136,20 @@ impl<'a, T: MappedStructure<'a>> StatefulContext<'a, T> { | |||
| 139 | } | 136 | } |
| 140 | } | 137 | } |
| 141 | 138 | ||
| 139 | impl<'a> From<&'a Value> for MappedValue<'a> { | ||
| 140 | fn from(value: &'a Value) -> Self { | ||
| 141 | match value { | ||
| 142 | Value::Dictionary(ctx) => MappedValue::Struct(ctx), | ||
| 143 | Value::List(list) => MappedValue::List(list), | ||
| 144 | Value::String(s) => MappedValue::String(s), | ||
| 145 | Value::Int(i) => MappedValue::Int64(*i), | ||
| 146 | Value::Float(f) => MappedValue::Float64(*f), | ||
| 147 | Value::Bool(b) => MappedValue::Bool(*b), | ||
| 148 | Value::Identifier(_identifier) => panic!("Identifier!?"), | ||
| 149 | } | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 142 | #[derive(Debug,Clone)] | 153 | #[derive(Debug,Clone)] |
| 143 | pub enum Value { | 154 | pub enum Value { |
| 144 | Dictionary(Context), | 155 | Dictionary(Context), |
