summaryrefslogtreecommitdiff
path: root/src/context.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/context.rs')
-rw-r--r--src/context.rs136
1 files changed, 76 insertions, 60 deletions
diff --git a/src/context.rs b/src/context.rs
index 2f12ea9..83f8539 100644
--- a/src/context.rs
+++ b/src/context.rs
@@ -1,23 +1,21 @@
1use std::collections::HashMap; 1use std::collections::HashMap;
2use std::fmt;
2 3
3use crate::{Identifier,IdentifierValue}; 4use crate::{Identifier,IdentifierValue};
4 5
5pub type Context = HashMap<String,Value>; 6pub type Context = HashMap<String,Value>;
6 7
7pub trait MappedStructure { 8pub trait MappedStructure<'a> {
8 fn get_value(&self, id: &[IdentifierValue]) -> Option<Value>; 9 fn get_value(&'a self, id: &str) -> Option<MappedValue<'a>>;
9 fn get_iterable<T>(&self, id: &[IdentifierValue]) -> Option<T>
10 where
11 T: IntoIterator,
12 T::Item: MappedStructure;
13} 10}
14 11
15impl MappedStructure for Context { 12impl<'a> MappedStructure<'a> for Context {
16 fn get_value(&self, id: &[IdentifierValue]) -> Option<Value> { 13 fn get_value(&'a self, _id: &str) -> Option<MappedValue<'a>> {
14 /*
17 if id.len() == 0 { 15 if id.len() == 0 {
18 return None; 16 return None;
19 } 17 }
20 let mut cur_val : Option<&Value> = if let IdentifierValue::Name(s) = &id[0] { 18 let mut cur_val : Option<&MappedValue> = if let IdentifierValue::Name(s) = &id[0] {
21 self.get(s) 19 self.get(s)
22 } else { 20 } else {
23 return None; 21 return None;
@@ -28,7 +26,7 @@ impl MappedStructure for Context {
28 26
29 for idx in 1..id.len() { 27 for idx in 1..id.len() {
30 if let IdentifierValue::Name(s) = &id[idx] && 28 if let IdentifierValue::Name(s) = &id[idx] &&
31 let Some(Value::Dictionary(d)) = &cur_val { 29 let Some(MappedValue::Dictionary(d)) = &cur_val {
32 cur_val.replace( if let Some(v) = d.get(s) { 30 cur_val.replace( if let Some(v) = d.get(s) {
33 v 31 v
34 } else { 32 } else {
@@ -38,49 +36,64 @@ impl MappedStructure for Context {
38 } 36 }
39 37
40 return cur_val.cloned(); 38 return cur_val.cloned();
39 */
40
41 None
41 } 42 }
42 43}
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 44
59 for idx in 1..id.len() { 45pub trait MappedList<'a> {
60 if let IdentifierValue::Name(s) = &id[idx] && 46 fn get_index(&'a self, id: usize) -> Option<MappedValue<'a>>;
61 let Some(Value::Dictionary(d)) = &cur_val { 47}
62 cur_val.replace( if let Some(v) = d.get(s) {
63 v
64 } else {
65 return None;
66 });
67 }
68 }
69 48
70 if let Some(Value::List(l)) = cur_val { 49pub enum MappedValue<'a> {
71 return l.into 50 Struct(&'a dyn MappedStructure<'a>),
72 } 51 List(&'a dyn MappedList<'a>),
52
53 Str(&'a str),
54 String(&'a String),
55 Int8(i8),
56 Int16(i16),
57 Int32(i32),
58 Int64(i64),
59 UInt8(u8),
60 UInt16(u16),
61 UInt32(u32),
62 UInt64(u64),
63 Float32(f32),
64 Float64(f64),
65 Bool(bool),
66}
73 67
74 return cur_val.cloned(); 68impl<'a> fmt::Debug for MappedValue<'a> {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 match self {
71 MappedValue::<'a>::Struct(_) => write!(f, "MappedValue"),
72 MappedValue::<'a>::List(_) => write!(f, "MappedList"),
73 MappedValue::<'a>::Str(v) => write!(f, "{:?}", v),
74 MappedValue::<'a>::String(v) => write!(f, "{:?}", v),
75 MappedValue::<'a>::Int8(v) => write!(f, "{:?}", v),
76 MappedValue::<'a>::Int16(v) => write!(f, "{:?}", v),
77 MappedValue::<'a>::Int32(v) => write!(f, "{:?}", v),
78 MappedValue::<'a>::Int64(v) => write!(f, "{:?}", v),
79 MappedValue::<'a>::UInt8(v) => write!(f, "{:?}", v),
80 MappedValue::<'a>::UInt16(v) => write!(f, "{:?}", v),
81 MappedValue::<'a>::UInt32(v) => write!(f, "{:?}", v),
82 MappedValue::<'a>::UInt64(v) => write!(f, "{:?}", v),
83 MappedValue::<'a>::Float32(v) => write!(f, "{:?}", v),
84 MappedValue::<'a>::Float64(v) => write!(f, "{:?}", v),
85 MappedValue::<'a>::Bool(v) => write!(f, "{:?}", v),
86 }
75 } 87 }
88
76} 89}
77 90
78pub struct StatefulContext<'a, T: MappedStructure> { 91pub struct StatefulContext<'a, T: MappedStructure<'a>> {
79 pub root: &'a T, 92 root: &'a T,
80 local: &'a T, 93 local: &'a T,
81} 94}
82 95
83impl<'a, T: MappedStructure> StatefulContext<'a, T> { 96impl<'a, T: MappedStructure<'a>> StatefulContext<'a, T> {
84 pub fn root( root: &'a T ) -> Self { 97 pub fn root( root: &'a T ) -> Self {
85 Self { 98 Self {
86 root, local: root, 99 root, local: root,
@@ -99,25 +112,28 @@ impl<'a, T: MappedStructure> StatefulContext<'a, T> {
99 root, local 112 root, local
100 } 113 }
101 } 114 }
102}
103 115
104impl<'a, T: MappedStructure> MappedStructure for StatefulContext<'a,T> { 116 pub fn get_value(&self, id: &[IdentifierValue]) -> Option<MappedValue<'a>> {
105 fn get_value(&self, id: &[IdentifierValue]) -> Option<Value> { 117 let mut value : Option<MappedValue> = Some(MappedValue::Struct(self.root));
106 if id.is_empty() { 118 for idpart in id {
107 return None; 119 match idpart {
108 } 120 IdentifierValue::Root => {
109 if id[0] == IdentifierValue::Root { 121 return None
110 self.root.get_value( &id[1..] ) 122 }
111 } else { 123 IdentifierValue::Name(name) => {
112 self.local.get_value( id ) 124 if let Some(MappedValue::Struct(s)) = value &&
125 let Some(nv) = s.get_value( &name ) {
126 value.replace( nv );
127 } else {
128 return None;
129 }
130 }
131 IdentifierValue::Index(_) => {
132 return None;
133 }
134 }
113 } 135 }
114 } 136 value
115
116 fn get_iter<U>(&self, id: &[IdentifierValue]) -> Option<U>
117 where
118 U: IntoIterator,
119 U::Item: MappedStructure, {
120 None
121 } 137 }
122} 138}
123 139