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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use smartcow::SmartCow;
use std::{
    borrow::Cow,
    iter::FromIterator,
    ops::{Deref, DerefMut},
};

/// An individual key-value pair
#[derive(Debug, Default)]
pub struct Capture<'key, 'value> {
    key: SmartCow<'key>,
    value: SmartCow<'value>,
}

impl<'key, 'value> Capture<'key, 'value> {
    /// Build a new Capture from the provided key and value. Passing a
    /// &str here is preferable, but a String will also work.
    pub fn new(key: impl Into<Cow<'key, str>>, value: impl Into<Cow<'value, str>>) -> Self {
        Self {
            key: key.into().into(),
            value: value.into().into(),
        }
    }

    /// returns the name of this capture
    pub fn name(&self) -> &str {
        &self.key
    }

    /// returns the value of this capture
    pub fn value(&self) -> &str {
        &self.value
    }

    /// transforms this potentially-borrowed Capture into a 'static
    /// capture that can outlive the source data. This allocates new
    /// strings if needed, and should be avoided unless necessary for
    /// a particular application
    pub fn into_owned(self) -> Capture<'static, 'static> {
        Capture {
            key: self.key.into_owned(),
            value: self.value.into_owned(),
        }
    }
}

/// Captured params and a wildcard
#[derive(Debug, Default)]
pub struct Captures<'keys, 'values> {
    pub(crate) params: Vec<Capture<'keys, 'values>>,
    pub(crate) wildcard: Option<SmartCow<'values>>,
}

impl<'keys, 'values> Captures<'keys, 'values> {
    /// Builds a new empty Captures
    pub fn new() -> Self {
        Self::default()
    }

    /// Transforms this Captures into a 'static Captures which can
    /// outlive the source data. This allocates new strings if needed,
    /// and should be avoided unless necessary for a particular
    /// application
    pub fn into_owned(self) -> Captures<'static, 'static> {
        Captures {
            params: self.params.into_iter().map(|c| c.into_owned()).collect(),
            wildcard: self.wildcard.map(SmartCow::into_owned),
        }
    }

    /// returns a slice of captures
    pub fn params(&self) -> &[Capture] {
        &self.params[..]
    }

    /// set the captured wildcard to the provided &str or
    /// String. Prefer passing a &str if available.
    pub fn set_wildcard(&mut self, wildcard: impl Into<Cow<'values, str>>) {
        self.wildcard = Some(wildcard.into().into());
    }

    /// returns what the * wildcard matched, if any
    pub fn wildcard(&self) -> Option<&str> {
        self.wildcard.as_deref()
    }

    /// checks the list of params for a matching key
    pub fn get(&self, key: &str) -> Option<&str> {
        self.params.iter().find_map(|capture| {
            if capture.key == key {
                Some(&*capture.value)
            } else {
                None
            }
        })
    }

    /// Add the provided Capture (or capture-like) to the end of the params
    pub fn push(&mut self, capture: impl Into<Capture<'keys, 'values>>) {
        self.params.push(capture.into());
    }

    /// Combine two captures
    pub fn append(&mut self, mut captures: Captures<'keys, 'values>) {
        self.params.append(&mut captures.params);
        self.wildcard = captures.wildcard;
    }

    /// Iterate over params as str pairs
    pub fn iter(&self) -> Iter<'_, '_, '_> {
        self.into()
    }
}

impl<'keys, 'values> Deref for Captures<'keys, 'values> {
    type Target = Vec<Capture<'keys, 'values>>;

    fn deref(&self) -> &Self::Target {
        &self.params
    }
}

impl<'keys, 'values> DerefMut for Captures<'keys, 'values> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.params
    }
}

impl<'key, 'value> From<(&'key str, &'value str)> for Capture<'key, 'value> {
    fn from(kv: (&'key str, &'value str)) -> Self {
        Self {
            key: kv.0.into(),
            value: kv.1.into(),
        }
    }
}

impl<'pair, 'key: 'pair, 'value: 'pair> From<&'pair (&'key str, &'value str)>
    for Capture<'key, 'value>
{
    fn from(kv: &'pair (&'key str, &'value str)) -> Self {
        Self {
            key: kv.0.into(),
            value: kv.1.into(),
        }
    }
}

impl<'keys, 'values, F> From<F> for Captures<'keys, 'values>
where
    F: IntoIterator<Item = (&'keys str, &'values str)>,
{
    fn from(f: F) -> Self {
        f.into_iter().collect()
    }
}

impl<'keys, 'values, I: Into<Capture<'keys, 'values>>> FromIterator<I>
    for Captures<'keys, 'values>
{
    fn from_iter<T: IntoIterator<Item = I>>(iter: T) -> Self {
        Self {
            params: iter.into_iter().map(Into::into).collect(),
            wildcard: None,
        }
    }
}

impl<'keys, 'values, I: Into<Capture<'keys, 'values>>> Extend<I> for Captures<'keys, 'values> {
    fn extend<T: IntoIterator<Item = I>>(&mut self, iter: T) {
        self.params.extend(iter.into_iter().map(Into::into));
    }
}

impl<'keys, 'values> IntoIterator for Captures<'keys, 'values> {
    type Item = Capture<'keys, 'values>;

    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.params.into_iter()
    }
}

#[derive(Debug)]
pub struct Iter<'captures: 'keys + 'values, 'keys, 'values>(
    std::slice::Iter<'captures, Capture<'keys, 'values>>,
);
impl<'captures: 'keys + 'values, 'keys, 'values> Iterator for Iter<'captures, 'keys, 'values> {
    type Item = (&'keys str, &'values str);

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|c| (c.name(), c.value()))
    }
}

impl<'captures: 'keys + 'values, 'keys, 'values> From<&'captures Captures<'keys, 'values>>
    for Iter<'captures, 'keys, 'values>
{
    fn from(value: &'captures Captures<'keys, 'values>) -> Self {
        Iter(value.params.iter())
    }
}

impl<'captures: 'keys + 'values, 'keys, 'values> IntoIterator
    for &'captures Captures<'keys, 'values>
{
    type Item = (&'keys str, &'values str);

    type IntoIter = Iter<'captures, 'keys, 'values>;

    fn into_iter(self) -> Self::IntoIter {
        self.into()
    }
}