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 `str`.
14    Str,
15    /// `std::string::String`.
16    #[cfg(feature = "std")]
17    String,
18    /// `std::borrow::Cow<'_, str>`.
19    #[cfg(feature = "std")]
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 `usize`.
34    USize,
35    /// Primitive type `i8`.
36    I8,
37    /// Primitive type `i16`.
38    I16,
39    /// Primitive type `i32`.
40    I32,
41    /// Primitive type `i64`.
42    I64,
43    /// Primitive type `isize`.
44    ISize,
45    /// `core::net::SocketAddr`.
46    #[cfg(feature = "std")]
47    SocketAddr,
48    /// `core::net::IpAddr`.
49    IpAddr,
50    /// `core::net::Ipv4Addr`.
51    Ipv4Addr,
52    /// `core::net::Ipv6Addr`.
53    Ipv6Addr,
54    /// `facet_core::typeid::ConstTypeId`.
55    ConstTypeId,
56}
57
58impl ScalarType {
59    /// Infer the type from a shape definition.
60    pub fn try_from_shape(shape: &'static Shape) -> Option<Self> {
61        #[cfg(feature = "std")]
62        if shape.id == ConstTypeId::of::<String>() {
63            return Some(ScalarType::String);
64        } else if shape.id == ConstTypeId::of::<std::borrow::Cow<'_, str>>() {
65            return Some(ScalarType::CowStr);
66        } else if shape.id == ConstTypeId::of::<std::net::SocketAddr>() {
67            return Some(ScalarType::SocketAddr);
68        }
69
70        if shape.id == ConstTypeId::of::<()>() {
71            Some(Self::Unit)
72        } else if shape.id == ConstTypeId::of::<bool>() {
73            Some(ScalarType::Bool)
74        } else if shape.id == ConstTypeId::of::<&str>() {
75            Some(ScalarType::Str)
76        } else if shape.id == ConstTypeId::of::<f32>() {
77            Some(ScalarType::F32)
78        } else if shape.id == ConstTypeId::of::<f64>() {
79            Some(ScalarType::F64)
80        } else if shape.id == ConstTypeId::of::<u8>() {
81            Some(ScalarType::U8)
82        } else if shape.id == ConstTypeId::of::<u16>() {
83            Some(ScalarType::U16)
84        } else if shape.id == ConstTypeId::of::<u32>() {
85            Some(ScalarType::U32)
86        } else if shape.id == ConstTypeId::of::<u64>() {
87            Some(ScalarType::U64)
88        } else if shape.id == ConstTypeId::of::<usize>() {
89            Some(ScalarType::USize)
90        } else if shape.id == ConstTypeId::of::<i8>() {
91            Some(ScalarType::I8)
92        } else if shape.id == ConstTypeId::of::<i16>() {
93            Some(ScalarType::I16)
94        } else if shape.id == ConstTypeId::of::<i32>() {
95            Some(ScalarType::I32)
96        } else if shape.id == ConstTypeId::of::<i64>() {
97            Some(ScalarType::I64)
98        } else if shape.id == ConstTypeId::of::<isize>() {
99            Some(ScalarType::ISize)
100        } else if shape.id == ConstTypeId::of::<IpAddr>() {
101            Some(ScalarType::IpAddr)
102        } else if shape.id == ConstTypeId::of::<Ipv4Addr>() {
103            Some(ScalarType::Ipv4Addr)
104        } else if shape.id == ConstTypeId::of::<Ipv6Addr>() {
105            Some(ScalarType::Ipv6Addr)
106        } else if shape.id == ConstTypeId::of::<ConstTypeId>() {
107            Some(ScalarType::ConstTypeId)
108        } else {
109            None
110        }
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117
118    use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};
119
120    use facet_core::Facet;
121
122    /// Simple check to ensure every can be loaded from a shape.
123    #[test]
124    fn test_ensure_try_from_shape() {
125        assert_eq!(
126            ScalarType::Unit,
127            ScalarType::try_from_shape(<()>::SHAPE).unwrap()
128        );
129        assert_eq!(
130            ScalarType::Bool,
131            ScalarType::try_from_shape(bool::SHAPE).unwrap()
132        );
133        assert_eq!(
134            ScalarType::Str,
135            ScalarType::try_from_shape(<&str>::SHAPE).unwrap()
136        );
137        #[cfg(feature = "std")]
138        assert_eq!(
139            ScalarType::String,
140            ScalarType::try_from_shape(String::SHAPE).unwrap()
141        );
142        #[cfg(feature = "std")]
143        assert_eq!(
144            ScalarType::CowStr,
145            ScalarType::try_from_shape(std::borrow::Cow::SHAPE).unwrap()
146        );
147        assert_eq!(
148            ScalarType::F32,
149            ScalarType::try_from_shape(f32::SHAPE).unwrap()
150        );
151        assert_eq!(
152            ScalarType::F64,
153            ScalarType::try_from_shape(f64::SHAPE).unwrap()
154        );
155        assert_eq!(
156            ScalarType::U8,
157            ScalarType::try_from_shape(u8::SHAPE).unwrap()
158        );
159        assert_eq!(
160            ScalarType::U16,
161            ScalarType::try_from_shape(u16::SHAPE).unwrap()
162        );
163        assert_eq!(
164            ScalarType::U32,
165            ScalarType::try_from_shape(u32::SHAPE).unwrap()
166        );
167        assert_eq!(
168            ScalarType::U64,
169            ScalarType::try_from_shape(u64::SHAPE).unwrap()
170        );
171        assert_eq!(
172            ScalarType::USize,
173            ScalarType::try_from_shape(usize::SHAPE).unwrap()
174        );
175        assert_eq!(
176            ScalarType::I8,
177            ScalarType::try_from_shape(i8::SHAPE).unwrap()
178        );
179        assert_eq!(
180            ScalarType::I16,
181            ScalarType::try_from_shape(i16::SHAPE).unwrap()
182        );
183        assert_eq!(
184            ScalarType::I32,
185            ScalarType::try_from_shape(i32::SHAPE).unwrap()
186        );
187        assert_eq!(
188            ScalarType::I64,
189            ScalarType::try_from_shape(i64::SHAPE).unwrap()
190        );
191        assert_eq!(
192            ScalarType::ISize,
193            ScalarType::try_from_shape(isize::SHAPE).unwrap()
194        );
195        #[cfg(feature = "std")]
196        assert_eq!(
197            ScalarType::SocketAddr,
198            ScalarType::try_from_shape(std::net::SocketAddr::SHAPE).unwrap()
199        );
200        assert_eq!(
201            ScalarType::IpAddr,
202            ScalarType::try_from_shape(IpAddr::SHAPE).unwrap()
203        );
204        assert_eq!(
205            ScalarType::Ipv4Addr,
206            ScalarType::try_from_shape(Ipv4Addr::SHAPE).unwrap()
207        );
208        assert_eq!(
209            ScalarType::Ipv6Addr,
210            ScalarType::try_from_shape(Ipv6Addr::SHAPE).unwrap()
211        );
212        assert_eq!(
213            ScalarType::ConstTypeId,
214            ScalarType::try_from_shape(ConstTypeId::SHAPE).unwrap()
215        );
216    }
217}