Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use proc_macro2::{Ident, TokenStream};
use proc_macro_error::emit_error;
use quote::quote;
use std::collections::HashSet;
use syn::{
ext::IdentExt,
parse::{Parse, ParseStream, Result},
spanned::Spanned,
};
use crate::child::Child;
use crate::{attribute::Attribute, children::Children};
#[derive(Clone, Debug)]
pub struct WidgetAttributes {
pub attributes: HashSet<Attribute>,
}
impl WidgetAttributes {
pub fn new(attributes: HashSet<Attribute>) -> Self {
Self { attributes }
}
pub fn for_custom_element<'c>(&self, children: &'c Children) -> CustomWidgetAttributes<'_, 'c> {
CustomWidgetAttributes {
attributes: &self.attributes,
children,
}
}
pub fn custom_parse(input: ParseStream) -> Result<Self> {
let mut parsed_self = input.parse::<Self>()?;
let new_attributes: HashSet<Attribute> = parsed_self
.attributes
.drain()
.filter_map(|attribute| match attribute.validate() {
Ok(x) => Some(x),
Err(err) => {
emit_error!(err.span(), "Invalid attribute: {}", err);
None
}
})
.collect();
Ok(WidgetAttributes::new(new_attributes))
}
}
impl Parse for WidgetAttributes {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let mut attributes: HashSet<Attribute> = HashSet::new();
while input.peek(syn::Ident::peek_any) {
let attribute = input.parse::<Attribute>()?;
let ident = attribute.ident();
if attributes.contains(&attribute) {
emit_error!(
ident.span(),
"There is a previous definition of the {} attribute",
quote!(#ident)
);
}
attributes.insert(attribute);
}
Ok(WidgetAttributes::new(attributes))
}
}
pub struct CustomWidgetAttributes<'a, 'c> {
pub attributes: &'a HashSet<Attribute>,
pub children: &'c Children,
}
impl<'a, 'c> CustomWidgetAttributes<'a, 'c> {
/// Assign this widget's attributes to the given ident
///
/// This takes the form: `IDENT.ATTR_NAME = ATTR_VALUE;`
///
/// # Arguments
///
/// * `ident`: The ident to assign to (i.e. "props")
///
/// returns: TokenStream
pub fn assign_attributes(&self, _ident: &Ident) -> TokenStream {
let attrs = self
.attributes
.iter()
.filter_map(|attribute| {
let key = attribute.ident();
let value = attribute.value_tokens();
let key_name = quote! { #key }.to_string();
if key_name == "id" || key_name == "key" {
None
} else {
Some(quote! {
#key: #value,
})
}
})
.collect::<Vec<_>>();
let result = quote! {
#( #attrs )*
};
result
}
/// Determines whether `children` should be added to this widget or not
pub fn should_add_children(&self) -> bool {
if self.children.nodes.is_empty() {
// No children
false
} else if self.children.nodes.len() == 1 {
let child = self.children.nodes.first().unwrap();
match child {
Child::RawBlock((block, _)) => {
// Is child NOT an empty block? (`<Foo>{}</Foo>`)
!block.stmts.is_empty()
}
// Child is a widget
_ => true,
}
} else {
// Multiple children
true
}
}
}