topcoat_router/
methods.rs1use std::borrow::Cow;
2
3use http::Method;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum Methods<'a> {
15 Any,
17 Only(&'a [Method]),
19}
20
21#[derive(Debug, Clone, PartialEq, Eq)]
30pub enum OwnedMethods {
31 One(Method),
33 Set(Cow<'static, [Method]>),
35 Any,
37}
38
39impl OwnedMethods {
40 #[must_use]
43 pub fn as_methods(&self) -> Methods<'_> {
44 match self {
45 Self::One(method) => Methods::Only(std::slice::from_ref(method)),
46 Self::Set(methods) => Methods::Only(methods),
47 Self::Any => Methods::Any,
48 }
49 }
50}
51
52impl From<Method> for OwnedMethods {
53 fn from(method: Method) -> Self {
54 Self::One(method)
55 }
56}
57
58impl From<&'static [Method]> for OwnedMethods {
59 fn from(methods: &'static [Method]) -> Self {
60 Self::Set(Cow::Borrowed(methods))
61 }
62}
63
64impl<const N: usize> From<&'static [Method; N]> for OwnedMethods {
65 fn from(methods: &'static [Method; N]) -> Self {
66 Self::Set(Cow::Borrowed(methods))
67 }
68}
69
70impl From<Vec<Method>> for OwnedMethods {
71 fn from(methods: Vec<Method>) -> Self {
72 Self::Set(Cow::Owned(methods))
73 }
74}
75
76impl From<Cow<'static, [Method]>> for OwnedMethods {
77 fn from(methods: Cow<'static, [Method]>) -> Self {
78 Self::Set(methods)
79 }
80}
81
82impl From<Methods<'static>> for OwnedMethods {
83 fn from(methods: Methods<'static>) -> Self {
84 match methods {
85 Methods::Any => Self::Any,
86 Methods::Only(methods) => Self::Set(Cow::Borrowed(methods)),
87 }
88 }
89}
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94
95 #[test]
96 fn a_single_method_converts_without_allocating() {
97 let methods = OwnedMethods::from(Method::GET);
98 assert_eq!(methods, OwnedMethods::One(Method::GET));
99 assert_eq!(methods.as_methods(), Methods::Only(&[Method::GET]));
100 }
101
102 #[test]
103 fn slices_arrays_and_vectors_convert_to_sets() {
104 let expected = Methods::Only(&[Method::GET, Method::POST][..]);
105
106 let slice: &'static [Method] = &[Method::GET, Method::POST];
107 assert_eq!(OwnedMethods::from(slice).as_methods(), expected);
108 assert_eq!(
109 OwnedMethods::from(&[Method::GET, Method::POST]).as_methods(),
110 expected
111 );
112 assert_eq!(
113 OwnedMethods::from(vec![Method::GET, Method::POST]).as_methods(),
114 expected
115 );
116 }
117
118 #[test]
119 fn methods_values_convert_losslessly() {
120 assert_eq!(OwnedMethods::from(Methods::Any), OwnedMethods::Any);
121 assert_eq!(OwnedMethods::Any.as_methods(), Methods::Any);
122 assert_eq!(
123 OwnedMethods::from(Methods::Only(&[Method::PUT])).as_methods(),
124 Methods::Only(&[Method::PUT])
125 );
126 }
127}