facet_reflect/
scalar.rs

1use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};
2
3use facet_core::{ConstTypeId, Shape};
4
5/// All scalar types supported out of the box by peek and poke.
6#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
7#[non_exhaustive]
8pub enum ScalarType {
9    /// Unit tuple `()`.
10    Unit,
11    /// Primitive type `bool`.
12    Bool,
13    /// Primitive type `char`.
14    Char,
15    /// Primitive type `str`.
16    Str,
17    /// `alloc::string::String`.
18    String,
19    /// `alloc::borrow::Cow<'_, str>`.
20    CowStr,
21    /// Primitive type `f32`.
22    F32,
23    /// Primitive type `f64`.
24    F64,
25    /// Primitive type `u8`.
26    U8,
27    /// Primitive type `u16`.
28    U16,
29    /// Primitive type `u32`.
30    U32,
31    /// Primitive type `u64`.
32    U64,
33    /// Primitive type `u128`.
34    U128,
35    /// Primitive type `usize`.
36    USize,
37    /// Primitive type `i8`.
38    I8,
39    /// Primitive type `i16`.
40    I16,
41    /// Primitive type `i32`.
42    I32,
43    /// Primitive type `i64`.
44    I64,
45    /// Primitive type `i128`.
46    I128,
47    /// Primitive type `isize`.
48    ISize,
49    /// `core::net::SocketAddr`.
50    SocketAddr,
51    /// `core::net::IpAddr`.
52    IpAddr,
53    /// `core::net::Ipv4Addr`.
54    Ipv4Addr,
55    /// `core::net::Ipv6Addr`.
56    Ipv6Addr,
57    /// `facet_core::typeid::ConstTypeId`.
58    ConstTypeId,
59}
60
61impl ScalarType {
62    /// Infer the type from a shape definition.
63    pub fn try_from_shape(shape: &Shape<'_>) -> Option<Self> {
64        #[cfg(feature = "alloc")]
65        if shape.id == ConstTypeId::of::<alloc::string::String>() {
66            return Some(ScalarType::String);
67        } else if shape.id == ConstTypeId::of::<alloc::borrow::Cow<'_, str>>() {
68            return Some(ScalarType::CowStr);
69        } else if shape.id == ConstTypeId::of::<core::net::SocketAddr>() {
70            return Some(ScalarType::SocketAddr);
71        }
72
73        if shape.id == ConstTypeId::of::<()>() {
74            Some(Self::Unit)
75        } else if shape.id == ConstTypeId::of::<bool>() {
76            Some(ScalarType::Bool)
77        } else if shape.id == ConstTypeId::of::<char>() {
78            Some(ScalarType::Char)
79        } else if shape.id == ConstTypeId::of::<&str>() {
80            Some(ScalarType::Str)
81        } else if shape.id == ConstTypeId::of::<f32>() {
82            Some(ScalarType::F32)
83        } else if shape.id == ConstTypeId::of::<f64>() {
84            Some(ScalarType::F64)
85        } else if shape.id == ConstTypeId::of::<u8>() {
86            Some(ScalarType::U8)
87        } else if shape.id == ConstTypeId::of::<u16>() {
88            Some(ScalarType::U16)
89        } else if shape.id == ConstTypeId::of::<u32>() {
90            Some(ScalarType::U32)
91        } else if shape.id == ConstTypeId::of::<u64>() {
92            Some(ScalarType::U64)
93        } else if shape.id == ConstTypeId::of::<u128>() {
94            Some(ScalarType::U128)
95        } else if shape.id == ConstTypeId::of::<usize>() {
96            Some(ScalarType::USize)
97        } else if shape.id == ConstTypeId::of::<i8>() {
98            Some(ScalarType::I8)
99        } else if shape.id == ConstTypeId::of::<i16>() {
100            Some(ScalarType::I16)
101        } else if shape.id == ConstTypeId::of::<i32>() {
102            Some(ScalarType::I32)
103        } else if shape.id == ConstTypeId::of::<i64>() {
104            Some(ScalarType::I64)
105        } else if shape.id == ConstTypeId::of::<i128>() {
106            Some(ScalarType::I128)
107        } else if shape.id == ConstTypeId::of::<isize>() {
108            Some(ScalarType::ISize)
109        } else if shape.id == ConstTypeId::of::<IpAddr>() {
110            Some(ScalarType::IpAddr)
111        } else if shape.id == ConstTypeId::of::<Ipv4Addr>() {
112            Some(ScalarType::Ipv4Addr)
113        } else if shape.id == ConstTypeId::of::<Ipv6Addr>() {
114            Some(ScalarType::Ipv6Addr)
115        } else if shape.id == ConstTypeId::of::<ConstTypeId>() {
116            Some(ScalarType::ConstTypeId)
117        } else {
118            None
119        }
120    }
121}
122
123#[cfg(test)]
124mod tests {
125    use super::*;
126
127    use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};
128
129    use facet_core::Facet;
130
131    /// Simple check to ensure every can be loaded from a shape.
132    #[test]
133    fn test_ensure_try_from_shape() {
134        assert_eq!(
135            ScalarType::Unit,
136            ScalarType::try_from_shape(<()>::SHAPE).unwrap()
137        );
138        assert_eq!(
139            ScalarType::Bool,
140            ScalarType::try_from_shape(bool::SHAPE).unwrap()
141        );
142        assert_eq!(
143            ScalarType::Str,
144            ScalarType::try_from_shape(<&str>::SHAPE).unwrap()
145        );
146        #[cfg(feature = "std")]
147        assert_eq!(
148            ScalarType::String,
149            ScalarType::try_from_shape(String::SHAPE).unwrap()
150        );
151        #[cfg(feature = "std")]
152        assert_eq!(
153            ScalarType::CowStr,
154            ScalarType::try_from_shape(alloc::borrow::Cow::SHAPE).unwrap()
155        );
156        assert_eq!(
157            ScalarType::F32,
158            ScalarType::try_from_shape(f32::SHAPE).unwrap()
159        );
160        assert_eq!(
161            ScalarType::F64,
162            ScalarType::try_from_shape(f64::SHAPE).unwrap()
163        );
164        assert_eq!(
165            ScalarType::U8,
166            ScalarType::try_from_shape(u8::SHAPE).unwrap()
167        );
168        assert_eq!(
169            ScalarType::U16,
170            ScalarType::try_from_shape(u16::SHAPE).unwrap()
171        );
172        assert_eq!(
173            ScalarType::U32,
174            ScalarType::try_from_shape(u32::SHAPE).unwrap()
175        );
176        assert_eq!(
177            ScalarType::U64,
178            ScalarType::try_from_shape(u64::SHAPE).unwrap()
179        );
180        assert_eq!(
181            ScalarType::U128,
182            ScalarType::try_from_shape(u128::SHAPE).unwrap()
183        );
184        assert_eq!(
185            ScalarType::USize,
186            ScalarType::try_from_shape(usize::SHAPE).unwrap()
187        );
188        assert_eq!(
189            ScalarType::I8,
190            ScalarType::try_from_shape(i8::SHAPE).unwrap()
191        );
192        assert_eq!(
193            ScalarType::I16,
194            ScalarType::try_from_shape(i16::SHAPE).unwrap()
195        );
196        assert_eq!(
197            ScalarType::I32,
198            ScalarType::try_from_shape(i32::SHAPE).unwrap()
199        );
200        assert_eq!(
201            ScalarType::I64,
202            ScalarType::try_from_shape(i64::SHAPE).unwrap()
203        );
204        assert_eq!(
205            ScalarType::I128,
206            ScalarType::try_from_shape(i128::SHAPE).unwrap()
207        );
208        assert_eq!(
209            ScalarType::ISize,
210            ScalarType::try_from_shape(isize::SHAPE).unwrap()
211        );
212        #[cfg(feature = "std")]
213        assert_eq!(
214            ScalarType::SocketAddr,
215            ScalarType::try_from_shape(core::net::SocketAddr::SHAPE).unwrap()
216        );
217        assert_eq!(
218            ScalarType::IpAddr,
219            ScalarType::try_from_shape(IpAddr::SHAPE).unwrap()
220        );
221        assert_eq!(
222            ScalarType::Ipv4Addr,
223            ScalarType::try_from_shape(Ipv4Addr::SHAPE).unwrap()
224        );
225        assert_eq!(
226            ScalarType::Ipv6Addr,
227            ScalarType::try_from_shape(Ipv6Addr::SHAPE).unwrap()
228        );
229        assert_eq!(
230            ScalarType::ConstTypeId,
231            ScalarType::try_from_shape(ConstTypeId::SHAPE).unwrap()
232        );
233    }
234}