actix_router/
pattern.rs

1/// One or many patterns.
2#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3pub enum Patterns {
4    Single(String),
5    List(Vec<String>),
6}
7
8impl Patterns {
9    pub fn is_empty(&self) -> bool {
10        match self {
11            Patterns::Single(_) => false,
12            Patterns::List(pats) => pats.is_empty(),
13        }
14    }
15}
16
17/// Helper trait for type that could be converted to one or more path patterns.
18pub trait IntoPatterns {
19    fn patterns(&self) -> Patterns;
20}
21
22impl IntoPatterns for String {
23    fn patterns(&self) -> Patterns {
24        Patterns::Single(self.clone())
25    }
26}
27
28impl IntoPatterns for &String {
29    fn patterns(&self) -> Patterns {
30        (*self).patterns()
31    }
32}
33
34impl IntoPatterns for str {
35    fn patterns(&self) -> Patterns {
36        Patterns::Single(self.to_owned())
37    }
38}
39
40impl IntoPatterns for &str {
41    fn patterns(&self) -> Patterns {
42        (*self).patterns()
43    }
44}
45
46impl IntoPatterns for bytestring::ByteString {
47    fn patterns(&self) -> Patterns {
48        Patterns::Single(self.to_string())
49    }
50}
51
52impl IntoPatterns for Patterns {
53    fn patterns(&self) -> Patterns {
54        self.clone()
55    }
56}
57
58impl<T: AsRef<str>> IntoPatterns for Vec<T> {
59    fn patterns(&self) -> Patterns {
60        let mut patterns = self.iter().map(|v| v.as_ref().to_owned());
61
62        match patterns.size_hint() {
63            (1, _) => Patterns::Single(patterns.next().unwrap()),
64            _ => Patterns::List(patterns.collect()),
65        }
66    }
67}
68
69macro_rules! array_patterns_single (($tp:ty) => {
70    impl IntoPatterns for [$tp; 1] {
71        fn patterns(&self) -> Patterns {
72            Patterns::Single(self[0].to_owned())
73        }
74    }
75});
76
77macro_rules! array_patterns_multiple (($tp:ty, $str_fn:expr, $($num:tt) +) => {
78    // for each array length specified in space-separated $num
79    $(
80        impl IntoPatterns for [$tp; $num] {
81            fn patterns(&self) -> Patterns {
82                Patterns::List(self.iter().map($str_fn).collect())
83            }
84        }
85    )+
86});
87
88array_patterns_single!(&str);
89array_patterns_multiple!(&str, |&v| v.to_owned(), 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16);
90
91array_patterns_single!(String);
92array_patterns_multiple!(String, |v| v.clone(), 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16);