Skip to content

Commit 2ec39ff

Browse files
committed
feat: support telling the attr the exact type
1 parent cec9150 commit 2ec39ff

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

  • html-node-macro/src/node_handlers

html-node-macro/src/node_handlers/mod.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rstml::node::{
1010
KeyedAttribute, NodeAttribute, NodeBlock, NodeComment, NodeDoctype, NodeElement, NodeFragment,
1111
NodeName, NodeText, RawText,
1212
};
13-
use syn::{spanned::Spanned, Type};
13+
use syn::{spanned::Spanned, Expr, ExprCast, Type};
1414

1515
use crate::tokenize_nodes;
1616

@@ -94,18 +94,27 @@ pub fn handle_element_untyped(
9494

9595
let key = quote!(::std::convert::Into::<::std::string::String>::into(#key));
9696

97-
let attribute_tokens = attribute.value().map_or_else(
97+
let value = attribute.value().map(|value| match value {
98+
Expr::Cast(ExprCast { expr, ty, .. }) => (&**expr, Some(ty)),
99+
_ => (value, None),
100+
});
101+
102+
let attribute_tokens = value.map_or_else(
98103
|| quote!((#key, ::std::option::Option::None)),
99-
|value| {
100-
quote! {
104+
|(value, ty)| ty.map_or_else(
105+
|| quote!{
101106
(
102107
#key,
103-
::std::option::Option::Some(
104-
::std::string::ToString::to_string(&#value),
105-
),
108+
::std::option::Option::Some(::std::string::ToString::to_string(&#value)),
106109
)
107-
}
108-
},
110+
},
111+
|ty| quote!{
112+
(
113+
#key,
114+
::std::option::Option::<#ty>::from(#value).map(|v| ::std::string::ToString::to_string(&v)),
115+
)
116+
},
117+
),
109118
);
110119

111120
(attribute_tokens, None)

0 commit comments

Comments
 (0)