1use crate::{MapKind, Node, NodeType, Schemaifier};
2use alloc::{
3 borrow::{Cow, ToOwned},
4 boxed::Box,
5 collections::BTreeMap,
6 string::String,
7 vec,
8 vec::Vec,
9};
10
11impl Schemaifier for () {
12 #[inline]
13 fn visit_schema(visitor: &mut crate::SchemaVisitor) -> crate::DefinitionReference {
14 visitor.insert(
15 Self::id(),
16 Node {
17 name: Cow::Borrowed("Unit"),
18 description: None,
19 value: NodeType::Unit,
20 },
21 )
22 }
23}
24
25impl Schemaifier for str {
26 #[inline]
27 fn visit_schema(visitor: &mut crate::SchemaVisitor) -> crate::DefinitionReference {
28 visitor.insert(
29 Self::id(),
30 Node {
31 name: Cow::Borrowed("str"),
32 description: None,
33 value: NodeType::String,
34 },
35 )
36 }
37}
38
39impl Schemaifier for String {
40 #[inline]
41 fn visit_schema(visitor: &mut crate::SchemaVisitor) -> crate::DefinitionReference {
42 visitor.insert(
43 Self::id(),
44 Node {
45 name: Cow::Borrowed("String"),
46 description: None,
47 value: NodeType::String,
48 },
49 )
50 }
51}
52
53macro_rules! impl_integer {
54 ($($t:ty),+) => {
55 $(
56 impl Schemaifier for $t {
57 #[inline]
58 fn visit_schema(visitor: &mut crate::SchemaVisitor) -> crate::DefinitionReference {
59 visitor.insert(Self::id(), Node {
60 name: Cow::Borrowed(stringify!($t)),
61 description: None,
62 value: NodeType::Integer {
63 signed: <$t>::MIN != 0,
64 precision: <$t>::BITS as u64,
65 },
66 })
67 }
68 }
69 )+
70 };
71}
72
73impl_integer!(u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, usize, isize);
74
75impl Schemaifier for f32 {
76 #[inline]
77 fn visit_schema(visitor: &mut crate::SchemaVisitor) -> crate::DefinitionReference {
78 visitor.insert(
79 Self::id(),
80 Node {
81 name: Cow::Borrowed("f32"),
82 description: None,
83 value: NodeType::Float,
84 },
85 )
86 }
87}
88
89impl Schemaifier for f64 {
90 #[inline]
91 fn visit_schema(visitor: &mut crate::SchemaVisitor) -> crate::DefinitionReference {
92 visitor.insert(
93 Self::id(),
94 Node {
95 name: Cow::Borrowed("f64"),
96 description: None,
97 value: NodeType::Double,
98 },
99 )
100 }
101}
102
103impl Schemaifier for bool {
104 #[inline]
105 fn visit_schema(visitor: &mut crate::SchemaVisitor) -> crate::DefinitionReference {
106 visitor.insert(
107 Self::id(),
108 Node {
109 name: Cow::Borrowed("bool"),
110 description: None,
111 value: NodeType::Boolean,
112 },
113 )
114 }
115}
116
117impl<T> Schemaifier for Box<T>
118where
119 T: Schemaifier + ?Sized,
120{
121 #[inline]
122 fn visit_schema(visitor: &mut crate::SchemaVisitor) -> crate::DefinitionReference {
123 let node = Node {
124 name: Cow::Borrowed(std::any::type_name::<Self>()),
125 description: None,
126 value: NodeType::Boxed {
127 inner: T::visit_schema(visitor),
128 },
129 };
130
131 visitor.insert(Self::id(), node)
132 }
133}
134
135impl<T> Schemaifier for Vec<T>
136where
137 T: Schemaifier,
138{
139 #[inline]
140 fn visit_schema(visitor: &mut crate::SchemaVisitor) -> crate::DefinitionReference {
141 let node = Node {
142 name: Cow::Borrowed(std::any::type_name::<Self>()),
143 description: None,
144 value: NodeType::Array {
145 items: T::visit_schema(visitor),
146 },
147 };
148
149 visitor.insert(Self::id(), node)
150 }
151}
152
153macro_rules! all_the_tuples {
154 ($($($n:ident),+);+$(;)?) => {
155 $(
156 impl<$($n: Schemaifier),+> Schemaifier for ($($n,)+) {
157 #[inline]
158 fn visit_schema(visitor: &mut crate::SchemaVisitor) -> crate::DefinitionReference {
159 let node = Node {
160 name: Cow::Borrowed(std::any::type_name::<Self>()),
161 description: None,
162 value: NodeType::Tuple {
163 items: vec![
164 $(<$n as Schemaifier>::visit_schema(visitor)),+
165 ],
166 },
167 };
168
169 visitor.insert(Self::id(), node)
170 }
171 }
172 )+
173 };
174}
175
176all_the_tuples! {
179 A;
180 A, B;
181 A, B, C;
182 A, B, C, D;
183 A, B, C, D, E;
184 A, B, C, D, E, F;
185 A, B, C, D, E, F, G;
186 A, B, C, D, E, F, G, H;
187 A, B, C, D, E, F, G, H, I;
188 A, B, C, D, E, F, G, H, I, J;
189 A, B, C, D, E, F, G, H, I, J, K;
190 A, B, C, D, E, F, G, H, I, J, K, L;
191 A, B, C, D, E, F, G, H, I, J, K, L, M;
192 A, B, C, D, E, F, G, H, I, J, K, L, M, N;
193 A, B, C, D, E, F, G, H, I, J, K, L, M, N, O;
194 A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P;
195}
196
197impl<T> Schemaifier for Option<T>
198where
199 T: Schemaifier,
200{
201 #[inline]
202 fn visit_schema(visitor: &mut crate::SchemaVisitor) -> crate::DefinitionReference {
203 let node = Node {
204 name: Cow::Borrowed(std::any::type_name::<Self>()),
205 description: None,
206 value: NodeType::Optional {
207 inner: T::visit_schema(visitor),
208 },
209 };
210
211 visitor.insert(Self::id(), node)
212 }
213}
214
215impl<K, V> Schemaifier for BTreeMap<K, V>
216where
217 K: Schemaifier,
218 V: Schemaifier,
219{
220 #[inline]
221 fn visit_schema(visitor: &mut crate::SchemaVisitor) -> crate::DefinitionReference {
222 let node = Node {
223 name: Cow::Borrowed(std::any::type_name::<Self>()),
224 description: None,
225 value: NodeType::Map {
226 kind: MapKind::BTree,
227 key: K::visit_schema(visitor),
228 value: V::visit_schema(visitor),
229 },
230 };
231
232 visitor.insert(Self::id(), node)
233 }
234}
235
236#[cfg(feature = "std")]
237impl<K, V, S> Schemaifier for std::collections::HashMap<K, V, S>
238where
239 K: Schemaifier,
240 V: Schemaifier,
241{
242 #[inline]
243 fn visit_schema(visitor: &mut crate::SchemaVisitor) -> crate::DefinitionReference {
244 let node = Node {
245 name: Cow::Borrowed(std::any::type_name::<Self>()),
246 description: None,
247 value: NodeType::Map {
248 kind: MapKind::Hash,
249 key: K::visit_schema(visitor),
250 value: V::visit_schema(visitor),
251 },
252 };
253
254 visitor.insert(Self::id(), node)
255 }
256}
257
258impl<T> Schemaifier for &T
259where
260 T: Schemaifier + ?Sized,
261{
262 #[inline]
263 fn visit_schema(visitor: &mut crate::SchemaVisitor) -> crate::DefinitionReference {
264 T::visit_schema(visitor)
265 }
266}
267
268impl<T> Schemaifier for Cow<'_, T>
269where
270 T: Schemaifier + ToOwned + ?Sized,
271{
272 #[inline]
273 fn visit_schema(visitor: &mut crate::SchemaVisitor) -> crate::DefinitionReference {
274 T::visit_schema(visitor)
275 }
276}