handle formatting options

This commit is contained in:
Evan Almloff committed 2022-05-30 18:32:57 -05:00
1 parent 2183ecf3fb
commit 94448ea4aa
10 files changed
+278 -386

No files matched your search

+41 -74
View File
@@ -1,7 +1,6 @@
use dioxus_core::{Attribute, AttributeValue, NodeFactory, VNode};
use dioxus_rsx::{BodyNode, CallBody, ElementAttr};
use dioxus_rsx::{BodyNode, CallBody, ElementAttr, IfmtInput, Segment};
use quote::ToTokens;
use std::str::FromStr;
use syn::{parse2, parse_str, Expr};
use crate::attributes::attrbute_to_static_str;
@@ -9,28 +8,31 @@ use crate::captuered_context::{CapturedContext, IfmtArgs};
use crate::elements::element_to_static_str;
use crate::error::{Error, RecompileReason};
#[derive(Debug)]
enum Segment {
Ident(String),
Literal(String),
}
struct InterpertedIfmt(IfmtInput);
struct InterperedIfmt {
segments: Vec<Segment>,
}
impl InterperedIfmt {
impl InterpertedIfmt {
fn resolve(&self, captured: &IfmtArgs) -> String {
let mut result = String::new();
for seg in &self.segments {
for seg in &self.0.segments {
match seg {
Segment::Ident(name) => {
let (_, value) = captured
Segment::Formatted {
segment,
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(|(n, _)| *n == name)
.expect(format!("could not resolve {}", name).as_str());
result.push_str(value);
.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);
}
Segment::Literal(lit) => result.push_str(lit),
}
@@ -39,52 +41,6 @@ impl InterperedIfmt {
}
}
impl FromStr for InterperedIfmt {
type Err = ();
fn from_str(input: &str) -> Result<Self, ()> {
let mut segments = Vec::new();
let mut segment = String::new();
let mut chars = input.chars().peekable();
while let Some(c) = chars.next() {
if c == '{' {
if chars.peek().copied() != Some('{') {
let old;
(old, segment) = (segment, String::new());
if !old.is_empty() {
segments.push(Segment::Literal(old));
}
while let Some(c) = chars.next() {
if c == '}' {
let old;
(old, segment) = (segment, String::new());
if !old.is_empty() {
segments.push(Segment::Ident(old));
}
break;
}
if c == ':' {
while Some('}') != chars.next() {}
let old;
(old, segment) = (segment, String::new());
if !old.is_empty() {
segments.push(Segment::Ident(old));
}
break;
}
segment.push(c);
}
}
} else {
segment.push(c);
}
}
if !segment.is_empty() {
segments.push(Segment::Literal(segment));
}
Ok(Self { segments })
}
}
pub fn build<'a>(
rsx: CallBody,
mut ctx: CapturedContext<'a>,
@@ -105,7 +61,9 @@ fn build_node<'a>(
let bump = factory.bump();
match node {
BodyNode::Text(text) => {
let ifmt: InterperedIfmt = text.value().parse().unwrap();
let ifmt = InterpertedIfmt(
IfmtInput::from_str(&text.value()).map_err(|err| Error::ParseError(err))?,
);
let text = bump.alloc(ifmt.resolve(&ctx.captured));
Ok(factory.text(format_args!("{}", text)))
}
@@ -114,13 +72,21 @@ fn build_node<'a>(
for attr in &el.attributes {
match &attr.attr {
ElementAttr::AttrText { .. } | ElementAttr::CustomAttrText { .. } => {
let (name, value): (String, InterperedIfmt) = match &attr.attr {
ElementAttr::AttrText { name, value } => {
(name.to_string(), value.value().parse().unwrap())
}
ElementAttr::CustomAttrText { name, value } => {
(name.value(), value.value().parse().unwrap())
}
let (name, value): (String, InterpertedIfmt) = match &attr.attr {
ElementAttr::AttrText { name, value } => (
name.to_string(),
InterpertedIfmt(
IfmtInput::from_str(&value.value())
.map_err(|err| Error::ParseError(err))?,
),
),
ElementAttr::CustomAttrText { name, value } => (
name.value(),
InterpertedIfmt(
IfmtInput::from_str(&value.value())
.map_err(|err| Error::ParseError(err))?,
),
),
_ => unreachable!(),
};
@@ -145,7 +111,6 @@ fn build_node<'a>(
ElementAttr::CustomAttrExpression { name, value } => {
(name.value(), value)
}
_ => unreachable!(),
};
if let Some((_, resulting_value)) = ctx
@@ -219,7 +184,9 @@ fn build_node<'a>(
None,
)),
Some(lit) => {
let ifmt: InterperedIfmt = lit.value().parse().unwrap();
let ifmt: InterpertedIfmt = InterpertedIfmt(
parse_str(&lit.value()).map_err(|err| Error::ParseError(err))?,
);
let key = bump.alloc(ifmt.resolve(&ctx.captured));
Ok(factory.raw_element(