1use crate::escape::{UnescapedRef, UnescapedRoute};
2use crate::tree::{denormalize_params, Node};
3
4use std::fmt;
5use std::ops::Deref;
6
7#[non_exhaustive]
9#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
10pub enum InsertError {
11 Conflict {
13 with: String,
15 },
16 InvalidParamSegment,
21 InvalidParam,
25 InvalidCatchAll,
27}
28
29impl fmt::Display for InsertError {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 match self {
32 Self::Conflict { with } => {
33 write!(
34 f,
35 "Insertion failed due to conflict with previously registered route: {}",
36 with
37 )
38 }
39 Self::InvalidParamSegment => {
40 write!(f, "Only one parameter is allowed per path segment")
41 }
42 Self::InvalidParam => write!(f, "Parameters must be registered with a valid name"),
43 Self::InvalidCatchAll => write!(
44 f,
45 "Catch-all parameters are only allowed at the end of a route"
46 ),
47 }
48 }
49}
50
51impl std::error::Error for InsertError {}
52
53impl InsertError {
54 pub(crate) fn conflict<T>(
58 route: &UnescapedRoute,
59 prefix: UnescapedRef<'_>,
60 current: &Node<T>,
61 ) -> Self {
62 let mut route = route.clone();
63
64 if prefix.unescaped() == current.prefix.unescaped() {
66 denormalize_params(&mut route, ¤t.remapping);
67 return InsertError::Conflict {
68 with: String::from_utf8(route.into_unescaped()).unwrap(),
69 };
70 }
71
72 route.truncate(route.len() - prefix.len());
74
75 if !route.ends_with(¤t.prefix) {
77 route.append(¤t.prefix);
78 }
79
80 let mut child = current.children.first();
82 while let Some(node) = child {
83 route.append(&node.prefix);
84 child = node.children.first();
85 }
86
87 let mut last = current;
89 while let Some(node) = last.children.first() {
90 last = node;
91 }
92 denormalize_params(&mut route, &last.remapping);
93
94 InsertError::Conflict {
96 with: String::from_utf8(route.into_unescaped()).unwrap(),
97 }
98 }
99}
100
101#[derive(Clone, Debug, Eq, PartialEq)]
105pub struct MergeError(pub(crate) Vec<InsertError>);
106
107impl MergeError {
108 pub fn into_errors(self) -> Vec<InsertError> {
111 self.0
112 }
113}
114
115impl fmt::Display for MergeError {
116 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117 for error in self.0.iter() {
118 writeln!(f, "{}", error)?;
119 }
120
121 Ok(())
122 }
123}
124
125impl std::error::Error for MergeError {}
126
127impl Deref for MergeError {
128 type Target = Vec<InsertError>;
129
130 fn deref(&self) -> &Self::Target {
131 &self.0
132 }
133}
134
135#[derive(Debug, PartialEq, Eq, Clone, Copy)]
151pub enum MatchError {
152 NotFound,
154}
155
156impl fmt::Display for MatchError {
157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158 write!(f, "Matching route not found")
159 }
160}
161
162impl std::error::Error for MatchError {}