diff options
| author | Mike Buland <mike@xagasoft.com> | 2026-06-14 07:09:59 -0700 |
|---|---|---|
| committer | Mike Buland <mike@xagasoft.com> | 2026-06-14 07:09:59 -0700 |
| commit | 68745e87a73ed7cfa305ee5319b85aeef85eb088 (patch) | |
| tree | 0a18f2543571dd095eacf06c4c7141b6e22093f1 | |
| parent | 038884232b6efbabb414cb011fa0fc4e3b4c8fd6 (diff) | |
| download | crimtag-68745e87a73ed7cfa305ee5319b85aeef85eb088.tar.gz crimtag-68745e87a73ed7cfa305ee5319b85aeef85eb088.tar.bz2 crimtag-68745e87a73ed7cfa305ee5319b85aeef85eb088.tar.xz crimtag-68745e87a73ed7cfa305ee5319b85aeef85eb088.zip | |
In progress...
| -rw-r--r-- | src/context.rs | 46 | ||||
| -rw-r--r-- | src/lib.rs | 2 |
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 | ||
| 7 | pub trait MappedStructure { | 7 | pub 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 | ||
| 12 | impl MappedStructure for Context { | 15 | impl 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 | ||
| 41 | pub struct StatefulContext<'a, T: MappedStructure> { | 78 | pub 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)] |
| @@ -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 | } |
