1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#![allow(dead_code)]
use std::any::Any;

/// A convenience class to contains RequestParams
#[derive(Debug)]
pub struct RequestAddonCollection {
    inner: Vec<RequestAddon>
}

impl RequestAddonCollection {
    ///
    pub fn new() -> Self {
        RequestAddonCollection {
            inner: Vec::new(),
        }
    }

    /// Retrieve a Ref of a param by its name
    pub fn get(&self, name: &str) -> Option<&RequestAddon> {
        self.inner.iter().find(|p| p.name.eq(name))
    }

    /// Retrieve a RefMut of a param by its name
    pub fn get_mut(&mut self, name: &str) -> Option<&mut RequestAddon> {
        self.inner.as_mut_slice().iter_mut().find(|p| p.name.eq(name))
    }

    /// Add a `RequestAddon` to the collection
    pub fn add(&mut self, p: RequestAddon) {
        self.inner.push(p);
    }

    /// Remove a `RequestAddon` from the collection
    pub fn remove(&mut self, name: &str) {
        if let Some((index, _)) = self.inner.iter().enumerate().find(|t| t.1.name.eq(name)) {
            self.inner.remove(index);
        }
    }
}

use std::ops::{Index, IndexMut};

impl Index<usize> for RequestAddonCollection {
    type Output = RequestAddon;

    fn index(&self, index: usize) -> &RequestAddon {
        &self.inner[index]
    }
}

impl IndexMut<usize> for RequestAddonCollection {
    fn index_mut(&mut self, index: usize) -> &mut RequestAddon {
        &mut self.inner[index]
    }
}

impl<'a> IntoIterator for &'a mut RequestAddonCollection {
    type Item = &'a mut RequestAddon;
    type IntoIter = ::std::slice::IterMut<'a, RequestAddon>;

    fn into_iter(self) -> <Self as IntoIterator>::IntoIter {
        let inner = &mut self.inner;
        inner.into_iter()
    }
}

impl IntoIterator for RequestAddonCollection {
    type Item = RequestAddon;
    type IntoIter = ::std::vec::IntoIter<RequestAddon>;

    fn into_iter(self) -> <Self as IntoIterator>::IntoIter {
        self.inner.into_iter()
    }
}

impl<'a> IntoIterator for &'a RequestAddonCollection {
    type Item = &'a RequestAddon;
    type IntoIter = ::std::slice::Iter<'a, RequestAddon>;

    fn into_iter(self) -> <Self as IntoIterator>::IntoIter {
        let inner = &self.inner;
        inner.into_iter()
    }
}

///
#[derive(Debug)]
pub struct RequestAddon {
    ///
    name: String,
    ///
    data: Box<Any + Send>
}

impl RequestAddon {
    /// Create a new RequestParam
    pub fn new<T>(name: String, data: T) -> Self where T: 'static + Any + Send {
        RequestAddon {
            name,
            data: Box::new(data),
        }
    }

    /// Check if data is of type T
    pub fn is<T: 'static + Any + Send>(&self) -> bool {
        self.data.is::<T>()
    }

    /// Retrieve RequestParam as Ref of type T, or none if the conversion failed
    pub fn borrow_as<T: 'static + Any + Send>(&self) -> Option<&T> {
        self.data.downcast_ref::<T>()
    }

    /// Retrieve RequestParam as RefMut of type T, or none if the conversion failed
    pub fn borrow_mut_as<T: 'static + Any + Send>(&mut self) -> Option<&mut T> {
        self.data.downcast_mut::<T>()
    }

    /// Get the name of the request param
    pub fn name(&self) -> &str {
        self.name.as_ref()
    }
}

impl<S: ToString, T: 'static + Any + Send> From<(S, T)> for RequestAddon {
    fn from(tup: (S, T)) -> Self {
        let (name, data) = tup;
        RequestAddon {
            name: name.to_string(),
            data: Box::new(data),
        }
    }
}

/// Enum representing whether or not a request should continue to be processed be the server
pub enum RequestContinuation {
    /// Next
    Continue,
    /// None
    Stop,
}

/// Trait to convert string type to regular expressions
pub trait ToRegex {
    ///
    fn to_regex(&self) -> Result<::regex::Regex, ::regex::Error>;
    ///
    fn as_str(&self) -> &str;
}

impl<'a> ToRegex for &'a str {
    fn to_regex(&self) -> Result<::regex::Regex, ::regex::Error> {
        ::regex::Regex::new(self)
    }

    fn as_str(&self) -> &str {
        self
    }
}

impl ToRegex for String {
    fn to_regex(&self) -> Result<::regex::Regex, ::regex::Error> {
        ::regex::Regex::new(self.as_str())
    }

    fn as_str(&self) -> &str {
        &self
    }
}

impl ToRegex for ::regex::Regex {
    fn to_regex(&self) -> Result<::regex::Regex, ::regex::Error> {
        Ok(self.clone())
    }

    fn as_str(&self) -> &str {
        self.as_str()
    }
}

#[macro_export]
macro_rules! reg {
    ($str_regex:expr) => {
        $str_regex.to_regex().expect("the parameter passed to reg macro is not a legitimate regex")
    };

}