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
use std::ops::{Deref, DerefMut};

use serde::{Deserialize, Serialize};

use crate::{OperationInput, OperationOutput};

/// Allows non [`OperationInput`] or [`OperationOutput`] types to be used in aide handlers with a default empty documentation.
///
/// For types that already implement [`OperationInput`] or [`OperationOutput`] it overrides the documentation and hides it.
/// ```ignore
/// pub async fn my_sqlx_tx_endpoint(
///     NoApi(mut tx): NoApi<Tx<sqlx::Any>> // allows usage of the TX
/// ) -> NoApi<Json<YourResult>> // Hides the API of the return type
/// # {}
/// ```
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct NoApi<T>(pub T);

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

impl<T> Deref for NoApi<T> {
    type Target = T;

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

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

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

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

impl<T> From<T> for NoApi<T> {
    fn from(value: T) -> Self {
        Self(value)
    }
}

impl<T> OperationInput for NoApi<T> {}

impl<T> OperationOutput for NoApi<T> {
    type Inner = ();
}

#[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::NoApi;

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

    impl<T> IntoResponseParts for NoApi<T>
    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, S> FromRequestParts<S> for NoApi<T>
    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?))
        }
    }

    #[async_trait]
    impl<T, S> FromRequest<S> for NoApi<T>
    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?))
        }
    }
}