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
|
use crate::escape::{UnescapedRef, UnescapedRoute};
use crate::tree::{denormalize_params, Node};
use std::fmt;
/// Represents errors that can occur when inserting a new route.
#[non_exhaustive]
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum InsertError {
/// Attempted to insert a path that conflicts with an existing route.
Conflict {
/// The existing route that the insertion is conflicting with.
with: String,
},
/// Only one parameter per route segment is allowed.
///
/// Static segments are also allowed before a parameter, but not after it. For example,
/// `/foo-{bar}` is a valid route, but `/{bar}-foo` is not.
InvalidParamSegment,
/// Parameters must be registered with a valid name and matching braces.
///
/// Note you can use `{{` or `}}` to escape literal brackets.
InvalidParam,
/// Catch-all parameters are only allowed at the end of a path.
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::InvalidParamSegment => {
write!(f, "Only one parameter is allowed per path segment")
}
Self::InvalidParam => write!(f, "Parameters must be registered with a valid 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 {
/// Returns an error for a route conflict with the given node.
///
/// This method attempts to find the full conflicting route.
pub(crate) fn conflict<T>(
route: &UnescapedRoute,
prefix: UnescapedRef<'_>,
current: &Node<T>,
) -> Self {
let mut route = route.clone();
// The route is conflicting with the current node.
if prefix.unescaped() == current.prefix.unescaped() {
denormalize_params(&mut route, ¤t.remapping);
return InsertError::Conflict {
with: String::from_utf8(route.into_unescaped()).unwrap(),
};
}
// Remove the non-matching suffix from the route.
route.truncate(route.len() - prefix.len());
// Add the conflicting prefix.
if !route.ends_with(¤t.prefix) {
route.append(¤t.prefix);
}
// Add the prefixes of any conflicting children.
let mut child = current.children.first();
while let Some(node) = child {
route.append(&node.prefix);
child = node.children.first();
}
// Denormalize any route parameters.
let mut last = current;
while let Some(node) = last.children.first() {
last = node;
}
denormalize_params(&mut route, &last.remapping);
// Return the conflicting route.
InsertError::Conflict {
with: String::from_utf8(route.into_unescaped()).unwrap(),
}
}
}
/// A failed match attempt.
///
/// ```
/// use matchit::{MatchError, Router};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut router = Router::new();
/// router.insert("/home", "Welcome!")?;
/// router.insert("/blog", "Our blog.")?;
///
/// // no routes match
/// if let Err(err) = router.at("/blo") {
/// assert_eq!(err, MatchError::NotFound);
/// }
/// # Ok(())
/// # }
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum MatchError {
/// No matching route was found.
NotFound,
}
impl fmt::Display for MatchError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Matching route not found")
}
}
impl std::error::Error for MatchError {}
|