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
use super::OpenApiFromFormValue;
use crate::gen::OpenApiGenerator;
use okapi::openapi3::*;
use std::result::Result as StdResult;

type Result = crate::Result<Parameter>;

macro_rules! impl_from_query_param {
    ($ty: path) => {
        impl<'r> OpenApiFromFormValue<'r> for $ty {
            fn query_parameter(gen: &mut OpenApiGenerator, name: String, required: bool) -> Result {
                let schema = gen.json_schema::<$ty>();
                Ok(Parameter {
                    name,
                    location: "query".to_owned(),
                    description: None,
                    required,
                    deprecated: false,
                    allow_empty_value: false,
                    value: ParameterValue::Schema {
                        style: None,
                        explode: None,
                        allow_reserved: false,
                        schema,
                        example: None,
                        examples: None,
                    },
                    extensions: Default::default(),
                })
            }
        }
    };
}

impl_from_query_param!(f32);
impl_from_query_param!(f64);
impl_from_query_param!(isize);
impl_from_query_param!(i8);
impl_from_query_param!(i16);
impl_from_query_param!(i32);
impl_from_query_param!(i64);
impl_from_query_param!(i128);
impl_from_query_param!(usize);
impl_from_query_param!(u8);
impl_from_query_param!(u16);
impl_from_query_param!(u32);
impl_from_query_param!(u64);
impl_from_query_param!(u128);
impl_from_query_param!(bool);
impl_from_query_param!(String);
// Cow<> does not implement FromFormValue
// impl_from_query_param!(std::borrow::Cow<'r, str>);

// OpenAPI specification does not support optional path params, so we leave `required` as true,
// even for Options and Results.
impl<'r, T: OpenApiFromFormValue<'r>> OpenApiFromFormValue<'r> for StdResult<T, T::Error> {
    fn query_parameter(gen: &mut OpenApiGenerator, name: String, required: bool) -> Result {
        T::query_parameter(gen, name, required)
    }
}

impl<'r, T: OpenApiFromFormValue<'r>> OpenApiFromFormValue<'r> for Option<T> {
    fn query_parameter(gen: &mut OpenApiGenerator, name: String, _required: bool) -> Result {
        T::query_parameter(gen, name, false)
    }
}