Skip to main content

windmark/response/
macros.rs

1macro_rules! sync_response {
2  ($($name:ident),*) => {
3    $(
4      /// Trailing commas are not supported at the moment!
5      #[macro_export]
6      macro_rules! $name {
7        ($body:expr /* $(,)? */) => {
8          |_: $crate::context::RouteContext| $crate::response::Response::$name($body)
9        };
10        ($context:ident, $body:expr /* $(,)? */) => {
11          |$context: $crate::context::RouteContext| $crate::response::Response::$name($body)
12        };
13      }
14    )*
15  };
16}
17
18macro_rules! async_response {
19  ($($name:ident),*) => {
20    $(::paste::paste! {
21      /// Trailing commas are not supported at the moment!
22      #[macro_export]
23      macro_rules! [< $name _async >] {
24        ($body:expr /* $(,)? */) => {
25          |_: $crate::context::RouteContext| async { $crate::response::Response::$name($body) }
26        };
27        ($context:ident, $body:expr /* $(,)? */) => {
28          |$context: $crate::context::RouteContext| async { $crate::response::Response::$name($body) }
29        };
30      }
31    })*
32  };
33}
34
35macro_rules! response {
36  ($($name:ident),* $(,)?) => {
37    $(
38      sync_response!($name);
39      async_response!($name);
40    )*
41  };
42}
43
44response!(
45  input,
46  sensitive_input,
47  success,
48  temporary_redirect,
49  permanent_redirect,
50  temporary_failure,
51  server_unavailable,
52  cgi_error,
53  proxy_error,
54  slow_down,
55  permanent_failure,
56  not_found,
57  gone,
58  proxy_refused,
59  bad_request,
60  client_certificate_required,
61  certificate_not_authorised,
62  certificate_not_valid,
63);
64
65#[cfg(feature = "auto-deduce-mime")]
66response!(binary_success_auto);
67
68/// Trailing commas are not supported at the moment!
69#[macro_export]
70macro_rules! binary_success {
71  ($body:expr, $mime:expr) => {
72    |_: $crate::context::RouteContext| {
73      $crate::response::Response::binary_success($body, $mime)
74    }
75  };
76  ($body:expr) => {{
77    #[cfg(not(feature = "auto-deduce-mime"))]
78    compile_error!(
79      "`binary_success` without a MIME type requires the `auto-deduce-mime` \
80       feature to be enabled"
81    );
82
83    |_: $crate::context::RouteContext| {
84      #[cfg(feature = "auto-deduce-mime")]
85      return $crate::response::Response::binary_success_auto($body);
86
87      // Suppress item not found warning
88      #[cfg(not(feature = "auto-deduce-mime"))]
89      $crate::response::Response::binary_success(
90        $body,
91        "application/octet-stream",
92      )
93    }
94  }};
95  ($context:ident, $body:expr, $mime:expr) => {
96    |$context: $crate::context::RouteContext| {
97      $crate::response::Response::binary_success($body, $mime)
98    }
99  };
100  ($context:ident, $body:expr) => {{
101    #[cfg(not(feature = "auto-deduce-mime"))]
102    compile_error!(
103      "`binary_success` without a MIME type requires the `auto-deduce-mime` \
104       feature to be enabled"
105    );
106
107    |$context: $crate::context::RouteContext| {
108      #[cfg(feature = "auto-deduce-mime")]
109      return $crate::response::Response::binary_success_auto($body);
110
111      // Suppress item not found warning
112      #[cfg(not(feature = "auto-deduce-mime"))]
113      $crate::response::Response::binary_success(
114        $body,
115        "application/octet-stream",
116      )
117    }
118  }};
119}