1use core::convert::Infallible;
4use std::fmt;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct RouteError {
9 pub status: u16,
10 pub message: String,
11}
12
13impl RouteError {
14 pub fn new(status: u16, message: impl Into<String>) -> Self {
15 Self {
16 status,
17 message: message.into(),
18 }
19 }
20
21 pub fn not_found(message: impl Into<String>) -> Self {
22 Self::new(404, message)
23 }
24
25 pub fn bad_request(message: impl Into<String>) -> Self {
26 Self::new(400, message)
27 }
28
29 pub fn internal(message: impl Into<String>) -> Self {
30 Self::new(500, message)
31 }
32}
33
34impl fmt::Display for RouteError {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 write!(f, "{}: {}", self.status, self.message)
37 }
38}
39
40impl core::error::Error for RouteError {}
41
42#[derive(Debug, Clone, PartialEq, Eq)]
44pub struct Redirect {
45 pub to: String,
46}
47
48impl Redirect {
49 pub fn to(to: impl Into<String>) -> Self {
50 Self { to: to.into() }
51 }
52}
53
54#[derive(Debug, Clone, PartialEq, Eq)]
56pub enum Rejection {
57 Error(RouteError),
58 Redirect(Redirect),
59}
60
61impl From<RouteError> for Rejection {
62 fn from(err: RouteError) -> Self {
63 Self::Error(err)
64 }
65}
66
67impl From<Redirect> for Rejection {
68 fn from(redirect: Redirect) -> Self {
69 Self::Redirect(redirect)
70 }
71}
72
73impl From<Infallible> for Rejection {
74 fn from(never: Infallible) -> Self {
75 match never {}
76 }
77}
78
79pub fn error(status: u16, message: impl Into<String>) -> Rejection {
81 RouteError::new(status, message).into()
82}
83
84pub fn redirect(to: impl Into<String>) -> Rejection {
86 Redirect::to(to).into()
87}