Skip to main content

zinq_reflect/number/
float.rs

1use crate::TypeOf;
2
3macro_rules! float_type {
4    ($($name:ident $type_name:ident $is_type:ident $to_type:ident $type:ty)*) => {
5        #[derive(Debug, Copy, Clone, PartialEq)]
6        #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7        pub enum FloatType {
8            $($name($type_name),)*
9        }
10
11        impl crate::Type {
12            $(
13                pub fn $is_type(&self) -> bool {
14                    return match self {
15                        Self::Number(v) => v.$is_type(),
16                        _ => false,
17                    };
18                }
19
20                pub fn $to_type(&self) -> $type_name {
21                    return match self {
22                        Self::Number(v) => v.$to_type(),
23                        _ => panic!("called '{}' on type '{}'", stringify!($to_type), stringify!($name)),
24                    };
25                }
26            )*
27        }
28
29        impl crate::NumberType {
30            $(
31                pub fn $is_type(&self) -> bool {
32                    return match self {
33                        Self::Float(v) => v.$is_type(),
34                        _ => false,
35                    };
36                }
37
38                pub fn $to_type(&self) -> $type_name {
39                    return match self {
40                        Self::Float(v) => v.$to_type(),
41                        _ => panic!("called '{}' on type '{}'", stringify!($to_type), stringify!($name)),
42                    };
43                }
44            )*
45        }
46
47        impl FloatType {
48            pub fn id(&self) -> std::any::TypeId {
49                return match self {
50                    $(Self::$name(v) => v.id(),)*
51                };
52            }
53
54            pub fn name(&self) -> String {
55                return match self {
56                    $(Self::$name(v) => v.name(),)*
57                };
58            }
59
60            pub fn to_type(&self) -> crate::Type {
61                return crate::Type::Number(crate::NumberType::Float(self.clone()));
62            }
63
64            $(
65                pub fn $is_type(&self) -> bool {
66                    return match self {
67                        Self::$name(_) => true,
68                        _ => false,
69                    };
70                }
71
72                pub fn $to_type(&self) -> $type_name {
73                    return match self {
74                        Self::$name(v) => v.clone(),
75                        _ => panic!("called '{}' on type '{}'", stringify!($to_type), stringify!($name)),
76                    };
77                }
78            )*
79
80            pub fn assignable_to(&self, _type: crate::Type) -> bool {
81                return match self {
82                    $(Self::$name(v) => v.assignable_to(_type),)*
83                };
84            }
85
86            pub fn convertable_to(&self, _type: crate::Type) -> bool {
87                return match self {
88                    $(Self::$name(v) => v.convertable_to(_type),)*
89                };
90            }
91        }
92
93        impl std::fmt::Display for FloatType {
94            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95                return match self {
96                    $(Self::$name(v) => write!(f, "{}", v),)*
97                };
98            }
99        }
100
101        impl From<std::any::TypeId> for FloatType {
102            fn from(value: std::any::TypeId) -> Self {
103                return Self::from(&value);
104            }
105        }
106
107        impl From<&std::any::TypeId> for FloatType {
108            fn from(value: &std::any::TypeId) -> Self {
109                if value == &std::any::TypeId::of::<f32>() {
110                    return Self::F32(F32Type::default());
111                } else if value == &std::any::TypeId::of::<f64>() {
112                    return Self::F64(F64Type::default());
113                }
114
115                panic!("invalid type");
116            }
117        }
118
119        $(
120            #[derive(Debug, Copy, Clone, PartialEq, Default)]
121            #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
122            pub struct $type_name;
123
124            impl $type_name {
125                pub fn id(&self) -> std::any::TypeId {
126                    return std::any::TypeId::of::<$type>();
127                }
128
129                pub fn name(&self) -> String {
130                    return format!("type::{}", stringify!($name));
131                }
132
133                pub fn assignable_to(&self, _type: crate::Type) -> bool {
134                    return self.id() == _type.id();
135                }
136
137                pub fn convertable_to(&self, _type: crate::Type) -> bool {
138                    return _type.is_float();
139                }
140            }
141
142            impl std::fmt::Display for $type_name {
143                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
144                    return write!(f, "{}", self.name());
145                }
146            }
147
148            impl crate::TypeOf for $type {
149                fn type_of() -> crate::Type {
150                    return crate::Type::Number(crate::NumberType::Float(crate::FloatType::$name($type_name)));
151                }
152            }
153        )*
154    };
155}
156
157macro_rules! float_value {
158    ($($name:ident $type_name:ident $is_type:ident $to_type:ident $type:ty)*) => {
159        #[derive(Debug, Copy, Clone, PartialEq)]
160        #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
161        pub enum Float {
162            $($name($name),)*
163        }
164
165        impl crate::Value {
166            $(
167                pub fn $is_type(&self) -> bool {
168                    return match self {
169                        Self::Number(v) => v.$is_type(),
170                        _ => false,
171                    };
172                }
173
174                pub fn $to_type(&self) -> $name {
175                    return match self {
176                        Self::Number(v) => v.$to_type(),
177                        _ => panic!("called '{}' on type '{}'", stringify!($to_type), stringify!($name)),
178                    };
179                }
180            )*
181        }
182
183        impl crate::Number {
184            $(
185                pub fn $is_type(&self) -> bool {
186                    return match self {
187                        Self::Float(v) => v.$is_type(),
188                        _ => false,
189                    };
190                }
191
192                pub fn $to_type(&self) -> $name {
193                    return match self {
194                        Self::Float(v) => v.$to_type(),
195                        _ => panic!("called '{}' on type '{}'", stringify!($to_type), stringify!($name)),
196                    };
197                }
198            )*
199        }
200
201        impl Float {
202            pub fn to_type(&self) -> crate::Type {
203                return match self {
204                    $(Self::$name(v) => v.to_type(),)*
205                };
206            }
207
208            $(
209                pub fn $is_type(&self) -> bool {
210                    return match self {
211                        Self::$name(_) => true,
212                        _ => false,
213                    };
214                }
215
216                pub fn $to_type(&self) -> $name {
217                    return match self {
218                        Self::$name(v) => v.clone(),
219                        _ => panic!("called '{}' on type '{}'", stringify!($to_type), stringify!($name)),
220                    };
221                }
222            )*
223        }
224
225        impl crate::Reflect for Float {
226            fn reflect(self) -> crate::Value {
227                return match self {
228                    $(Self::$name(v) => v.reflect(),)*
229                };
230            }
231        }
232
233        impl std::fmt::Display for Float {
234            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
235                return match self {
236                    $(Self::$name(v) => write!(f, "{}", v),)*
237                };
238            }
239        }
240
241        $(
242            #[derive(Debug, Copy, Clone, PartialEq, Default)]
243            #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
244            pub struct $name($type);
245
246            impl $name {
247                pub fn get(&self) -> $type {
248                    return self.0;
249                }
250            }
251
252            impl From<$type> for crate::Value {
253                fn from(value: $type) -> Self {
254                    return Self::Number(crate::Number::Float(crate::Float::$name($name(value))));
255                }
256            }
257
258            impl Into<$type> for crate::Value {
259                fn into(self) -> $type {
260                    return self.$to_type().get();
261                }
262            }
263
264            impl From<$type> for $name {
265                fn from(value: $type) -> Self {
266                    return Self(value);
267                }
268            }
269
270            impl Into<$type> for $name {
271                fn into(self) -> $type {
272                    return self.0;
273                }
274            }
275
276            impl AsRef<$type> for $name {
277                fn as_ref(&self) -> &$type {
278                    return &self.0;
279                }
280            }
281
282            impl AsMut<$type> for $name {
283                fn as_mut(&mut self) -> &mut $type {
284                    return &mut self.0;
285                }
286            }
287
288            impl std::ops::Deref for $name {
289                type Target = $type;
290
291                fn deref(&self) -> &Self::Target {
292                    return &self.0;
293                }
294            }
295
296            impl std::ops::DerefMut for $name {
297                fn deref_mut(&mut self) -> &mut Self::Target {
298                    return &mut self.0;
299                }
300            }
301
302            impl std::fmt::Display for $name {
303                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
304                    return write!(f, "{}", self.as_ref());
305                }
306            }
307
308            impl crate::TypeOf for $name {
309                fn type_of() -> crate::Type {
310                    return <$type>::type_of();
311                }
312            }
313
314            impl crate::Reflect for $name {
315                fn reflect(self) -> crate::Value {
316                    return crate::Value::Number(crate::Number::Float(Float::$name(self.clone())));
317                }
318            }
319
320            impl crate::Reflect for $type {
321                fn reflect(self) -> crate::Value {
322                    return crate::Value::Number(crate::Number::Float(Float::$name($name(self.clone()))));
323                }
324            }
325        )*
326    };
327}
328
329float_type! {
330    F32 F32Type is_f32 to_f32 f32
331    F64 F64Type is_f64 to_f64 f64
332}
333
334float_value! {
335    F32 F32Type is_f32 to_f32 f32
336    F64 F64Type is_f64 to_f64 f64
337}
338
339#[cfg(test)]
340mod test {
341    use crate::*;
342
343    #[test]
344    pub fn f32() {
345        let value = value_of!(300.26_f32);
346
347        assert!(value.is_float());
348        assert!(value.is_f32());
349        assert_eq!(value.to_f32().get(), 300.26);
350    }
351
352    #[test]
353    pub fn f64() {
354        let value = value_of!(350.26_f64);
355
356        assert!(value.is_float());
357        assert!(value.is_f64());
358        assert_eq!(value.to_f64().get(), 350.26);
359    }
360}