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
210
211
212
213
214
215
216
217
218
219
|
use crate::eval::{CastInfo, FromValue, IntoValue, Reflect};
use super::*;
/// A container with components for the four corners of a rectangle.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Corners<T> {
/// The value for the top left corner.
pub top_left: T,
/// The value for the top right corner.
pub top_right: T,
/// The value for the bottom right corner.
pub bottom_right: T,
/// The value for the bottom left corner.
pub bottom_left: T,
}
impl<T> Corners<T> {
/// Create a new instance from the four components.
pub const fn new(top_left: T, top_right: T, bottom_right: T, bottom_left: T) -> Self {
Self { top_left, top_right, bottom_right, bottom_left }
}
/// Create an instance with four equal components.
pub fn splat(value: T) -> Self
where
T: Clone,
{
Self {
top_left: value.clone(),
top_right: value.clone(),
bottom_right: value.clone(),
bottom_left: value,
}
}
/// Map the individual fields with `f`.
pub fn map<F, U>(self, mut f: F) -> Corners<U>
where
F: FnMut(T) -> U,
{
Corners {
top_left: f(self.top_left),
top_right: f(self.top_right),
bottom_right: f(self.bottom_right),
bottom_left: f(self.bottom_left),
}
}
/// Zip two instances into one.
pub fn zip<U>(self, other: Corners<U>) -> Corners<(T, U)> {
Corners {
top_left: (self.top_left, other.top_left),
top_right: (self.top_right, other.top_right),
bottom_right: (self.bottom_right, other.bottom_right),
bottom_left: (self.bottom_left, other.bottom_left),
}
}
/// An iterator over the corners, starting with the top left corner,
/// clockwise.
pub fn iter(&self) -> impl Iterator<Item = &T> {
[&self.top_left, &self.top_right, &self.bottom_right, &self.bottom_left]
.into_iter()
}
/// Whether all sides are equal.
pub fn is_uniform(&self) -> bool
where
T: PartialEq,
{
self.top_left == self.top_right
&& self.top_right == self.bottom_right
&& self.bottom_right == self.bottom_left
}
}
impl<T> Get<Corner> for Corners<T> {
type Component = T;
fn get_ref(&self, corner: Corner) -> &T {
match corner {
Corner::TopLeft => &self.top_left,
Corner::TopRight => &self.top_right,
Corner::BottomRight => &self.bottom_right,
Corner::BottomLeft => &self.bottom_left,
}
}
fn get_mut(&mut self, corner: Corner) -> &mut T {
match corner {
Corner::TopLeft => &mut self.top_left,
Corner::TopRight => &mut self.top_right,
Corner::BottomRight => &mut self.bottom_right,
Corner::BottomLeft => &mut self.bottom_left,
}
}
}
/// The four corners of a rectangle.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Corner {
/// The top left corner.
TopLeft,
/// The top right corner.
TopRight,
/// The bottom right corner.
BottomRight,
/// The bottom left corner.
BottomLeft,
}
impl<T: Reflect> Reflect for Corners<Option<T>> {
fn describe() -> CastInfo {
T::describe() + Dict::describe()
}
fn castable(value: &Value) -> bool {
Dict::castable(value) || T::castable(value)
}
}
impl<T> IntoValue for Corners<T>
where
T: PartialEq + IntoValue,
{
fn into_value(self) -> Value {
if self.is_uniform() {
return self.top_left.into_value();
}
let mut dict = Dict::new();
let mut handle = |key: &str, component: T| {
let value = component.into_value();
if value != Value::None {
dict.insert(key.into(), value);
}
};
handle("top-left", self.top_left);
handle("top-right", self.top_right);
handle("bottom-right", self.bottom_right);
handle("bottom-left", self.bottom_left);
Value::Dict(dict)
}
}
impl<T> FromValue for Corners<Option<T>>
where
T: FromValue + Clone,
{
fn from_value(mut value: Value) -> StrResult<Self> {
let keys = [
"top-left",
"top-right",
"bottom-right",
"bottom-left",
"left",
"top",
"right",
"bottom",
"rest",
];
if let Value::Dict(dict) = &mut value {
if dict.iter().any(|(key, _)| keys.contains(&key.as_str())) {
let mut take = |key| dict.take(key).ok().map(T::from_value).transpose();
let rest = take("rest")?;
let left = take("left")?.or_else(|| rest.clone());
let top = take("top")?.or_else(|| rest.clone());
let right = take("right")?.or_else(|| rest.clone());
let bottom = take("bottom")?.or_else(|| rest.clone());
let corners = Corners {
top_left: take("top-left")?
.or_else(|| top.clone())
.or_else(|| left.clone()),
top_right: take("top-right")?
.or_else(|| top.clone())
.or_else(|| right.clone()),
bottom_right: take("bottom-right")?
.or_else(|| bottom.clone())
.or_else(|| right.clone()),
bottom_left: take("bottom-left")?
.or_else(|| bottom.clone())
.or_else(|| left.clone()),
};
dict.finish(&keys)?;
return Ok(corners);
}
}
if T::castable(&value) {
Ok(Self::splat(Some(T::from_value(value)?)))
} else {
Err(Self::error(&value))
}
}
}
impl<T: Resolve> Resolve for Corners<T> {
type Output = Corners<T::Output>;
fn resolve(self, styles: StyleChain) -> Self::Output {
self.map(|v| v.resolve(styles))
}
}
impl<T: Fold> Fold for Corners<Option<T>> {
type Output = Corners<T::Output>;
fn fold(self, outer: Self::Output) -> Self::Output {
self.zip(outer).map(|(inner, outer)| match inner {
Some(value) => value.fold(outer),
None => outer,
})
}
}
|