openapi_type/
impls.rs

1use crate::{ObjectVisitor, OpenapiType, Visitor};
2use indexmap::{IndexMap, IndexSet};
3use serde_json::Value;
4use std::{
5	collections::{BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, VecDeque},
6	ffi::{CStr, CString},
7	num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize}
8};
9
10impl OpenapiType for () {
11	fn visit_type<V: Visitor>(visitor: &mut V) {
12		visitor.visit_unit();
13	}
14}
15
16impl OpenapiType for Value {
17	fn visit_type<V: Visitor>(visitor: &mut V) {
18		visitor.visit_any();
19	}
20}
21
22impl OpenapiType for bool {
23	fn visit_type<V: Visitor>(visitor: &mut V) {
24		visitor.visit_bool();
25	}
26}
27
28////////////////////////////////////////////////////////////////////////////////
29
30macro_rules! int {
31	($($ty:ident($minimum:expr, $byte:expr);)+) => {
32		$(
33			impl OpenapiType for $ty {
34				fn visit_type<V: Visitor>(visitor: &mut V) {
35					visitor.visit_int($byte, $minimum);
36				}
37			}
38		)+
39	}
40}
41
42int! {
43	isize(None, None);
44	i8(None, Some(1));
45	i16(None, Some(2));
46	i32(None, Some(4));
47	i64(None, Some(8));
48	i128(None, Some(16));
49
50	usize(Some(0), None);
51	u8(Some(0), Some(1));
52	u16(Some(0), Some(2));
53	u32(Some(0), Some(4));
54	u64(Some(0), Some(8));
55	u128(Some(0), Some(16));
56
57	NonZeroUsize(Some(1), None);
58	NonZeroU8(Some(1), Some(1));
59	NonZeroU16(Some(1), Some(2));
60	NonZeroU32(Some(1), Some(4));
61	NonZeroU64(Some(1), Some(8));
62	NonZeroU128(Some(1), Some(16));
63}
64
65////////////////////////////////////////////////////////////////////////////////
66
67macro_rules! number {
68	($($ty:ident($byte:expr);)+) => {
69		$(
70			impl OpenapiType for $ty {
71				fn visit_type<V: Visitor>(visitor: &mut V) {
72					visitor.visit_number($byte);
73				}
74			}
75		)+
76	}
77}
78
79number! {
80	f32(Some(4));
81	f64(Some(8));
82}
83
84////////////////////////////////////////////////////////////////////////////////
85
86impl OpenapiType for char {
87	fn visit_type<V: Visitor>(visitor: &mut V) {
88		visitor.visit_char();
89	}
90}
91
92////////////////////////////////////////////////////////////////////////////////
93
94macro_rules! string {
95	($($ty:ident;)+) => {
96		$(
97			impl OpenapiType for $ty {
98				fn visit_type<V: Visitor>(visitor: &mut V) {
99					visitor.visit_string();
100				}
101			}
102		)+
103	}
104}
105
106string! {
107	String;
108	str;
109	CString;
110	CStr;
111}
112
113////////////////////////////////////////////////////////////////////////////////
114
115#[cfg(feature = "uuid08")]
116impl OpenapiType for uuid08::Uuid {
117	fn visit_type<V: Visitor>(visitor: &mut V) {
118		visitor.visit_uuid();
119	}
120}
121
122#[cfg(feature = "uuid1")]
123impl OpenapiType for uuid1::Uuid {
124	fn visit_type<V: Visitor>(visitor: &mut V) {
125		visitor.visit_uuid();
126	}
127}
128
129////////////////////////////////////////////////////////////////////////////////
130
131#[cfg(any(feature = "time03", feature = "chrono04"))]
132macro_rules! date {
133	($($($ty:ident)::+ $(<$arg:ident: $bound:path>)?;)+) => {
134		$(
135			impl$(<$arg: $bound>)? OpenapiType for $($ty)::+$(<$arg>)? {
136				fn visit_type<V: Visitor>(visitor: &mut V) {
137					visitor.visit_date();
138				}
139			}
140		)+
141	}
142}
143
144#[cfg(feature = "time03")]
145date! {
146	time03::Date;
147}
148
149#[cfg(feature = "chrono04")]
150date! {
151	chrono04::Date<T: chrono04::TimeZone>;
152	chrono04::NaiveDate;
153}
154
155////////////////////////////////////////////////////////////////////////////////
156
157#[cfg(any(feature = "time03", feature = "chrono04"))]
158macro_rules! datetime {
159	($($($ty:ident)::+ $(<$arg:ident: $bound:path>)?;)+) => {
160		$(
161			impl$(<$arg: $bound>)? OpenapiType for $($ty)::+$(<$arg>)? {
162				fn visit_type<V: Visitor>(visitor: &mut V) {
163					visitor.visit_datetime();
164				}
165			}
166		)+
167	}
168}
169
170#[cfg(feature = "time03")]
171datetime! {
172	time03::OffsetDateTime;
173	time03::PrimitiveDateTime;
174}
175
176#[cfg(feature = "chrono04")]
177datetime! {
178	chrono04::DateTime<T: chrono04::TimeZone>;
179	chrono04::NaiveDateTime;
180}
181
182////////////////////////////////////////////////////////////////////////////////
183
184impl<T: OpenapiType> OpenapiType for Option<T> {
185	fn visit_type<V: Visitor>(visitor: &mut V) {
186		let v = visitor.visit_option();
187		T::visit_type(v);
188	}
189}
190
191////////////////////////////////////////////////////////////////////////////////
192
193macro_rules! array {
194	($($($ty:ident)::+ $(<$($arg:ident),+>)? ($unique_items:literal, $inner:ident);)+) => {
195		$(
196			impl$(<$($arg),+>)? OpenapiType for $($ty)::+$(<$($arg),+>)?
197			where
198				$inner: OpenapiType
199			{
200				fn visit_type<V: Visitor>(visitor: &mut V) {
201					let v = visitor.visit_array(None, $unique_items);
202					<$inner as OpenapiType>::visit_type(v);
203				}
204			}
205		)+
206	}
207}
208
209type Array<T> = [T];
210
211array! {
212	Array<T>(false, T);
213	LinkedList<T>(false, T);
214	Vec<T>(false, T);
215	VecDeque<T>(false, T);
216
217	BTreeSet<T>(true, T);
218	HashSet<T, S>(true, T);
219	IndexSet<T>(true, T);
220}
221
222impl<T: OpenapiType, const N: usize> OpenapiType for [T; N] {
223	fn visit_type<V: Visitor>(visitor: &mut V) {
224		let v = visitor.visit_array(Some(N), false);
225		T::visit_type(v);
226	}
227}
228
229////////////////////////////////////////////////////////////////////////////////
230
231macro_rules! map {
232	($($($ty:ident)::+ $(<$($arg:ident$(: $bound:path)?),+>)? ($inner:ident);)+) => {
233		$(
234			impl$(<$($arg$(: $bound)?),+>)? OpenapiType for $($ty)::+$(<$($arg),+>)?
235			where
236				$inner: OpenapiType
237			{
238				fn visit_type<Vi: Visitor>(visitor: &mut Vi) {
239					let obj = visitor.visit_object();
240					let v = obj.visit_additional();
241					<$inner as OpenapiType>::visit_type(v);
242				}
243			}
244		)+
245	}
246}
247
248map! {
249	BTreeMap<K, V>(V);
250	HashMap<K, V, S>(V);
251	IndexMap<K, V, S>(V);
252}
253
254#[cfg(feature = "indexmap1")]
255map! {
256	indexmap1::IndexMap<K, V, S>(V);
257}
258
259#[cfg(feature = "linked-hash-map05")]
260map! {
261	linked_hash_map05::LinkedHashMap<K, V, S>(V);
262}