1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use roa_core::http;
use std::fmt::{self, Display, Formatter};
#[derive(Debug)]
pub enum RouterError {
MissingVariable(String),
Conflict(Conflict),
}
#[derive(Debug, Eq, PartialEq)]
pub enum Conflict {
Path(String),
Method(String, http::Method),
Variable {
paths: (String, String),
var_name: String,
},
}
impl Display for Conflict {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
match self {
Conflict::Path(path) => f.write_str(&format!("conflict path: `{}`", path)),
Conflict::Method(path, method) => f.write_str(&format!(
"conflict method: `{}` on `{}` is already set",
method, path
)),
Conflict::Variable { paths, var_name } => f.write_str(&format!(
"conflict variable `{}`: between `{}` and `{}`",
var_name, paths.0, paths.1
)),
}
}
}
impl Display for RouterError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
match self {
RouterError::Conflict(conflict) => {
f.write_str(&format!("Conflict! {}", conflict))
}
RouterError::MissingVariable(path) => {
f.write_str(&format!("missing variable on path {}", path))
}
}
}
}
impl From<Conflict> for RouterError {
fn from(conflict: Conflict) -> Self {
RouterError::Conflict(conflict)
}
}
impl std::error::Error for Conflict {}
impl std::error::Error for RouterError {}
#[cfg(test)]
mod tests {
use super::{Conflict, RouterError};
use roa_core::http;
#[test]
fn conflict_to_string() {
assert_eq!(
"conflict path: `/`",
Conflict::Path("/".to_string()).to_string()
);
assert_eq!(
"conflict method: `GET` on `/` is already set",
Conflict::Method("/".to_string(), http::Method::GET).to_string()
);
assert_eq!(
"conflict variable `id`: between `/:id` and `/user/:id`",
Conflict::Variable {
paths: ("/:id".to_string(), "/user/:id".to_string()),
var_name: "id".to_string()
}
.to_string()
);
}
#[test]
fn err_to_string() {
assert_eq!(
"Conflict! conflict path: `/`",
RouterError::Conflict(Conflict::Path("/".to_string())).to_string()
);
assert_eq!(
"missing variable on path /:",
RouterError::MissingVariable("/:".to_string()).to_string()
);
}
}