summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorMike Buland <mike@xagasoft.com>2026-06-11 10:43:24 -0700
committerMike Buland <mike@xagasoft.com>2026-06-11 10:43:24 -0700
commit92dbff3cda66a2a677f0c2306bb8508c64884445 (patch)
treee0118f3a4f45b03558b878864c37d33384d736c8 /src/lib.rs
parent887943e3f1908583d6888c8375f345b33b74fdb5 (diff)
downloadcrimtag-92dbff3cda66a2a677f0c2306bb8508c64884445.tar.gz
crimtag-92dbff3cda66a2a677f0c2306bb8508c64884445.tar.bz2
crimtag-92dbff3cda66a2a677f0c2306bb8508c64884445.tar.xz
crimtag-92dbff3cda66a2a677f0c2306bb8508c64884445.zip
Broke out error and renamed it.
I like the new name, now it could be for all sorts.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs27
1 files changed, 16 insertions, 11 deletions
diff --git a/src/lib.rs b/src/lib.rs
index feb00f0..9b838f3 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,8 +6,9 @@ use std::time::SystemTime;
6mod lexer; 6mod lexer;
7mod parser; 7mod parser;
8mod position; 8mod position;
9mod error;
9 10
10pub use parser::ParseError; 11pub use error::{CrimError,CrimResult};
11pub use position::*; 12pub use position::*;
12 13
13#[derive(Debug)] 14#[derive(Debug)]
@@ -38,7 +39,7 @@ struct View {
38} 39}
39 40
40impl View { 41impl View {
41 fn process(&self, input: &impl MappedStructure) -> Result<Context, ParseError> { 42 fn process(&self, input: &impl MappedStructure) -> Result<Context, CrimError> {
42 let mut out_vars = Context::new(); 43 let mut out_vars = Context::new();
43 for output in &self.outputs { 44 for output in &self.outputs {
44 let mut buf = String::new(); 45 let mut buf = String::new();
@@ -61,6 +62,9 @@ type Properties = HashMap<String, String>;
61enum Token { 62enum Token {
62 Loop(Identifier, Vec<Token>), 63 Loop(Identifier, Vec<Token>),
63 Show(Identifier,Properties), 64 Show(Identifier,Properties),
65 If(Identifier, Vec<Token>),
66 ElseIf(Identifier, Vec<Token>),
67 Else(Vec<Token>),
64 Text(String), 68 Text(String),
65} 69}
66 70
@@ -165,13 +169,13 @@ impl Crimtag {
165 Ok(()) 169 Ok(())
166 } 170 }
167 171
168 pub fn load_static(&mut self, data: &str) -> Result<(),ParseError> { 172 pub fn load_static(&mut self, data: &str) -> Result<(),CrimError> {
169 let source_id = self.id_source( &ViewSource::Static ); 173 let source_id = self.id_source( &ViewSource::Static );
170 let p = parser::Parser::new(); 174 let p = parser::Parser::new();
171 self.register_views( p.parse( &data, source_id )? ) 175 self.register_views( p.parse( &data, source_id )? )
172 } 176 }
173 177
174 pub fn load_external(&mut self, key: &str, data: &str) -> Result<(),ParseError> { 178 pub fn load_external(&mut self, key: &str, data: &str) -> Result<(),CrimError> {
175 let source_id = self.id_source( 179 let source_id = self.id_source(
176 &ViewSource::External{ key: key.to_string()} 180 &ViewSource::External{ key: key.to_string()}
177 ); 181 );
@@ -182,7 +186,7 @@ impl Crimtag {
182 ) 186 )
183 } 187 }
184 188
185 fn register_views(&mut self, views: Vec<View>) -> Result<(),ParseError> { 189 fn register_views(&mut self, views: Vec<View>) -> Result<(),CrimError> {
186 for view in views { 190 for view in views {
187 self.views.insert( view.name.clone(), view ); 191 self.views.insert( view.name.clone(), view );
188 } 192 }
@@ -201,15 +205,15 @@ impl Crimtag {
201 } 205 }
202 } 206 }
203 207
204 pub fn render_partial(&self, view: &str, input: &impl MappedStructure ) -> Result<Context,ParseError> { 208 pub fn render_partial(&self, view: &str, input: &impl MappedStructure ) -> Result<Context,CrimError> {
205 if let Some(view) = self.views.get(view) { 209 if let Some(view) = self.views.get(view) {
206 return view.process( input ) 210 return view.process( input )
207 } else { 211 } else {
208 return Err(ParseError::new(Position::new(0,0),"No such view found")); 212 return Err(CrimError::new(Position::none(),"No such view found".into()));
209 } 213 }
210 } 214 }
211 215
212 pub fn render(&self, view: &str, input: &impl MappedStructure ) -> Result<String,ParseError> { 216 pub fn render(&self, view: &str, input: &impl MappedStructure ) -> Result<String,CrimError> {
213 if let Some(view) = self.views.get( view ) { 217 if let Some(view) = self.views.get( view ) {
214 let mut output = view.process( input )?; 218 let mut output = view.process( input )?;
215 if let Some(l) = &view.layout { 219 if let Some(l) = &view.layout {
@@ -220,17 +224,17 @@ impl Crimtag {
220 224
221 Ok(s) 225 Ok(s)
222 } else { 226 } else {
223 Err(ParseError::new(Position::new(0,0),"No content found in root layout.")) 227 Err(CrimError::new(Position::none(), "No content found in root layout.".into()))
224 } 228 }
225 } 229 }
226 230
227 } else { 231 } else {
228 Err(ParseError::new(Position::new(0,0),"No such view found")) 232 Err(CrimError::new(Position::none(),"No such view found".into()))
229 } 233 }
230 } 234 }
231} 235}
232 236
233fn exec(input: &impl MappedStructure, tokens: &Vec<Token>, buf: &mut String) -> Result<(),ParseError> { 237fn exec(input: &impl MappedStructure, tokens: &Vec<Token>, buf: &mut String) -> Result<(),CrimError> {
234 for token in tokens { 238 for token in tokens {
235 match token { 239 match token {
236 Token::Loop(ident,code) => { 240 Token::Loop(ident,code) => {
@@ -279,6 +283,7 @@ fn exec(input: &impl MappedStructure, tokens: &Vec<Token>, buf: &mut String) ->
279 Token::Text(s) => { 283 Token::Text(s) => {
280 buf.push_str( s ); 284 buf.push_str( s );
281 } 285 }
286 _ => {}
282 } 287 }
283 } 288 }
284 Ok(()) 289 Ok(())