poem_openapi/types/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! Commonly used data types.

mod any;
mod base64_type;
mod binary;
mod error;
mod external;
mod maybe_undefined;
mod string_types;

pub mod multipart;

use std::{borrow::Cow, future::Future, sync::Arc};

pub use any::Any;
pub use base64_type::Base64;
pub use binary::Binary;
pub use error::{ParseError, ParseResult};
pub use maybe_undefined::MaybeUndefined;
use poem::{http::HeaderValue, web::Field as PoemField};
use serde_json::Value;
#[cfg(feature = "email")]
pub use string_types::Email;
#[cfg(feature = "hostname")]
pub use string_types::Hostname;
pub use string_types::Password;

use crate::registry::{MetaSchemaRef, Registry};

/// Represents a OpenAPI type.
pub trait Type: Send + Sync {
    /// If it is `true`, it means that this type is required.
    const IS_REQUIRED: bool;

    /// The raw type used for validator.
    ///
    /// Usually it is `Self`, but the wrapper type is its internal type.
    ///
    /// For example:
    ///
    /// `i32::RawValueType` is `i32`
    /// `Option<i32>::RawValueType` is `i32`.
    type RawValueType;

    /// The raw element type used for validator.
    type RawElementValueType;

    /// Returns the name of this type
    fn name() -> Cow<'static, str>;

    /// Get schema reference of this type.
    fn schema_ref() -> MetaSchemaRef;

    /// Register this type to types registry.
    #[allow(unused_variables)]
    fn register(registry: &mut Registry) {}

    /// Returns a reference to the raw value.
    fn as_raw_value(&self) -> Option<&Self::RawValueType>;

    /// Returns an iterator for traversing the elements.
    fn raw_element_iter<'a>(
        &'a self,
    ) -> Box<dyn Iterator<Item = &'a Self::RawElementValueType> + 'a>;

    /// Returns `true` if this value is empty.
    ///
    /// If the object's field has the `skip_serializing_if_is_empty` attribute,
    /// call this method to test that the value is empty.
    #[inline]
    fn is_empty(&self) -> bool {
        false
    }

    /// Returns `true` if this value is none.
    ///
    /// If the object's field has the `skip_serializing_if_is_none` attribute,
    /// call this method to test that the value is none.
    #[inline]
    fn is_none(&self) -> bool {
        false
    }
}

/// Represents a object type.
pub trait IsObjectType: Type {}

/// Represents a type that can parsing from JSON.
pub trait ParseFromJSON: Sized + Type {
    /// Parse from [`serde_json::Value`].
    fn parse_from_json(value: Option<Value>) -> ParseResult<Self>;

    /// Parse from JSON string.
    fn parse_from_json_string(s: &str) -> ParseResult<Self> {
        let value = serde_json::from_str(s).map_err(|err| ParseError::custom(err.to_string()))?;
        Self::parse_from_json(value)
    }
}

/// Represents a type that can parsing from XML.
pub trait ParseFromXML: Sized + Type {
    /// Parse from [`serde_json::Value`].
    fn parse_from_xml(value: Option<Value>) -> ParseResult<Self>;

    /// Parse from XML string.
    fn parse_from_xml_string(s: &str) -> ParseResult<Self> {
        let value =
            quick_xml::de::from_str(s).map_err(|err| ParseError::custom(err.to_string()))?;
        Self::parse_from_xml(value)
    }
}

/// Represents a type that can parsing from YAML.
pub trait ParseFromYAML: Sized + Type {
    /// Parse from [`serde_json::Value`].
    fn parse_from_yaml(value: Option<Value>) -> ParseResult<Self>;

    /// Parse from YAML string.
    fn parse_from_yaml_string(s: &str) -> ParseResult<Self> {
        let value = serde_yaml::from_str(s).map_err(|err| ParseError::custom(err.to_string()))?;
        Self::parse_from_yaml(value)
    }
}

/// Represents a type that can parsing from parameter. (header, query, path,
/// cookie)
pub trait ParseFromParameter: Sized + Type {
    /// Parse from parameter.
    fn parse_from_parameter(value: &str) -> ParseResult<Self>;

    /// Parse from multiple parameters.
    fn parse_from_parameters<I: IntoIterator<Item = A>, A: AsRef<str>>(
        iter: I,
    ) -> ParseResult<Self> {
        let mut iter = iter.into_iter();
        match iter.next().as_ref().map(|item| item.as_ref()) {
            Some(value) => Self::parse_from_parameter(value),
            None => Err(ParseError::expected_input()),
        }
    }
}

/// Represents a type that can parsing from multipart.
pub trait ParseFromMultipartField: Sized + Type {
    /// Parse from multipart field.
    fn parse_from_multipart(
        field: Option<PoemField>,
    ) -> impl Future<Output = ParseResult<Self>> + Send;

    /// Parse from repeated multipart field.
    fn parse_from_repeated_field(
        self,
        _field: PoemField,
    ) -> impl Future<Output = ParseResult<Self>> + Send {
        async move { Err(ParseError::<Self>::custom("repeated field")) }
    }
}

