diff options
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 98 |
1 files changed, 28 insertions, 70 deletions
| @@ -30,6 +30,7 @@ pub enum ViewSource { | |||
| 30 | 30 | ||
| 31 | #[derive(Debug)] | 31 | #[derive(Debug)] |
| 32 | struct View { | 32 | struct View { |
| 33 | name: String, | ||
| 33 | source: usize, | 34 | source: usize, |
| 34 | outputs: Vec<Output>, | 35 | outputs: Vec<Output>, |
| 35 | // theme: Option<String> | 36 | // theme: Option<String> |
| @@ -40,11 +41,9 @@ impl View { | |||
| 40 | fn process(&self, input: &Value) -> Result<Value, ParseError> { | 41 | fn process(&self, input: &Value) -> Result<Value, ParseError> { |
| 41 | let mut out_vars = HashMap::new(); | 42 | let mut out_vars = HashMap::new(); |
| 42 | for output in &self.outputs { | 43 | for output in &self.outputs { |
| 43 | if let Token::Output(_,/*_,*/tokens) = &output.ast_root { | 44 | let mut buf = String::new(); |
| 44 | let mut buf = String::new(); | 45 | exec( input, &output.code, &mut buf )?; |
| 45 | exec( input, tokens, &mut buf )?; | 46 | out_vars.insert( output.name.clone(), Value::String(buf) ); |
| 46 | out_vars.insert( output.name.clone(), Value::String(buf) ); | ||
| 47 | } | ||
| 48 | } | 47 | } |
| 49 | Ok(Value::Dictionary(out_vars)) | 48 | Ok(Value::Dictionary(out_vars)) |
| 50 | } | 49 | } |
| @@ -53,16 +52,13 @@ impl View { | |||
| 53 | #[derive(Debug)] | 52 | #[derive(Debug)] |
| 54 | struct Output { | 53 | struct Output { |
| 55 | name: String, | 54 | name: String, |
| 56 | ast_root: Token, | 55 | code: Vec<Token>, |
| 57 | } | 56 | } |
| 58 | 57 | ||
| 59 | type Properties = HashMap<String, String>; | 58 | type Properties = HashMap<String, String>; |
| 60 | 59 | ||
| 61 | #[derive(Debug)] | 60 | #[derive(Debug)] |
| 62 | enum Token { | 61 | enum Token { |
| 63 | Root(Vec<Token>), | ||
| 64 | View(String,Properties,Vec<Token>), | ||
| 65 | Output(String,/*Properties,*/ Vec<Token>), | ||
| 66 | Show(String,Properties), | 62 | Show(String,Properties), |
| 67 | Text(String), | 63 | Text(String), |
| 68 | } | 64 | } |
| @@ -109,78 +105,44 @@ impl Crimtag { | |||
| 109 | } else { | 105 | } else { |
| 110 | SystemTime::now() | 106 | SystemTime::now() |
| 111 | }; | 107 | }; |
| 112 | 108 | let source_id = self.id_source( | |
| 109 | &ViewSource::File { | ||
| 110 | path: path.to_path_buf(), | ||
| 111 | loaded: time, | ||
| 112 | } | ||
| 113 | ); | ||
| 113 | let p = parser::Parser::new(); | 114 | let p = parser::Parser::new(); |
| 114 | self.register_views( | 115 | self.register_views( |
| 115 | p.parse( &String::from_utf8( std::fs::read( path )? )? )?, | 116 | p.parse( |
| 116 | ViewSource::File { | 117 | &String::from_utf8( std::fs::read( path )? )?, |
| 117 | path: path.to_path_buf(), | 118 | source_id |
| 118 | loaded: time, | 119 | )?, |
| 119 | })?; | 120 | )?; |
| 120 | Ok(()) | 121 | Ok(()) |
| 121 | } | 122 | } |
| 122 | 123 | ||
| 123 | pub fn load_static(&mut self, data: &str) -> Result<(),ParseError> { | 124 | pub fn load_static(&mut self, data: &str) -> Result<(),ParseError> { |
| 125 | let source_id = self.id_source( &ViewSource::Static ); | ||
| 124 | let p = parser::Parser::new(); | 126 | let p = parser::Parser::new(); |
| 125 | self.register_views( p.parse( &data )?, ViewSource::Static ) | 127 | self.register_views( p.parse( &data, source_id )? ) |
| 126 | } | 128 | } |
| 127 | 129 | ||
| 128 | pub fn load_external(&mut self, key: &str, data: &str) -> Result<(),ParseError> { | 130 | pub fn load_external(&mut self, key: &str, data: &str) -> Result<(),ParseError> { |
| 129 | let p = parser::Parser::new(); | 131 | let source_id = self.id_source( |
| 132 | &ViewSource::External{ key: key.to_string()} | ||
| 133 | ); | ||
| 134 | |||
| 135 | let p = parser::Parser::new(); | ||
| 130 | self.register_views( | 136 | self.register_views( |
| 131 | p.parse( &data )?, | 137 | p.parse( &data, source_id )?, |
| 132 | ViewSource::External{ key: key.to_string()} | ||
| 133 | ) | 138 | ) |
| 134 | } | 139 | } |
| 135 | 140 | ||
| 136 | fn register_views(&mut self, token: Token, source: ViewSource) -> Result<(),ParseError> { | 141 | fn register_views(&mut self, views: Vec<View>) -> Result<(),ParseError> { |
| 137 | let source_id = self.id_source( &source ); | 142 | for view in views { |
| 138 | if let Token::Root(views) = token { | 143 | self.views.insert( view.name.clone(), view ); |
| 139 | for token in views { | ||
| 140 | if let Token::View(name,props,output_tokens) = token { | ||
| 141 | let mut outputs = Vec::new(); | ||
| 142 | for ot in output_tokens { | ||
| 143 | let output_name = if let Token::Output(on,..) = &ot { | ||
| 144 | on.clone() | ||
| 145 | } else { | ||
| 146 | return Err(ParseError::new( | ||
| 147 | Position::new(0,0), | ||
| 148 | "Broken parser? Item in view is not an output." | ||
| 149 | )); | ||
| 150 | }; | ||
| 151 | outputs.push( | ||
| 152 | Output { | ||
| 153 | name: output_name, | ||
| 154 | ast_root: ot, | ||
| 155 | } | ||
| 156 | ); | ||
| 157 | } | ||
| 158 | |||
| 159 | let layout = if let Some(l) = props.get("layout") { | ||
| 160 | Some(l.clone()) | ||
| 161 | } else { | ||
| 162 | None | ||
| 163 | }; | ||
| 164 | |||
| 165 | self.views.insert( name, View { | ||
| 166 | source: source_id, | ||
| 167 | outputs: outputs, | ||
| 168 | layout: layout, | ||
| 169 | }); | ||
| 170 | } else { | ||
| 171 | return Err(ParseError::new( | ||
| 172 | Position::new(0,0), | ||
| 173 | "Broken parser? Item in root is not a view." | ||
| 174 | )); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | Ok(()) | ||
| 178 | } else { | ||
| 179 | Err(ParseError::new( | ||
| 180 | Position::new(0,0), | ||
| 181 | "Broken parser? Root is not a root token." | ||
| 182 | )) | ||
| 183 | } | 144 | } |
| 145 | Ok(()) | ||
| 184 | } | 146 | } |
| 185 | 147 | ||
| 186 | pub fn get_view_source(&self, view: &str) -> Option<ViewSource> { | 148 | pub fn get_view_source(&self, view: &str) -> Option<ViewSource> { |
| @@ -228,10 +190,6 @@ impl Crimtag { | |||
| 228 | fn exec(input: &Value, tokens: &Vec<Token>, buf: &mut String) -> Result<(),ParseError> { | 190 | fn exec(input: &Value, tokens: &Vec<Token>, buf: &mut String) -> Result<(),ParseError> { |
| 229 | for token in tokens { | 191 | for token in tokens { |
| 230 | match token { | 192 | match token { |
| 231 | Token::Root(..) | Token::View(..) | Token::Output(..) => { | ||
| 232 | println!("Too high!?"); | ||
| 233 | // Error? | ||
| 234 | } | ||
| 235 | Token::Show(s,p) => { | 193 | Token::Show(s,p) => { |
| 236 | let format = if let Some(s) = p.get("format") { | 194 | let format = if let Some(s) = p.get("format") { |
| 237 | s | 195 | s |
