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
use std::borrow::Cow;
use std::marker::PhantomData;
use unicode_xid::UnicodeXID;
use ext::IntoOwned;
use uri::{Origin, UriPart, Path, Query};
use uri::encoding::unsafe_percent_encode;
use self::Error::*;
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Kind {
Static,
Single,
Multi,
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Source {
Path,
Query,
Data,
Unknown,
}
#[derive(Debug, Clone)]
pub struct RouteSegment<'a, P: UriPart> {
pub string: Cow<'a, str>,
pub kind: Kind,
pub name: Cow<'a, str>,
pub index: Option<usize>,
_part: PhantomData<P>,
}
impl<'a, P: UriPart + 'static> IntoOwned for RouteSegment<'a, P> {
type Owned = RouteSegment<'static, P>;
#[inline]
fn into_owned(self) -> Self::Owned {
RouteSegment {
string: IntoOwned::into_owned(self.string),
kind: self.kind,
name: IntoOwned::into_owned(self.name),
index: self.index,
_part: PhantomData
}
}
}
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Error<'a> {
Empty,
Ident(&'a str),
Ignored,
MissingClose,
Malformed,
Uri,
Trailing(&'a str)
}
pub type SResult<'a, P> = Result<RouteSegment<'a, P>, (&'a str, Error<'a>)>;
#[inline]
fn is_ident_start(c: char) -> bool {
('a' <= c && c <= 'z')
|| ('A' <= c && c <= 'Z')
|| c == '_'
|| (c > '\x7f' && UnicodeXID::is_xid_start(c))
}
#[inline]
fn is_ident_continue(c: char) -> bool {
('a' <= c && c <= 'z')
|| ('A' <= c && c <= 'Z')
|| c == '_'
|| ('0' <= c && c <= '9')
|| (c > '\x7f' && UnicodeXID::is_xid_continue(c))
}
fn is_valid_ident(string: &str) -> bool {
let mut chars = string.chars();
match chars.next() {
Some(c) => is_ident_start(c) && chars.all(is_ident_continue),
None => false
}
}
impl<'a, P: UriPart> RouteSegment<'a, P> {
pub fn parse_one(segment: &'a str) -> Result<Self, Error> {
let (string, index) = (segment.into(), None);
if segment.starts_with('<') && segment.ends_with('>') {
let mut kind = Kind::Single;
let mut name = &segment[1..(segment.len() - 1)];
if name.ends_with("..") {
kind = Kind::Multi;
name = &name[..(name.len() - 2)];
}
if name.is_empty() {
return Err(Empty);
} else if !is_valid_ident(name) {
return Err(Ident(name));
} else if name == "_" {
return Err(Ignored);
}
let name = name.into();
return Ok(RouteSegment { string, name, kind, index, _part: PhantomData });
} else if segment.is_empty() {
return Err(Empty);
} else if segment.starts_with('<') && segment.len() > 1
&& !segment[1..].contains('<') && !segment[1..].contains('>') {
return Err(MissingClose);
} else if segment.contains('>') || segment.contains('<') {
return Err(Malformed);
} else if unsafe_percent_encode::<P>(segment) != segment {
return Err(Uri);
}
Ok(RouteSegment {
string, index,
name: segment.into(),
kind: Kind::Static,
_part: PhantomData
})
}
pub fn parse_many(
string: &'a str,
) -> impl Iterator<Item = SResult<P>> {
let mut last_multi_seg: Option<&str> = None;
string.split(P::DELIMITER)
.filter(|s| !s.is_empty())
.enumerate()
.map(move |(i, seg)| {
if let Some(multi_seg) = last_multi_seg {
return Err((seg, Trailing(multi_seg)));
}
let mut parsed = Self::parse_one(seg).map_err(|e| (seg, e))?;
if parsed.kind == Kind::Multi {
last_multi_seg = Some(seg);
}
parsed.index = Some(i);
Ok(parsed)
})
}
}
impl<'a> RouteSegment<'a, Path> {
pub fn parse(uri: &'a Origin) -> impl Iterator<Item = SResult<'a, Path>> {
Self::parse_many(uri.path())
}
}
impl<'a> RouteSegment<'a, Query> {
pub fn parse(uri: &'a Origin) -> Option<impl Iterator<Item = SResult<'a, Query>>> {
uri.query().map(|q| Self::parse_many(q))
}
}