/// Represents a type that can converted to JSON value.
pub trait ToJSON: Type {
    /// Convert this value to [`Value`].
    fn to_json(&self) -> Option<Value>;

    /// Convert this value to JSON string.
    fn to_json_string(&self) -> String {
        serde_json::to_string(&self.to_json()).unwrap_or_default()
    }
}

/// Represents a type that can converted to XML value.
pub trait ToXML: Type {
    /// Convert this value to [`Value`].
    fn to_xml(&self) -> Option<Value>;

    /// Convert this value to XML string.
    fn to_xml_string(&self) -> String {
        quick_xml::se::to_string(&self.to_xml()).unwrap_or_default()
    }
}

/// Represents a type that can converted to YAML value.
pub trait ToYAML: Type {
    /// Convert this value to [`Value`].
    fn to_yaml(&self) -> Option<Value>;

    /// Convert this value to YAML string.
    fn to_yaml_string(&self) -> String {
        serde_yaml::to_string(&self.to_yaml()).unwrap_or_default()
    }
}

/// Represents a type that can converted to HTTP header.
pub trait ToHeader: Type {
    /// Convert this value to [`HeaderValue`].
    fn to_header(&self) -> Option<HeaderValue>;
}

impl<T: Type> Type for &T {
    const IS_REQUIRED: bool = T::IS_REQUIRED;

    type RawValueType = T::RawValueType;

    type RawElementValueType = T::RawElementValueType;

    fn name() -> Cow<'static, str> {
        T::name()
    }

    fn schema_ref() -> MetaSchemaRef {
        T::schema_ref()
    }

    fn register(registry: &mut Registry) {
        T::register(registry);
    }

    fn as_raw_value(&self) -> Option<&Self::RawValueType> {
        (*self).as_raw_value()
    }

    fn raw_element_iter<'a>(
        &'a self,
    ) -> Box<dyn Iterator<Item = &'a Self::RawElementValueType> + 'a> {
        (*self).raw_element_iter()
    }
}

impl<T: ToJSON> ToJSON for &T {
    fn to_json(&self) -> Option<Value> {
        T::to_json(self)
    }
}

impl<T: ToXML> ToXML for &T {
    fn to_xml(&self) -> Option<Value> {
        T::to_xml(self)
    }
}

impl<T: ToYAML> ToYAML for &T {
    fn to_yaml(&self) -> Option<Value> {
        T::to_yaml(self)
    }
}

impl<T: ToHeader> ToHeader for &T {
    fn to_header(&self) -> Option<HeaderValue> {
        T::to_header(self)
    }
}

impl<T: Type> Type for Arc<T> {
    const IS_REQUIRED: bool = T::IS_REQUIRED;

    type RawValueType = T::RawValueType;

    type RawElementValueType = T::RawElementValueType;

    fn name() -> Cow<'static, str> {
        T::name()
    }

    fn schema_ref() -> MetaSchemaRef {
        T::schema_ref()
    }

    fn register(registry: &mut Registry) {
        T::register(registry);
    }

    fn as_raw_value(&self) -> Option<&Self::RawValueType> {
        self.as_ref().as_raw_value()
    }

    fn raw_element_iter<'a>(
        &'a self,
    ) -> Box<dyn Iterator<Item = &'a Self::RawElementValueType> + 'a> {
        self.as_ref().raw_element_iter()
    }
}

impl<T: ParseFromJSON> ParseFromJSON for Arc<T> {
    fn parse_from_json(value: Option<Value>) -> ParseResult<Self> {
        T::parse_from_json(value)
            .map_err(ParseError::propagate)
            .map(Arc::new)
    }
}

impl<T: ParseFromXML> ParseFromXML for Arc<T> {
    fn parse_from_xml(value: Option<Value>) -> ParseResult<Self> {
        T::parse_from_xml(value)
            .map_err(ParseError::propagate)
            .map(Arc::new)
    }
}

impl<T: ParseFromParameter> ParseFromParameter for Arc<T> {
    fn parse_from_parameter(_value: &str) -> ParseResult<Self> {
        unreachable!()
    }

    fn parse_from_parameters<I: IntoIterator<Item = A>, A: AsRef<str>>(
        iter: I,
    ) -> ParseResult<Self> {
        T::parse_from_parameters(iter)
            .map_err(ParseError::propagate)
            .map(Arc::new)
    }
}

impl<T: ToJSON> ToJSON for Arc<T> {
    fn to_json(&self) -> Option<Value> {
        self.as_ref().to_json()
    }
}

impl<T: ToXML> ToXML for Arc<T> {
    fn to_xml(&self) -> Option<Value> {
        self.as_ref().to_xml()
    }
}

impl<T: ToHeader> ToHeader for Arc<T> {
    fn to_header(&self) -> Option<HeaderValue> {
        self.as_ref().to_header()
    }
}

impl<T: Type> Type for Box<T> {
    const IS_REQUIRED: bool = T::IS_REQUIRED;

    type RawValueType = T::RawValueType;

    type RawElementValueType = T::RawElementValueType;

