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
use std::{
borrow::Cow,
iter::FromIterator,
ops::{Deref, DerefMut},
};
#[derive(Debug, Default)]
pub struct Capture<'key, 'value> {
key: Cow<'key, str>,
value: Cow<'value, str>,
}
impl<'key, 'value> Capture<'key, 'value> {
pub fn new(key: impl Into<Cow<'key, str>>, value: impl Into<Cow<'value, str>>) -> Self {
Self {
key: key.into(),
value: value.into(),
}
}
pub fn name(&self) -> &str {
&self.key
}
pub fn value(&self) -> &str {
&self.value
}
pub fn into_owned(self) -> Capture<'static, 'static> {
Capture {
key: self.key.to_string().into(),
value: self.value.to_string().into(),
}
}
}
#[derive(Debug, Default)]
pub struct Captures<'keys, 'values> {
pub(crate) params: Vec<Capture<'keys, 'values>>,
pub(crate) wildcard: Option<Cow<'values, str>>,
}
impl<'keys, 'values> Captures<'keys, 'values> {
pub fn new() -> Self {
Self::default()
}
pub fn into_owned(self) -> Captures<'static, 'static> {
Captures {
params: self.params.into_iter().map(|c| c.into_owned()).collect(),
wildcard: self.wildcard.map(|c| c.to_string().into()),
}
}
pub fn params(&self) -> &[Capture] {
&self.params[..]
}
pub fn set_wildcard(&mut self, wildcard: impl Into<Cow<'values, str>>) {
self.wildcard = Some(wildcard.into());
}
pub fn wildcard(&self) -> Option<&str> {
self.wildcard.as_deref()
}
pub fn get(&self, key: &str) -> Option<&str> {
self.params.iter().find_map(|capture| {
if capture.key == key {
Some(&*capture.value)
} else {
None
}
})
}
pub fn push(&mut self, capture: impl Into<Capture<'keys, 'values>>) {
self.params.push(capture.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<'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> FromIterator<(&'keys str, &'values str)> for Captures<'keys, 'values> {
fn from_iter<T: IntoIterator<Item = (&'keys str, &'values str)>>(iter: T) -> Self {
Self {
params: iter.into_iter().map(Into::into).collect(),
wildcard: None,
}
}
}
impl<'keys, 'values> Extend<(&'keys str, &'values str)> for Captures<'keys, 'values> {
fn extend<T: IntoIterator<Item = (&'keys str, &'values str)>>(&mut self, iter: T) {
for (k, v) in iter {
self.params.push(Capture::new(k, v));
}
}
}