rust_api_kit/http/client/
macros.rs

1#[macro_export]
2macro_rules! define_http_routes {
3    (
4        $(
5            group (
6                $(auth $auth_ty:ty;)?
7                error $unexpected:ty;
8
9                $(
10                    $method:ident $path:literal $req:ty => $res:ty | $err:ty;
11                )*
12            );
13        )*
14    ) => {
15        $(
16            $crate::define_http_routes!(@impl_group $(auth $auth_ty;)? $unexpected; $(($method, $path, $req, $res, $err))*);
17        )*
18    };
19
20    (@impl_group auth $auth_ty:ty; $unexpected:ty; $(($method:ident, $path:literal, $req:ty, $res:ty, $err:ty))*) => {
21        $(
22            impl $crate::http::client::HttpRequest<$res, $err, $unexpected> for $req {
23                const ENDPOINT: &'static str = concat!("/", $path);
24                const METHOD: $crate::http::client::RequestMethod = $crate::http::client::RequestMethod::$method;
25            }
26
27            impl Into<$crate::http::client::Response<$res, $err, $unexpected>> for $res
28            {
29                fn into(
30                    self,
31                ) -> $crate::http::client::Response<$res, $err, $unexpected>
32                {
33                    $crate::http::client::Response::Ok(self)
34                }
35            }
36
37            impl Into<$crate::http::client::Response<$res, $err,$unexpected>> for $err
38            {
39                fn into(
40                    self,
41                ) -> $crate::http::client::Response<$res, $err,$unexpected>
42                {
43                    $crate::http::client::Response::Error(self)
44                }
45            }
46
47        impl Into<$crate::http::client::Response<$res, $err, $unexpected>> for $unexpected
48        {
49            fn into(
50                self,
51            ) -> $crate::http::client::Response<$res, $err, $unexpected>
52            {
53                $crate::http::client::Response::UnexpectedError(self)
54            }
55        }
56
57
58        impl $crate::http::client::AuthenticatedHttpRequest<$auth_ty> for $req {}
59        )*
60    };
61
62    (@impl_group $unexpected:ty; $(($method:ident, $path:literal, $req:ty, $res:ty, $err:ty))*) => {
63        $(
64            impl $crate::http::client::HttpRequest<$res, $err, $unexpected> for $req {
65                const ENDPOINT: &'static str = concat!("/", $path);
66                const METHOD: $crate::http::client::RequestMethod = $crate::http::client::RequestMethod::$method;
67            }
68
69            impl Into<$crate::http::client::Response<$res, $err, $unexpected>> for $res
70            {
71                fn into(
72                    self,
73                ) -> $crate::http::client::Response<$res, $err, $unexpected>
74                {
75                    $crate::http::client::Response::Ok(self)
76                }
77            }
78
79            impl Into<$crate::http::client::Response<$res, $err,$unexpected>> for $err
80            {
81                fn into(
82                    self,
83                ) -> $crate::http::client::Response<$res, $err,$unexpected>
84                {
85                    $crate::http::client::Response::Error(self)
86                }
87            }
88
89        impl Into<$crate::http::client::Response<$res, $err, $unexpected>> for $unexpected
90        {
91            fn into(
92                self,
93            ) -> $crate::http::client::Response<$res, $err, $unexpected>
94            {
95                $crate::http::client::Response::UnexpectedError(self)
96            }
97        }
98        )*
99    };
100}