aide/helpers/
use_api.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
use std::{
    marker::PhantomData,
    ops::{Deref, DerefMut},
};

use serde::{Deserialize, Serialize};

use crate::gen::GenContext;
use crate::openapi::{Operation, Response};
use crate::{OperationInput, OperationOutput};

/// helper trait to allow simplified use of [`UseApi`] in responses
pub trait IntoApi {
    /// into [`UseApi`]
    fn into_api<A>(self) -> UseApi<Self, A>
    where
        Self: Sized;
}

impl<T> IntoApi for T {
    fn into_api<A>(self) -> UseApi<Self, A>
    where
        Self: Sized,
    {
        self.into()
    }
}

/// Allows non [`OperationInput`] or [`OperationOutput`] types to be used in aide handlers with the api documentation of [A].
///
/// For types that already implement [`OperationInput`] or [`OperationOutput`] it overrides the documentation with the provided one.
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct UseApi<T, A>(pub T, pub PhantomData<A>);

impl<T, A> UseApi<T, A> {
    /// Unwraps [Self] into its inner type
    pub fn into_inner(self) -> T {
        self.0
    }
}

impl<T, A> Deref for UseApi<T, A> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T, A> DerefMut for UseApi<T, A> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T, A> AsRef<T> for UseApi<T, A> {
    fn as_ref(&self) -> &T {
        &self.0
    }
}

impl<T, A> AsMut<T> for UseApi<T, A> {
    fn as_mut(&mut self) -> &mut T {
        &mut self.0
    }
}

impl<T, A> From<T> for UseApi<T, A> {
    fn from(value: T) -> Self {
        Self(value, Default::default())
    }
}

impl<T, A> OperationInput for UseApi<T, A>
where
    A: OperationInput,
{
    fn operation_input(ctx: &mut GenContext, operation: &mut Operation) {
        A::operation_input(ctx, operation);
    }

    fn inferred_early_responses(
        ctx: &mut GenContext,
        operation: &mut Operation,
    ) -> Vec<(Option<u16>, Response)> {
        A::inferred_early_responses(ctx, operation)
    }
}

impl<T, A> OperationOutput for UseApi<T, A>
where
    A: OperationOutput,
{
    type Inner = A::Inner;

    fn operation_response(ctx: &mut GenContext, operation: &mut Operation) -> Option<Response> {
        A::operation_response(ctx, operation)
    }

    fn inferred_responses(
        ctx: &mut GenContext,
        operation: &mut Operation,
    ) -> Vec<(Option<u16>, Response)> {
        A::inferred_responses(ctx, operation)
    }
}

#[cfg(feature = "axum")]
mod axum {
    use axum::extract::{FromRequest, FromRequestParts};
    use axum::response::{IntoResponse, IntoResponseParts, Response, ResponseParts};
    use axum::{async_trait, body::Body};
    use http::request::Parts;
    use http::Request;

    use crate::UseApi;

    impl<T, A> IntoResponse for UseApi<T, A>
    where
        T: IntoResponse,
    {
        fn into_response(self) -> Response {
            self.0.into_response()
        }
    }

    impl<T, A> IntoResponseParts for UseApi<T, A>
    where
        T: IntoResponseParts,
    {
        type Error = T::Error;

        fn into_response_parts(self, res: ResponseParts) -> Result<ResponseParts, Self::Error> {
            self.0.into_response_parts(res)
        }
    }

    #[async_trait]
    impl<T, A, S> FromRequestParts<S> for UseApi<T, A>
    where
        T: FromRequestParts<S>,
        S: Send + Sync,
    {
        type Rejection = T::Rejection;

        async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
            Ok(Self(
                T::from_request_parts(parts, state).await?,
                Default::default(),
            ))
        }
    }

    #[async_trait]
    impl<T, A, S> FromRequest<S> for UseApi<T, A>
    where
        T: FromRequest<S>,
        S: Send + Sync,
    {
        type Rejection = T::Rejection;

        async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
            Ok(Self(T::from_request(req, state).await?, Default::default()))
        }
    }
}