1use crate::tree::Node;
2
3use std::fmt;
4
5#[non_exhaustive]
7#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
8pub enum InsertError {
9 Conflict {
11 with: String,
13 },
14 TooManyParams,
16 UnnamedParam,
18 InvalidCatchAll,
20}
21
22impl fmt::Display for InsertError {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 match self {
25 Self::Conflict { with } => {
26 write!(
27 f,
28 "insertion failed due to conflict with previously registered route: {}",
29 with
30 )
31 }
32 Self::TooManyParams => write!(f, "only one parameter is allowed per path segment"),
33 Self::UnnamedParam => write!(f, "parameters must be registered with a name"),
34 Self::InvalidCatchAll => write!(
35 f,
36 "catch-all parameters are only allowed at the end of a route"
37 ),
38 }
39 }
40}
41
42impl std::error::Error for InsertError {}
43
44impl InsertError {
45 pub(crate) fn conflict<T>(route: &[u8], prefix: &[u8], current: &Node<T>) -> Self {
46 let mut route = route[..route.len() - prefix.len()].to_owned();
47
48 if !route.ends_with(¤t.prefix) {
49 route.extend_from_slice(¤t.prefix);
50 }
51
52 let mut current = current.children.first();
53 while let Some(node) = current {
54 route.extend_from_slice(&node.prefix);
55 current = node.children.first();
56 }
57
58 InsertError::Conflict {
59 with: String::from_utf8(route).unwrap(),
60 }
61 }
62}
63
64#[derive(Debug, PartialEq, Eq, Clone, Copy)]
90pub enum MatchError {
91 MissingTrailingSlash,
93 ExtraTrailingSlash,
95 NotFound,
97}
98
99impl MatchError {
100 pub(crate) fn unsure(full_path: &[u8]) -> Self {
101 if full_path[full_path.len() - 1] == b'/' {
102 MatchError::ExtraTrailingSlash
103 } else {
104 MatchError::MissingTrailingSlash
105 }
106 }
107}
108
109impl fmt::Display for MatchError {
110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111 let msg = match self {
112 MatchError::MissingTrailingSlash => "match error: expected trailing slash",
113 MatchError::ExtraTrailingSlash => "match error: found extra trailing slash",
114 MatchError::NotFound => "match error: route not found",
115 };
116
117 write!(f, "{}", msg)
118 }
119}
120
121impl std::error::Error for MatchError {}