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
mod from_data_impls;
mod from_form_multi_param_impls;
mod from_form_param_impls;
mod from_param_impls;
mod from_request_impls;

use super::gen::OpenApiGenerator;
use super::Result;
extern crate okapi;
use okapi::openapi3::{Parameter, RequestBody, SecurityRequirement, SecurityScheme};

/// Expose this to the public to be use when manualy implementing a
/// [Form Guard](https://api.rocket.rs/master/rocket/form/trait.FromForm.html).
pub use from_form_multi_param_impls::get_nested_form_parameters;

/// This trait means that the implementer can be used as a `FromData` request guard, and that this
/// can also be documented.
pub trait OpenApiFromData<'r>: rocket::data::FromData<'r> {
    /// Return a `RequestBody` containing the information required to document the `FromData` for
    /// implementer.
    fn request_body(gen: &mut OpenApiGenerator) -> Result<RequestBody>;
}

/// This trait means that the implementer can be used as a `FromParam` request guard, and that this
/// can also be documented.
pub trait OpenApiFromParam<'r>: rocket::request::FromParam<'r> {
    /// Return a `RequestBody` containing the information required to document the `FromParam` for
    /// implementer. Path paremeters.
    fn path_parameter(gen: &mut OpenApiGenerator, name: String) -> Result<Parameter>;
}

/// This trait means that the implementer can be used as a `FromSegments` request guard, and that
/// this can also be documented.
pub trait OpenApiFromSegments<'r>: rocket::request::FromSegments<'r> {
    /// Return a `RequestBody` containing the information required to document the `FromSegments`
    /// for implementer.
    fn path_multi_parameter(gen: &mut OpenApiGenerator, name: String) -> Result<Parameter>;
}

/// This trait means that the implementer can be used as a `FromFormField` request guard, and that
/// this can also be documented.
pub trait OpenApiFromFormField<'r>: rocket::form::FromForm<'r> {
    /// Return a `RequestBody` containing the information required to document the `FromFormField`
    /// for implementer.
    fn form_parameter(
        gen: &mut OpenApiGenerator,
        name: String,
        required: bool,
    ) -> Result<Parameter>;
}

/// This trait means that the implementer can be used as a `FromForm` request guard, and that this
/// can also be documented.
pub trait OpenApiFromForm<'r>: rocket::form::FromForm<'r> {
    /// Return a `RequestBody` containing the information required to document the `FromForm` for
    /// implementer.
    fn form_multi_parameter(
        gen: &mut OpenApiGenerator,
        name: String,
        required: bool,
    ) -> Result<Vec<Parameter>>;
}

/// Commonly the items in the request header can be parameters, or authorization methods in rocket,
/// this item will let the implementer choose what they are
pub enum RequestHeaderInput {
    /// This request header requires no input anywhere
    None,
    /// Parameter input to the path, can be used to parse values from the header for example
    Parameter(Parameter),
    /// the request guard implements a security scheme
    Security((SecurityScheme, SecurityRequirement)),
}

impl Into<RequestHeaderInput> for Parameter {
    fn into(self) -> RequestHeaderInput {
        RequestHeaderInput::Parameter(self)
    }
}

/// This will let the request guards add to the openapi spec
pub trait OpenApiFromRequest<'a>: rocket::request::FromRequest<'a> {
    /// Return a parameter for items that are found in the function call, but are not found
    /// anywhere in the path definition by rocket, defaults to a none implementation for simplicity
    fn request_input(_gen: &mut OpenApiGenerator, _name: String) -> Result<RequestHeaderInput> {
        Ok(RequestHeaderInput::None)
    }
}