diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/context.rs | 27 | ||||
| -rw-r--r-- | src/lib.rs | 8 |
2 files changed, 30 insertions, 5 deletions
diff --git a/src/context.rs b/src/context.rs index 4501a0a..0b51677 100644 --- a/src/context.rs +++ b/src/context.rs | |||
| @@ -40,6 +40,33 @@ impl<'a> MappedList<'a> for Vec<Value> { | |||
| 40 | } | 40 | } |
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | impl<'a> MappedList<'a> for Vec<MappedValue<'a>> { | ||
| 44 | fn get_index(&'a self, id: usize) -> Option<MappedValue<'a>> { | ||
| 45 | if let Some(x) = self.get(id) { | ||
| 46 | Some(*x) | ||
| 47 | } else { | ||
| 48 | None | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | fn for_each(&'a self, func: &dyn Fn(&MappedValue<'a>, &mut String) -> CrimResult<()>) -> CrimResult<String> { | ||
| 53 | let mut buf = String::new(); | ||
| 54 | for i in self { | ||
| 55 | func( i, &mut buf )?; | ||
| 56 | } | ||
| 57 | Ok(buf) | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | pub type MappedHash<'a> = HashMap<String, MappedValue<'a>>; | ||
| 62 | |||
| 63 | impl<'a> MappedStructure<'a> for MappedHash<'a> { | ||
| 64 | fn get_value(&'a self, id: &str) -> Option<MappedValue<'a>> { | ||
| 65 | self.get(id).copied() | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | #[derive(Copy,Clone)] | ||
| 43 | pub enum MappedValue<'a> { | 70 | pub enum MappedValue<'a> { |
| 44 | Struct(&'a dyn MappedStructure<'a>), | 71 | Struct(&'a dyn MappedStructure<'a>), |
| 45 | List(&'a dyn MappedList<'a>), | 72 | List(&'a dyn MappedList<'a>), |
| @@ -7,7 +7,7 @@ mod lexer; | |||
| 7 | mod parser; | 7 | mod parser; |
| 8 | mod position; | 8 | mod position; |
| 9 | mod error; | 9 | mod error; |
| 10 | mod context; | 10 | pub mod context; |
| 11 | 11 | ||
| 12 | pub use error::{CrimError,CrimResult}; | 12 | pub use error::{CrimError,CrimResult}; |
| 13 | pub use position::*; | 13 | pub use position::*; |
| @@ -213,10 +213,8 @@ fn exec<'a>(input: &StatefulContext<'a>, tokens: &Vec<Token>, buf: &mut String) | |||
| 213 | } | 213 | } |
| 214 | Token::If(ident,code,other) => { | 214 | Token::If(ident,code,other) => { |
| 215 | if let Some(value) = input.get_value( &ident ) && | 215 | if let Some(value) = input.get_value( &ident ) && |
| 216 | let MappedValue::Bool(b) = value && | 216 | let MappedValue::Bool(b) = value && b { |
| 217 | b { | 217 | exec(input, code, buf)? |
| 218 | |||
| 219 | exec(input, code, buf)? | ||
| 220 | } else { | 218 | } else { |
| 221 | exec(input, other, buf)? | 219 | exec(input, other, buf)? |
| 222 | } | 220 | } |
