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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use super::{
colors,
PropertyToken,
};
use bevy::{
prelude::{
Color,
Deref
},
ui::{
OverflowAxis,
UiRect,
Val,
},
};
use smallvec::SmallVec;
/// A list of [`PropertyToken`] which was parsed from a single property.
#[derive(Clone, Debug, Default)]
#[derive(Deref)]
pub struct PropertyValues(
pub(crate) SmallVec<[PropertyToken; 8]>
);
impl PropertyValues
{
/// Tries to parses the current values as a single [`String`].
pub fn string(
&self
) -> Option<String> {
self.0.iter()
.find_map(|token| match token
{
PropertyToken::String(id) => match id.is_empty()
{
true => None,
false => Some(id.clone()),
},
_ => None,
})
}
/// Tries to parses the current values as a single [`Color`].
///
/// Currently only [named colors](https://developer.mozilla.org/en-US/docs/Web/CSS/named-color)
/// and [hex-colors](https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color) are supported.
pub fn color(
&self
) -> Option<Color> {
if self.0.len() == 1
{
match &self.0[0]
{
PropertyToken::Identifier(name) => colors::parse_named_color(name.as_str()),
PropertyToken::Hash(hash) => colors::parse_hex_color(hash.as_str()),
_ => None,
}
}
else
{
// TODO: Implement color function like rgba(255, 255, 255, 255)
// https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
None
}
}
/// Tries to parse the current value as a single [`OverflowAxis`].
pub fn overflow(
&self
) -> Option<OverflowAxis> {
if self.0.len() == 1
{
match &self.0[0]
{
PropertyToken::Identifier(overflow) => match overflow.as_ref()
{
"visible" => Some(OverflowAxis::Visible),
"hidden" | "clip" => Some(OverflowAxis::Clip),
_ => None,
},
_ => None,
}
}
else
{
None
}
}
/// Tries to parses the current values as a single identifier.
pub fn identifier(
&self
) -> Option<&str> {
self.0.iter()
.find_map(|token| match token
{
PropertyToken::Identifier(id) => match id.is_empty()
{
true => None,
false => Some(id.as_str()),
}
_ => None,
})
}
/// Tries to parses the current values as a single [`Val`].
///
/// Only [`Percentage`](PropertyToken::Percentage) and [`Dimension`](PropertyToken::Dimension`) are considered valid values,
/// where former is converted to [`Val::Percent`] and latter is converted to [`Val::Px`].
pub fn val(
&self
) -> Option<Val> {
self.0.iter()
.find_map(|token| match token
{
PropertyToken::Percentage(val) => Some(Val::Percent(*val)),
PropertyToken::Dimension(val) => Some(Val::Px(*val)),
PropertyToken::Identifier(val) if val == "auto" => Some(Val::Auto),
_ => None,
})
}
/// Tries to parses the current values as a single [`f32`].
///
/// Only [`Percentage`](PropertyToken::Percentage), [`Dimension`](PropertyToken::Dimension`) and [`Number`](PropertyToken::Number`)
/// are considered valid values.
pub fn f32(
&self
) -> Option<f32> {
self.0.iter()
.find_map(|token| match token
{
PropertyToken::Percentage(val)
| PropertyToken::Dimension(val)
| PropertyToken::Number(val) => Some(*val),
_ => None,
})
}
/// Tries to parses the current values as a single [`Option<f32>`].
///
/// This function is useful for properties where either a numeric value or a `none` value is expected.
///
/// If a [`Option::None`] is returned, it means some invalid value was found.
///
/// If there is a [`Percentage`](PropertyToken::Percentage), [`Dimension`](PropertyToken::Dimension`) or [`Number`](PropertyToken::Number`) token,
/// a [`Option::Some`] with parsed [`Option<f32>`] is returned.
/// If there is a identifier with a `none` value, then [`Option::Some`] with [`None`] is returned.
pub fn option_f32(
&self
) -> Option<Option<f32>> {
self.0.iter()
.find_map(|token| match token
{
PropertyToken::Percentage(val)
| PropertyToken::Dimension(val)
| PropertyToken::Number(val) => Some(Some(*val)),
PropertyToken::Identifier(ident) => match ident.as_str()
{
"none" => Some(None),
_ => None,
},
_ => None,
})
}
/// Tries to parses the current values as a single [`Option<UiRect<Val>>`].
///
/// Optional values are handled by this function, so if only one value is present it is used as `top`, `right`, `bottom` and `left`,
/// otherwise values are applied in the following order: `top`, `right`, `bottom` and `left`.
///
/// Note that it is not possible to create a [`UiRect`] with only `top` value, since it'll be understood to replicated it on all fields.
pub fn rect(
&self
) -> Option<UiRect> {
if self.0.len() == 1
{
self.val().map(UiRect::all)
}
else
{
self.0.iter()
.fold((None, 0), |(rect, idx), token|
{
let val = match token
{
PropertyToken::Percentage(val) => Val::Percent(*val),
PropertyToken::Dimension(val) => Val::Px(*val),
PropertyToken::Identifier(val) if val == "auto" => Val::Auto,
_ => return (rect, idx),
};
let mut rect: UiRect = rect.unwrap_or_default();
match idx
{
0 => rect.top = val,
1 => rect.right = val,
2 => rect.bottom = val,
3 => rect.left = val,
_ => (),
}
(Some(rect), idx + 1)
}).0
}
}
}