Skip to main content

yew_nav_link/
errors.rs

1//! # NavError
2//!
3//! Error types returned by navigation operations. Use [`NavResult<T>`] as a
4//! convenience alias for `Result<T, NavError>`.
5//!
6//! # Example
7//!
8//! ```rust
9//! use yew_nav_link::NavError;
10//!
11//! fn handle_error(err: &NavError) {
12//!     match err {
13//!         NavError::RouteNotFound => { /* handle */ }
14//!         NavError::InvalidRoute(msg) => { /* handle */ }
15//!         NavError::NavigationCancelled => { /* handle */ }
16//!     }
17//! }
18//! ```
19//!
20//! # Variants
21//!
22//! | Variant | Description |
23//! |---------|-------------|
24//! | `RouteNotFound` | Target route does not match any registered route |
25//! | `InvalidRoute(String)` | Route string could not be parsed |
26//! | `NavigationCancelled` | Navigation was cancelled |
27
28/// Errors that can occur during navigation operations.
29///
30/// Returned by navigation hooks and helper functions when something
31/// goes wrong while resolving or activating a route.
32#[derive(Debug, Clone, PartialEq, Eq)]
33#[must_use]
34pub enum NavError {
35    /// The target route does not match any registered route.
36    RouteNotFound,
37    /// A route string could not be parsed. Contains a diagnostic message.
38    InvalidRoute(String),
39    /// Navigation was cancelled before completion.
40    NavigationCancelled
41}
42
43impl std::fmt::Display for NavError {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        match self {
46            Self::RouteNotFound => write!(f, "route not found"),
47            Self::InvalidRoute(msg) => write!(f, "invalid route: {msg}"),
48            Self::NavigationCancelled => write!(f, "navigation cancelled")
49        }
50    }
51}
52
53impl std::error::Error for NavError {}
54
55impl NavError {
56    /// Creates a [`NavError::RouteNotFound`] error.
57    pub const fn route_not_found() -> Self {
58        Self::RouteNotFound
59    }
60
61    /// Creates a [`NavError::InvalidRoute`] error with the given message.
62    pub fn invalid_route<S: Into<String>>(msg: S) -> Self {
63        Self::InvalidRoute(msg.into())
64    }
65
66    /// Creates a [`NavError::NavigationCancelled`] error.
67    pub const fn navigation_cancelled() -> Self {
68        Self::NavigationCancelled
69    }
70}
71
72/// A convenience alias for `Result<T, NavError>`.
73pub type NavResult<T> = Result<T, NavError>;
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn route_not_found() {
81        let err = NavError::route_not_found();
82        assert_eq!(err, NavError::RouteNotFound);
83    }
84
85    #[test]
86    fn invalid_route() {
87        let err = NavError::invalid_route("bad route");
88        assert_eq!(err, NavError::InvalidRoute("bad route".to_string()));
89    }
90
91    #[test]
92    fn navigation_cancelled() {
93        let err = NavError::navigation_cancelled();
94        assert_eq!(err, NavError::NavigationCancelled);
95    }
96
97    #[test]
98    fn nav_error_display() {
99        assert_eq!(
100            format!("{}", NavError::route_not_found()),
101            "route not found"
102        );
103        assert_eq!(
104            format!("{}", NavError::invalid_route("foo")),
105            "invalid route: foo"
106        );
107        assert_eq!(
108            format!("{}", NavError::navigation_cancelled()),
109            "navigation cancelled"
110        );
111    }
112
113    #[test]
114    fn nav_error_debug() {
115        let err = NavError::route_not_found();
116        let debug_str = format!("{err:?}");
117        assert!(debug_str.contains("RouteNotFound"));
118    }
119
120    #[test]
121    fn nav_error_clone() {
122        let err1 = NavError::route_not_found();
123        let err2 = err1.clone();
124        assert_eq!(err1, err2);
125    }
126
127    #[test]
128    fn nav_error_partial_eq() {
129        assert_eq!(NavError::route_not_found(), NavError::route_not_found());
130        assert_eq!(
131            NavError::navigation_cancelled(),
132            NavError::navigation_cancelled()
133        );
134    }
135
136    #[test]
137    fn nav_result_alias() {
138        let ok: NavResult<i32> = Ok(42);
139        let err: NavResult<i32> = Err(NavError::route_not_found());
140        assert!(ok.is_ok());
141        assert!(err.is_err());
142    }
143}