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
99
100
101
102
103
104
105
use crate::response::Response;
pub type Result<T> = std::result::Result<T, Response>;
pub trait ElseResponse {
type Expect;
fn _else<F: FnOnce() -> Response>(self, err: F) -> Result<Self::Expect>;
}
impl<T> ElseResponse for Option<T> {
type Expect = T;
fn _else<F: FnOnce() -> Response>(self, err: F) -> Result<Self::Expect> {
self.ok_or_else(err)
}
}
impl ElseResponse for bool {
type Expect = ();
fn _else<F: FnOnce() -> Response>(self, err: F) -> Result<Self::Expect> {
self.then_some(()).ok_or_else(err)
}
}
pub trait ElseResponseWithErr<E> {
type Expect;
fn _else<F: FnOnce(E) -> Response>(self, err: F) -> Result<Self::Expect>;
}
impl<T, E> ElseResponseWithErr<E> for std::result::Result<T, E> {
type Expect = T;
fn _else<F: FnOnce(E) -> Response>(self, err: F) -> Result<Self::Expect> {
self.map_err(err)
}
}
impl From<std::io::Error> for Response {
fn from(value: std::io::Error) -> Self {
Self::InternalServerError(value.to_string() + ": caused by I/O")
}
}
impl From<serde_json::Error> for Response {
fn from(value: serde_json::Error) -> Self {
Self::InternalServerError(value.to_string() + ": caused by json handling :: " + {
if value.is_data() {
"invalid json data"
} else if value.is_eof() {
"unexpected end of line"
} else if value.is_io() {
"about io"
} else { "wrong json syntax"
}
})
}
}
impl From<std::str::Utf8Error> for Response {
fn from(value: std::str::Utf8Error) -> Self {
Self::InternalServerError(value.to_string() + ": caused by UTF-8 handling")
}
}
#[cfg(any(feature = "postgres", feature = "mysql"))]
impl From<sqlx::Error> for Response {
fn from(value: sqlx::Error) -> Self {
Self::InternalServerError(value.to_string() + ": caused by DB handling")
}
}