alux_http/method.rs
1//! States which request method an endpoint answers on.
2//!
3//! A method is a value rather than a capability function. An interpreter witnesses every method by
4//! interpreting one value, so declaring a method neither adds an obligation to an interpreter nor
5//! grows the selector algebra.
6
7/// Names the request method a declaration marker denotes.
8pub trait HttpMethodAlg {
9 /// The method this marker selects.
10 const METHOD: HttpMethod;
11}
12
13macro_rules! http_methods {
14 ($($declaration:ident => $marker:ident, $label:literal),+ $(,)?) => {
15 /// Names one HTTP request method.
16 ///
17 /// These are the standard request methods, which every major framework routes natively. A
18 /// surface that answers on an extension method states it as the closest standard method,
19 /// because a method no framework can express is a method no interpreter could witness.
20 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
21 pub enum HttpMethod {
22 $(
23 #[doc = concat!("Selects the `", $label, "` request method.")]
24 $marker,
25 )+
26 }
27
28 impl HttpMethod {
29 /// Lists every request method the specification names, in declaration order.
30 pub const ALL: &'static [Self] = &[$(Self::$marker),+];
31
32 /// Returns the token this method is written as on the wire.
33 pub const fn label(self) -> &'static str {
34 match self {
35 $(Self::$marker => $label,)+
36 }
37 }
38 }
39
40 $(
41 #[doc = concat!("Identifies a `", $label, "` endpoint declaration.")]
42 #[derive(Debug, Default)]
43 pub struct $marker;
44
45 impl HttpMethodAlg for $marker {
46 const METHOD: HttpMethod = HttpMethod::$marker;
47 }
48 )+
49 };
50}
51
52with_http_methods!(http_methods);