diff options
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 18 |
1 files changed, 10 insertions, 8 deletions
| @@ -11,7 +11,7 @@ mod context; | |||
| 11 | 11 | ||
| 12 | pub use error::{CrimError,CrimResult}; | 12 | pub use error::{CrimError,CrimResult}; |
| 13 | pub use position::*; | 13 | pub use position::*; |
| 14 | pub use context::{Context,Value,MappedStructure}; | 14 | pub use context::{Context,Value,MappedStructure,StatefulContext}; |
| 15 | 15 | ||
| 16 | #[derive(Debug)] | 16 | #[derive(Debug)] |
| 17 | pub struct Crimtag { | 17 | pub struct Crimtag { |
| @@ -41,11 +41,12 @@ struct View { | |||
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | impl View { | 43 | impl View { |
| 44 | fn process(&self, input: &impl MappedStructure) -> CrimResult<Context> { | 44 | fn process<T: MappedStructure>(&self, input: &T) -> CrimResult<Context> { |
| 45 | let mut out_vars = Context::new(); | 45 | let mut out_vars = Context::new(); |
| 46 | for output in &self.outputs { | 46 | for output in &self.outputs { |
| 47 | let mut buf = String::new(); | 47 | let mut buf = String::new(); |
| 48 | exec( input, &output.code, &mut buf )?; | 48 | let sc = StatefulContext::root( input ); |
| 49 | exec( &sc, &output.code, &mut buf )?; | ||
| 49 | out_vars.insert( output.name.clone(), Value::String(buf) ); | 50 | out_vars.insert( output.name.clone(), Value::String(buf) ); |
| 50 | } | 51 | } |
| 51 | Ok(out_vars) | 52 | Ok(out_vars) |
| @@ -68,8 +69,9 @@ enum Token { | |||
| 68 | Text(String), | 69 | Text(String), |
| 69 | } | 70 | } |
| 70 | 71 | ||
| 71 | #[derive(Debug,Clone)] | 72 | #[derive(Debug,Clone,PartialEq)] |
| 72 | pub enum IdentifierValue { | 73 | pub enum IdentifierValue { |
| 74 | Root, | ||
| 73 | Name(String), | 75 | Name(String), |
| 74 | Index(usize), | 76 | Index(usize), |
| 75 | } | 77 | } |
| @@ -161,7 +163,7 @@ impl Crimtag { | |||
| 161 | } | 163 | } |
| 162 | } | 164 | } |
| 163 | 165 | ||
| 164 | pub fn render_partial(&self, view: &str, input: &impl MappedStructure ) -> CrimResult<Context> { | 166 | pub fn render_partial<T: MappedStructure>(&self, view: &str, input: &T ) -> CrimResult<Context> { |
| 165 | if let Some(view) = self.views.get(view) { | 167 | if let Some(view) = self.views.get(view) { |
| 166 | return view.process( input ) | 168 | return view.process( input ) |
| 167 | } else { | 169 | } else { |
| @@ -169,7 +171,7 @@ impl Crimtag { | |||
| 169 | } | 171 | } |
| 170 | } | 172 | } |
| 171 | 173 | ||
| 172 | pub fn render(&self, view: &str, input: &impl MappedStructure ) -> CrimResult<String> { | 174 | pub fn render<T: MappedStructure>(&self, view: &str, input: &T ) -> CrimResult<String> { |
| 173 | if let Some(view) = self.views.get( view ) { | 175 | if let Some(view) = self.views.get( view ) { |
| 174 | //println!("::Token tree::\n{:?}", view ); | 176 | //println!("::Token tree::\n{:?}", view ); |
| 175 | let mut output = view.process( input )?; | 177 | let mut output = view.process( input )?; |
| @@ -191,7 +193,7 @@ impl Crimtag { | |||
| 191 | } | 193 | } |
| 192 | } | 194 | } |
| 193 | 195 | ||
| 194 | fn exec(input: &impl MappedStructure, tokens: &Vec<Token>, buf: &mut String) -> CrimResult<()> { | 196 | fn exec<T: MappedStructure>(input: &StatefulContext<T>, tokens: &Vec<Token>, buf: &mut String) -> CrimResult<()> { |
| 195 | for token in tokens { | 197 | for token in tokens { |
| 196 | match token { | 198 | match token { |
| 197 | Token::Loop(ident,code) => { | 199 | Token::Loop(ident,code) => { |
| @@ -199,7 +201,7 @@ fn exec(input: &impl MappedStructure, tokens: &Vec<Token>, buf: &mut String) -> | |||
| 199 | let Value::List(list) = value { | 201 | let Value::List(list) = value { |
| 200 | for rec in &list { | 202 | for rec in &list { |
| 201 | if let Value::Dictionary(d) = rec { | 203 | if let Value::Dictionary(d) = rec { |
| 202 | exec( d, code, buf )? | 204 | exec( &input.local(d), code, buf )? |
| 203 | } | 205 | } |
| 204 | } | 206 | } |
| 205 | } | 207 | } |
