1#[derive(Debug, Clone, PartialEq, Eq)]
33#[must_use]
34pub enum NavError {
35 RouteNotFound,
37 InvalidRoute(String),
39 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 pub const fn route_not_found() -> Self {
58 Self::RouteNotFound
59 }
60
61 pub fn invalid_route<S: Into<String>>(msg: S) -> Self {
63 Self::InvalidRoute(msg.into())
64 }
65
66 pub const fn navigation_cancelled() -> Self {
68 Self::NavigationCancelled
69 }
70}
71
72pub 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}