return an error when a expression could not be found

This commit is contained in:
Evan Almloff committed 2022-05-31 08:29:30 -05:00
1 parent 94448ea4aa
commit c94cf9c8e2
1 file changed
+24 -17
+24 -17
View File
@@ -11,7 +11,7 @@ use crate::error::{Error, RecompileReason};
struct InterpertedIfmt(IfmtInput); struct InterpertedIfmt(IfmtInput);
impl InterpertedIfmt { impl InterpertedIfmt {
fn resolve(&self, captured: &IfmtArgs) -> String { fn resolve(&self, captured: &IfmtArgs) -> Result<String, Error> {
let mut result = String::new(); let mut result = String::new();
for seg in &self.0.segments { for seg in &self.0.segments {
match seg { match seg {
@@ -20,24 +20,31 @@ impl InterpertedIfmt {
format_args, format_args,
} => { } => {
let expr = segment.to_token_stream(); let expr = segment.to_token_stream();
let expr_str = expr.to_string();
let expr: Expr = parse2(expr).unwrap(); let expr: Expr = parse2(expr).unwrap();
let formatted = captured let search = captured.named_args.iter().find(|fmted| {
.named_args parse_str::<Expr>(fmted.expr).unwrap() == expr
.iter() && fmted.format_args == format_args
.find(|fmted| { });
parse_str::<Expr>(fmted.expr).unwrap() == expr match search {
&& fmted.format_args == format_args Some(formatted) => {
}) result.push_str(&formatted.result);
.expect( }
format!("could not resolve {{{}:{}}}", expr_str, format_args).as_str(), None => {
); let expr_str = segment.to_token_stream().to_string();
result.push_str(&formatted.result); println!("{}", expr_str);
return Err(Error::RecompileRequiredError(
RecompileReason::CapturedExpression(format!(
"could not resolve {{{}:{}}}",
expr_str, format_args
)),
));
}
}
} }
Segment::Literal(lit) => result.push_str(lit), Segment::Literal(lit) => result.push_str(lit),
} }
} }
result Ok(result)
} }
} }
@@ -64,7 +71,7 @@ fn build_node<'a>(
let ifmt = InterpertedIfmt( let ifmt = InterpertedIfmt(
IfmtInput::from_str(&text.value()).map_err(|err| Error::ParseError(err))?, 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))) Ok(factory.text(format_args!("{}", text)))
} }
BodyNode::Element(el) => { BodyNode::Element(el) => {
@@ -91,7 +98,7 @@ fn build_node<'a>(
}; };
if let Some((name, namespace)) = attrbute_to_static_str(&name) { 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 { attributes.push(Attribute {
name, name,
value: AttributeValue::Text(value), value: AttributeValue::Text(value),
@@ -187,7 +194,7 @@ fn build_node<'a>(
let ifmt: InterpertedIfmt = InterpertedIfmt( let ifmt: InterpertedIfmt = InterpertedIfmt(
parse_str(&lit.value()).map_err(|err| Error::ParseError(err))?, 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( Ok(factory.raw_element(
tag, tag,