1use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};
2
3use facet_core::{ConstTypeId, Shape};
4
5#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
7#[non_exhaustive]
8pub enum ScalarType {
9 Unit,
11 Bool,
13 Char,
15 Str,
17 String,
19 CowStr,
21 F32,
23 F64,
25 U8,
27 U16,
29 U32,
31 U64,
33 U128,
35 USize,
37 I8,
39 I16,
41 I32,
43 I64,
45 I128,
47 ISize,
49 SocketAddr,
51 IpAddr,
53 Ipv4Addr,
55 Ipv6Addr,
57 ConstTypeId,
59}
60
61impl ScalarType {
62 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 #[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}