matchit/
error.rs

1use crate::tree::Node;
2
3use std::fmt;
4
5/// Represents errors that can occur when inserting a new route.
6#[non_exhaustive]
7#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
8pub enum InsertError {
9    /// Attempted to insert a path that conflicts with an existing route.
10    Conflict {
11        /// The existing route that the insertion is conflicting with.
12        with: String,
13    },
14    /// Only one parameter per route segment is allowed.
15    TooManyParams,
16    /// Parameters must be registered with a name.
17    UnnamedParam,
18    /// Catch-all parameters are only allowed at the end of a path.
19    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(&current.prefix) {
49            route.extend_from_slice(&current.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/// A failed match attempt.
65///
66/// ```
67/// use matchit::{MatchError, Router};
68/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
69/// let mut router = Router::new();
70/// router.insert("/home", "Welcome!")?;
71/// router.insert("/blog/", "Our blog.")?;
72///
73/// // a route exists without the trailing slash
74/// if let Err(err) = router.at("/home/") {
75///     assert_eq!(err, MatchError::ExtraTrailingSlash);
76/// }
77///
78/// // a route exists with a trailing slash
79/// if let Err(err) = router.at("/blog") {
80///     assert_eq!(err, MatchError::MissingTrailingSlash);
81/// }
82///
83/// // no routes match
84/// if let Err(err) = router.at("/foobar") {
85///     assert_eq!(err, MatchError::NotFound);
86/// }
87/// # Ok(())
88/// # }
89#[derive(Debug, PartialEq, Eq, Clone, Copy)]
90pub enum MatchError {
91    /// The path was missing a trailing slash.
92    MissingTrailingSlash,
93    /// The path had an extra trailing slash.
94    ExtraTrailingSlash,
95    /// No matching route was found.
96    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 {}