pax_runtime_api/pax_value/
to_from_impls.rs1use std::ops::Range;
2use std::rc::Rc;
3
4use std::cell::RefCell;
5
6use super::ImplToFromPaxAny;
7use super::Numeric;
8use super::PaxValue;
9use super::ToPaxValue;
10use crate::impl_to_from_pax_value;
11use crate::math::Space;
12use crate::math::Transform2;
13use crate::math::Vector2;
14use crate::properties::PropertyValue;
15use crate::Color;
16use crate::ColorChannel;
17use crate::Fill;
18use crate::GradientStop;
19use crate::LinearGradient;
20use crate::PathElement;
21use crate::Percent;
22use crate::Property;
23use crate::RadialGradient;
24use crate::Rotation;
25use crate::Size;
26use crate::Stroke;
27use crate::Transform2D;
28
29impl_to_from_pax_value!(bool, PaxValue::Bool);
31
32impl_to_from_pax_value!(u8, PaxValue::Numeric, Numeric::U8);
33impl_to_from_pax_value!(u16, PaxValue::Numeric, Numeric::U16);
34impl_to_from_pax_value!(u32, PaxValue::Numeric, Numeric::U32);
35impl_to_from_pax_value!(u64, PaxValue::Numeric, Numeric::U64);
36
37impl_to_from_pax_value!(i8, PaxValue::Numeric, Numeric::I8);
38impl_to_from_pax_value!(i16, PaxValue::Numeric, Numeric::I16);
39impl_to_from_pax_value!(i32, PaxValue::Numeric, Numeric::I32);
40impl_to_from_pax_value!(i64, PaxValue::Numeric, Numeric::I64);
41
42impl_to_from_pax_value!(f32, PaxValue::Numeric, Numeric::F32);
43impl_to_from_pax_value!(f64, PaxValue::Numeric, Numeric::F64);
44
45impl_to_from_pax_value!(isize, PaxValue::Numeric, Numeric::ISize);
46impl_to_from_pax_value!(usize, PaxValue::Numeric, Numeric::USize);
47
48impl ImplToFromPaxAny for () {}
50
51impl<F: Space, T: Space> ImplToFromPaxAny for Transform2<F, T> {}
53
54impl_to_from_pax_value!(String, PaxValue::String);
55
56impl_to_from_pax_value!(Numeric, PaxValue::Numeric);
58impl_to_from_pax_value!(Size, PaxValue::Size);
59impl_to_from_pax_value!(Rotation, PaxValue::Rotation);
60impl_to_from_pax_value!(Percent, PaxValue::Percent);
61impl_to_from_pax_value!(PathElement, PaxValue::PathElement);
62
63impl ToPaxValue for Color {
64    fn to_pax_value(self) -> PaxValue {
65        PaxValue::Color(Box::new(self))
66    }
67}
68
69impl<T: ToPaxValue> ToPaxValue for Vec<T> {
71    fn to_pax_value(self) -> PaxValue {
72        PaxValue::Vec(
73            self.into_iter()
74                .map(|v| v.to_pax_value())
75                .collect::<Vec<_>>(),
76        )
77    }
78}
79
80impl<T: ToPaxValue> ToPaxValue for Option<T> {
81    fn to_pax_value(self) -> PaxValue {
82        match self {
83            Some(v) => PaxValue::Option(Box::new(Some(v.to_pax_value()))),
84            None => PaxValue::Option(Box::new(None)),
85        }
86    }
87}
88
89impl<T: ToPaxValue> ToPaxValue for Range<T> {
90    fn to_pax_value(self) -> PaxValue {
91        PaxValue::Range(
92            Box::new(self.start.to_pax_value()),
93            Box::new(self.end.to_pax_value()),
94        )
95    }
96}
97
98impl<T: ToPaxValue + Clone> ToPaxValue for Rc<RefCell<T>> {
99    fn to_pax_value(self) -> PaxValue {
100        crate::borrow!(self).clone().to_pax_value()
101    }
102}
103
104impl ToPaxValue for PaxValue {
105    fn to_pax_value(self) -> PaxValue {
106        self
107    }
108}
109
110impl<T: ToPaxValue + PropertyValue> ToPaxValue for Property<T> {
111    fn to_pax_value(self) -> PaxValue {
112        self.get().to_pax_value()
113    }
114}
115
116impl ToPaxValue for Fill {
117    fn to_pax_value(self) -> PaxValue {
118        match self {
119            Fill::Solid(color) => PaxValue::Enum(
120                "Fill".to_string(),
121                "Solid".to_string(),
122                vec![color.to_pax_value()],
123            ),
124            Fill::LinearGradient(gradient) => PaxValue::Enum(
125                "Fill".to_string(),
126                "LinearGradient".to_string(),
127                vec![gradient.to_pax_value()],
128            ),
129            Fill::RadialGradient(gradient) => PaxValue::Enum(
130                "Fill".to_string(),
131                "RadialGradient".to_string(),
132                vec![gradient.to_pax_value()],
133            ),
134        }
135    }
136}
137
138impl ToPaxValue for ColorChannel {
139    fn to_pax_value(self) -> PaxValue {
140        match self {
141            ColorChannel::Rotation(rot) => PaxValue::Enum(
142                "ColorChannel".to_string(),
143                "Rotation".to_string(),
144                vec![rot.to_pax_value()],
145            ),
146            ColorChannel::Percent(perc) => PaxValue::Enum(
147                "ColorChannel".to_string(),
148                "Percent".to_string(),
149                vec![perc.to_pax_value()],
150            ),
151            ColorChannel::Integer(num) => PaxValue::Enum(
152                "ColorChannel".to_string(),
153                "Integer".to_string(),
154                vec![num.to_pax_value()],
155            ),
156        }
157    }
158}
159
160impl ToPaxValue for Stroke {
161    fn to_pax_value(self) -> PaxValue {
162        PaxValue::Object(
163            vec![
164                ("color".to_string(), self.color.get().to_pax_value()),
165                ("width".to_string(), self.width.to_pax_value()),
166            ]
167            .into_iter()
168            .collect(),
169        )
170    }
171}
172
173impl ToPaxValue for GradientStop {
174    fn to_pax_value(self) -> PaxValue {
175        PaxValue::Object(
176            vec![
177                ("position".to_string(), self.position.to_pax_value()),
178                ("color".to_string(), self.color.to_pax_value()),
179            ]
180            .into_iter()
181            .collect(),
182        )
183    }
184}
185
186impl ToPaxValue for LinearGradient {
187    fn to_pax_value(self) -> PaxValue {
188        PaxValue::Object(
189            vec![
190                (
191                    "start".to_string(),
192                    PaxValue::Vec(vec![
193                        self.start.0.to_pax_value(),
194                        self.start.1.to_pax_value(),
195                    ]),
196                ),
197                (
198                    "end".to_string(),
199                    PaxValue::Vec(vec![self.end.0.to_pax_value(), self.end.1.to_pax_value()]),
200                ),
201                ("stops".to_string(), self.stops.to_pax_value()),
202            ]
203            .into_iter()
204            .collect(),
205        )
206    }
207}
208
209impl ToPaxValue for RadialGradient {
210    fn to_pax_value(self) -> PaxValue {
211        PaxValue::Object(
212            vec![
213                (
214                    "start".to_string(),
215                    PaxValue::Vec(vec![
216                        self.start.0.to_pax_value(),
217                        self.start.1.to_pax_value(),
218                    ]),
219                ),
220                (
221                    "end".to_string(),
222                    PaxValue::Vec(vec![self.end.0.to_pax_value(), self.end.1.to_pax_value()]),
223                ),
224                ("radius".to_string(), self.radius.to_pax_value()),
225                ("stops".to_string(), self.stops.to_pax_value()),
226            ]
227            .into_iter()
228            .collect(),
229        )
230    }
231}
232
233impl ToPaxValue for Transform2D {
234    fn to_pax_value(self) -> PaxValue {
235        PaxValue::Object(
236            vec![
237                (
238                    "previous".to_string(),
239                    self.previous
240                        .map(|p| p.to_pax_value())
241                        .unwrap_or(PaxValue::Option(Box::new(None))),
242                ),
243                (
244                    "rotate".to_string(),
245                    self.rotate
246                        .map(|r| r.to_pax_value())
247                        .unwrap_or(PaxValue::Option(Box::new(None))),
248                ),
249                (
250                    "translate".to_string(),
251                    self.translate
252                        .map(|t| PaxValue::Option(Box::new(Some(t.to_vec().to_pax_value()))))
253                        .unwrap_or(PaxValue::Option(Box::new(None))),
254                ),
255                (
256                    "anchor".to_string(),
257                    self.anchor
258                        .map(|a| PaxValue::Option(Box::new(Some(a.to_vec().to_pax_value()))))
259                        .unwrap_or(PaxValue::Option(Box::new(None))),
260                ),
261                (
262                    "scale".to_string(),
263                    self.scale
264                        .map(|s| PaxValue::Option(Box::new(Some(s.to_vec().to_pax_value()))))
265                        .unwrap_or(PaxValue::Option(Box::new(None))),
266                ),
267                (
268                    "skew".to_string(),
269                    self.skew
270                        .map(|s| PaxValue::Option(Box::new(Some(s.to_vec().to_pax_value()))))
271                        .unwrap_or(PaxValue::Option(Box::new(None))),
272                ),
273            ]
274            .into_iter()
275            .collect(),
276        )
277    }
278}
279
280impl ToPaxValue for Transform2 {
281    fn to_pax_value(self) -> PaxValue {
282        PaxValue::Object(
283            vec![(
284                "m".to_string(),
285                PaxValue::Vec(self.m.iter().map(|v| v.to_pax_value()).collect::<Vec<_>>()),
286            )]
287            .into_iter()
288            .collect(),
289        )
290    }
291}
292
293impl ToPaxValue for Vector2 {
294    fn to_pax_value(self) -> PaxValue {
295        PaxValue::Object(
296            vec![
297                ("x".to_string(), self.x.to_pax_value()),
298                ("y".to_string(), self.y.to_pax_value()),
299            ]
300            .into_iter()
301            .collect(),
302        )
303    }
304}