1use crate::tree::{denormalize_params, 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 last = current;
53 while let Some(node) = last.children.first() {
54 last = node;
55 }
56
57 let mut current = current.children.first();
58 while let Some(node) = current {
59 route.extend_from_slice(&node.prefix);
60 current = node.children.first();
61 }
62
63 denormalize_params(&mut route, &last.param_remapping);
64
65 InsertError::Conflict {
66 with: String::from_utf8(route).unwrap(),
67 }
68 }
69}
70
71#[derive(Debug, PartialEq, Eq, Clone, Copy)]
97pub enum MatchError {
98 MissingTrailingSlash,
100 ExtraTrailingSlash,
102 NotFound,
104}
105
106impl MatchError {
107 pub(crate) fn unsure(full_path: &[u8]) -> Self {
108 if full_path[full_path.len() - 1] == b'/' {
109 MatchError::ExtraTrailingSlash
110 } else {
111 MatchError::MissingTrailingSlash
112 }
113 }
114}
115
116impl fmt::Display for MatchError {
117 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118 let msg = match self {
119 MatchError::MissingTrailingSlash => "match error: expected trailing slash",
120 MatchError::ExtraTrailingSlash => "match error: found extra trailing slash",
121 MatchError::NotFound => "match error: route not found",
122 };
123
124 write!(f, "{}", msg)
125 }
126}
127
128impl std::error::Error for MatchError {}