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
use crate::tree::Node;
use std::fmt;
#[non_exhaustive]
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum InsertError {
Conflict {
with: String,
},
TooManyParams,
UnnamedParam,
InvalidCatchAll,
}
impl fmt::Display for InsertError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Conflict { with } => {
write!(
f,
"insertion failed due to conflict with previously registered route: {}",
with
)
}
Self::TooManyParams => write!(f, "only one parameter is allowed per path segment"),
Self::UnnamedParam => write!(f, "parameters must be registered with a name"),
Self::InvalidCatchAll => write!(
f,
"catch-all parameters are only allowed at the end of a route"
),
}
}
}
impl std::error::Error for InsertError {}
impl InsertError {
pub(crate) fn conflict<T>(route: &[u8], prefix: &[u8], current: &Node<T>) -> Self {
let mut route = route[..route.len() - prefix.len()].to_owned();
if !route.ends_with(¤t.prefix) {
route.extend_from_slice(¤t.prefix);
}
let mut current = current.children.first();
while let Some(node) = current {
route.extend_from_slice(&node.prefix);
current = node.children.first();
}
InsertError::Conflict {
with: String::from_utf8(route).unwrap(),
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum MatchError {
MissingTrailingSlash,
ExtraTrailingSlash,
NotFound,
}
impl MatchError {
pub(crate) fn unsure(full_path: &[u8]) -> Self {
if full_path[full_path.len() - 1] == b'/' {
MatchError::ExtraTrailingSlash
} else {
MatchError::MissingTrailingSlash
}
}
}
impl fmt::Display for MatchError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let msg = match self {
MatchError::MissingTrailingSlash => "match error: expected trailing slash",
MatchError::ExtraTrailingSlash => "match error: found extra trailing slash",
MatchError::NotFound => "match error: route not found",
};
write!(f, "{}", msg)
}
}
impl std::error::Error for MatchError {}