Explorar o código

return an error when a expression could not be found

Evan Almloff %!s(int64=3) %!d(string=hai) anos
pai
achega
c94cf9c8e2
Modificáronse 1 ficheiros con 24 adicións e 17 borrados
  1. 24 17
      packages/rsx_interperter/src/interperter.rs

+ 24 - 17
packages/rsx_interperter/src/interperter.rs

@@ -11,7 +11,7 @@ use crate::error::{Error, RecompileReason};
 struct InterpertedIfmt(IfmtInput);
 
 impl InterpertedIfmt {
-    fn resolve(&self, captured: &IfmtArgs) -> String {
+    fn resolve(&self, captured: &IfmtArgs) -> Result<String, Error> {
         let mut result = String::new();
         for seg in &self.0.segments {
             match seg {
@@ -20,24 +20,31 @@ impl InterpertedIfmt {
                     format_args,
                 } => {
                     let expr = segment.to_token_stream();
-                    let expr_str = expr.to_string();
                     let expr: Expr = parse2(expr).unwrap();
-                    let formatted = captured
-                        .named_args
-                        .iter()
-                        .find(|fmted| {
-                            parse_str::<Expr>(fmted.expr).unwrap() == expr
-                                && fmted.format_args == format_args
-                        })
-                        .expect(
-                            format!("could not resolve {{{}:{}}}", expr_str, format_args).as_str(),
-                        );
-                    result.push_str(&formatted.result);
+                    let search = captured.named_args.iter().find(|fmted| {
+                        parse_str::<Expr>(fmted.expr).unwrap() == expr
+                            && fmted.format_args == format_args
+                    });
+                    match search {
+                        Some(formatted) => {
+                            result.push_str(&formatted.result);
+                        }
+                        None => {
+                            let expr_str = segment.to_token_stream().to_string();
+                            println!("{}", expr_str);
+                            return Err(Error::RecompileRequiredError(
+                                RecompileReason::CapturedExpression(format!(
+                                    "could not resolve {{{}:{}}}",
+                                    expr_str, format_args
+                                )),
+                            ));
+                        }
+                    }
                 }
                 Segment::Literal(lit) => result.push_str(lit),
             }
         }
-        result
+        Ok(result)
     }
 }
 
@@ -64,7 +71,7 @@ fn build_node<'a>(
             let ifmt = InterpertedIfmt(
                 IfmtInput::from_str(&text.value()).map_err(|err| Error::ParseError(err))?,
             );
-            let text = bump.alloc(ifmt.resolve(&ctx.captured));
+            let text = bump.alloc(ifmt.resolve(&ctx.captured)?);
             Ok(factory.text(format_args!("{}", text)))
         }
         BodyNode::Element(el) => {
@@ -91,7 +98,7 @@ fn build_node<'a>(
                         };
 
                         if let Some((name, namespace)) = attrbute_to_static_str(&name) {
-                            let value = bump.alloc(value.resolve(&ctx.captured));
+                            let value = bump.alloc(value.resolve(&ctx.captured)?);
                             attributes.push(Attribute {
                                 name,
                                 value: AttributeValue::Text(value),
@@ -187,7 +194,7 @@ fn build_node<'a>(
                         let ifmt: InterpertedIfmt = InterpertedIfmt(
                             parse_str(&lit.value()).map_err(|err| Error::ParseError(err))?,
                         );
-                        let key = bump.alloc(ifmt.resolve(&ctx.captured));
+                        let key = bump.alloc(ifmt.resolve(&ctx.captured)?);
 
                         Ok(factory.raw_element(
                             tag,