Merge branch 'jk/rsx-refactor' of https://github.com/demonthos/dioxus into jk/rsx-refactor

This commit is contained in:
Evan Almloff committed 2022-06-13 16:48:52 -05:00
commit 9e4a069f42
7 files changed
+54 -50

No files matched your search

+34 -41
View File
@@ -9,43 +9,39 @@ use crate::captuered_context::{CapturedContext, IfmtArgs};
use crate::elements::element_to_static_str;
use crate::error::{Error, RecompileReason};
struct InterpertedIfmt(IfmtInput);
impl InterpertedIfmt {
fn resolve(&self, captured: &IfmtArgs) -> Result<String, Error> {
let mut result = String::new();
for seg in &self.0.segments {
match seg {
Segment::Formatted {
segment,
format_args,
} => {
let expr = segment.to_token_stream();
let expr: Expr = parse2(expr).unwrap();
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();
return Err(Error::RecompileRequiredError(
RecompileReason::CapturedExpression(format!(
"could not resolve {{{}:{}}}",
expr_str, format_args
)),
));
}
fn resolve_ifmt(ifmt: &IfmtInput, captured: &IfmtArgs) -> Result<String, Error> {
let mut result = String::new();
for seg in &ifmt.segments {
match seg {
Segment::Formatted {
segment,
format_args,
} => {
let expr = segment.to_token_stream();
let expr: Expr = parse2(expr).unwrap();
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();
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),
}
Ok(result)
}
Ok(result)
}
pub fn build<'a>(
@@ -68,10 +64,8 @@ fn build_node<'a>(
let bump = factory.bump();
match node {
BodyNode::Text(text) => {
let ifmt = InterpertedIfmt(
IfmtInput::from_str(&text.value()).map_err(|err| Error::ParseError(err))?,
);
let text = bump.alloc(ifmt.resolve(&ctx.captured)?);
let ifmt = IfmtInput::from_str(&text.value()).map_err(|err| Error::ParseError(err))?;
let text = bump.alloc(resolve_ifmt(&ifmt, &ctx.captured)?);
Ok(factory.text(format_args!("{}", text)))
}
BodyNode::Element(el) => {
@@ -101,7 +95,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(resolve_ifmt(&value, &ctx.captured)?);
attributes.push(Attribute {
name,
value: AttributeValue::Text(value),
@@ -197,10 +191,9 @@ fn build_node<'a>(
None,
)),
Some(lit) => {
let ifmt: InterpertedIfmt = InterpertedIfmt(
parse_str(&lit.value()).map_err(|err| Error::ParseError(err))?,
);
let key = bump.alloc(ifmt.resolve(&ctx.captured)?);
let ifmt: IfmtInput =
parse_str(&lit.value()).map_err(|err| Error::ParseError(err))?;
let key = bump.alloc(resolve_ifmt(&ifmt, &ctx.captured)?);
Ok(factory.raw_element(
tag,