    fn name() -> Cow<'static, str> {
        T::name()
    }

    fn schema_ref() -> MetaSchemaRef {
        T::schema_ref()
    }

    fn register(registry: &mut Registry) {
        T::register(registry);
    }

    fn as_raw_value(&self) -> Option<&Self::RawValueType> {
        self.as_ref().as_raw_value()
    }

    fn raw_element_iter<'a>(
        &'a self,
    ) -> Box<dyn Iterator<Item = &'a Self::RawElementValueType> + 'a> {
        self.as_ref().raw_element_iter()
    }
}

impl<T: ParseFromJSON> ParseFromJSON for Box<T> {
    fn parse_from_json(value: Option<Value>) -> ParseResult<Self> {
        T::parse_from_json(value)
            .map_err(ParseError::propagate)
            .map(Box::new)
    }
}

impl<T: ParseFromXML> ParseFromXML for Box<T> {
    fn parse_from_xml(value: Option<Value>) -> ParseResult<Self> {
        T::parse_from_xml(value)
            .map_err(ParseError::propagate)
            .map(Box::new)
    }
}

impl<T: ParseFromParameter> ParseFromParameter for Box<T> {
    fn parse_from_parameter(_value: &str) -> ParseResult<Self> {
        unreachable!()
    }

    fn parse_from_parameters<I: IntoIterator<Item = A>, A: AsRef<str>>(
        iter: I,
    ) -> ParseResult<Self> {
        T::parse_from_parameters(iter)
            .map_err(ParseError::propagate)
            .map(Box::new)
    }
}

impl<T: ParseFromMultipartField> ParseFromMultipartField for Box<T> {
    async fn parse_from_multipart(field: Option<PoemField>) -> ParseResult<Self> {
        T::parse_from_multipart(field)
            .await
            .map_err(ParseError::propagate)
            .map(Box::new)
    }

    async fn parse_from_repeated_field(self, field: PoemField) -> ParseResult<Self> {
        T::parse_from_repeated_field(*self, field)
            .await
            .map_err(ParseError::propagate)
            .map(Box::new)
    }
}

impl<T: ToJSON> ToJSON for Box<T> {
    fn to_json(&self) -> Option<Value> {
        self.as_ref().to_json()
    }
}

impl<T: ToXML> ToXML for Box<T> {
    fn to_xml(&self) -> Option<Value> {
        self.as_ref().to_xml()
    }
}

impl<T: ToYAML> ToYAML for Box<T> {
    fn to_yaml(&self) -> Option<Value> {
        self.as_ref().to_yaml()
    }
}

impl<T: ToHeader> ToHeader for Box<T> {
    fn to_header(&self) -> Option<HeaderValue> {
        self.as_ref().to_header()
    }
}

/// Represents an example
pub trait Example {
    /// Returns the example object
    fn example() -> Self;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[allow(clippy::assertions_on_constants)]
    fn arc_type() {
        assert!(Arc::<i32>::IS_REQUIRED);
        assert_eq!(Arc::<i32>::name(), "integer(int32)");
        assert_eq!(Arc::new(100).as_raw_value(), Some(&100));

        let value: Arc<i32> =
            ParseFromJSON::parse_from_json(Some(Value::Number(100.into()))).unwrap();
        assert_eq!(value, Arc::new(100));

        let value: Arc<i32> =
            ParseFromXML::parse_from_xml(Some(Value::Number(100.into()))).unwrap();
        assert_eq!(value, Arc::new(100));

        let value: Arc<i32> =
            ParseFromParameter::parse_from_parameters(std::iter::once("100")).unwrap();
        assert_eq!(value, Arc::new(100));

        assert_eq!(
            ToJSON::to_json(&Arc::new(100)),
            Some(Value::Number(100.into()))
        );

        assert_eq!(
            ToXML::to_xml(&Arc::new(100)),
            Some(Value::Number(100.into()))
        );
    }

    #[test]
    #[allow(clippy::assertions_on_constants)]
    #[allow(unused_allocation)]
    fn box_type() {
        assert!(Box::<i32>::IS_REQUIRED);
        assert_eq!(Box::<i32>::name(), "integer(int32)");
        assert_eq!(Box::new(100).as_raw_value(), Some(&100));

        let value: Box<i32> =
            ParseFromJSON::parse_from_json(Some(Value::Number(100.into()))).unwrap();
        assert_eq!(value, Box::new(100));

        let value: Box<i32> =
            ParseFromJSON::parse_from_json(Some(Value::Number(100.into()))).unwrap();
        assert_eq!(value, Box::new(100));

        let value: Box<i32> =
            ParseFromXML::parse_from_xml(Some(Value::Number(100.into()))).unwrap();
        assert_eq!(value, Box::new(100));

        let value: Box<i32> =
            ParseFromParameter::parse_from_parameters(std::iter::once("100")).unwrap();
        assert_eq!(value, Box::new(100));

        assert_eq!(
            ToJSON::to_json(&Box::new(100)),
            Some(Value::Number(100.into()))
        );

        assert_eq!(
            ToXML::to_xml(&Box::new(100)),
            Some(Value::Number(100.into()))
        );
    }
}