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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use std::sync::atomic::{AtomicU16, Ordering};
use proc_macro2::Span as Span2;
use syn::parse::ParseBuffer;
use syn::spanned::Spanned;
use syn::{braced, bracketed, parenthesized, Error, Ident, Path};
use super::{ParseError, PropertyName};
use crate::widgets::{AssignPropertyAttr, WidgetAttr, WidgetFunc};
pub(super) fn attr_twice_error(span: Span2) -> Error {
Error::new(span, "Cannot use the same attribute twice.")
}
impl From<Error> for ParseError {
fn from(error: Error) -> Self {
Self::Generic(error.to_compile_error())
}
}
impl ParseError {
pub(super) fn add_path(self, path: &Path) -> Self {
if let ParseError::Generic(tokens) = self {
if let Some(ident) = path.get_ident() {
ParseError::Ident((ident.clone(), tokens))
} else {
ParseError::Path((path.clone(), tokens))
}
} else {
self
}
}
}
impl WidgetFunc {
pub(super) fn into_property_name(self) -> Result<PropertyName, Error> {
if let Some(methods) = &self.method_chain {
Err(Error::new(
methods.span(),
"Can't use method calls in property assignments",
))
} else if let Some(args) = &self.args {
Err(Error::new(
args.span(),
"Can't use function arguments in property assignments",
))
} else {
Ok(if let Some(ident) = self.path.get_ident() {
PropertyName::Ident(ident.clone())
} else {
PropertyName::Path(self.path)
})
}
}
}
impl WidgetFunc {
pub(super) fn snake_case_name(&self) -> Ident {
idents_to_snake_case(
self.path.segments.iter().map(|seg| &seg.ident),
self.path.span(),
)
}
}
impl WidgetAttr {
pub(super) fn is_local_attr(&self) -> bool {
matches!(self, Self::Local | Self::LocalRef)
}
}
impl PartialEq for AssignPropertyAttr {
fn eq(&self, other: &Self) -> bool {
core::mem::discriminant(self) == core::mem::discriminant(other)
}
}
pub(crate) fn string_to_snake_case(string: &str) -> Ident {
idents_to_snake_case(
[Ident::new(string, Span2::call_site())].iter(),
Span2::call_site(),
)
}
pub(crate) fn idents_to_snake_case<'a, I: Iterator<Item = &'a Ident>>(
idents: I,
span: Span2,
) -> Ident {
static COUNTER: AtomicU16 = AtomicU16::new(0);
let val = COUNTER.fetch_add(1, Ordering::Relaxed);
let index_str = val.to_string();
let segments: Vec<String> = idents
.map(|ident| ident.to_string().to_lowercase())
.collect();
let length: usize =
segments.iter().map(|seg| seg.len() + 1).sum::<usize>() + index_str.len() + 1;
let mut name: String = String::with_capacity(length);
for seg in &segments {
name.push('_');
name.push_str(seg);
}
name.push('_');
name.push_str(&index_str);
Ident::new(&name, span)
}
pub(super) fn parens<'a>(input: &'a ParseBuffer<'_>) -> Result<ParseBuffer<'a>, ParseError> {
let content = (move || {
let content;
parenthesized!(content in input);
Ok(content)
})();
Ok(content?)
}
pub(super) fn braces<'a>(input: &'a ParseBuffer<'_>) -> Result<ParseBuffer<'a>, ParseError> {
let content = (move || {
let content;
braced!(content in input);
Ok(content)
})();
Ok(content?)
}
pub(super) fn brackets<'a>(input: &'a ParseBuffer<'_>) -> Result<ParseBuffer<'a>, ParseError> {
let content = (move || {
let content;
bracketed!(content in input);
Ok(content)
})();
Ok(content?)
}