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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use std::convert::Infallible;
use std::error::Error;
use std::fmt::{self, Display};
use std::str::FromStr;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Hash, Debug)]
#[serde(transparent)]
pub struct ApiErrorId(pub String);
impl FromStr for ApiErrorId {
type Err = Infallible;
fn from_str(s: &str) -> Result<ApiErrorId, Infallible> {
Ok(ApiErrorId(s.to_string()))
}
}
impl Display for ApiErrorId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy)]
#[serde(rename_all = "camelCase")]
pub enum ApiErrorKind {
Client,
Server,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ApiError {
pub id: ApiErrorId,
pub message: String,
pub code: String,
pub kind: ApiErrorKind,
#[serde(default)]
pub info: serde_json::Value,
}
impl Display for ApiError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.kind {
ApiErrorKind::Client => write!(f, "Client error: ")?,
ApiErrorKind::Server => write!(f, "Server error: ")?,
}
write!(f, "{} ({})", self.message, self.code)
}
}
impl Error for ApiError {}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
#[must_use = "this `ApiResult` may be an `Err` variant, which should be handled"]
pub enum ApiResult<T> {
Err {
error: ApiError,
},
Ok(T),
}
impl<T> Into<Result<T, ApiError>> for ApiResult<T> {
fn into(self) -> Result<T, ApiError> {
self.into_result()
}
}
impl<T> ApiResult<T> {
pub fn into_result(self) -> Result<T, ApiError> {
match self {
ApiResult::Err { error } => Err(error),
ApiResult::Ok(x) => Ok(x),
}
}
pub fn ok(self) -> Option<T> {
match self {
ApiResult::Err { .. } => None,
ApiResult::Ok(x) => Some(x),
}
}
pub fn err(self) -> Option<ApiError> {
match self {
ApiResult::Err { error } => Some(error),
ApiResult::Ok(_) => None,
}
}
pub fn is_ok(&self) -> bool {
match self {
ApiResult::Err { .. } => false,
ApiResult::Ok(_) => true,
}
}
pub fn is_err(&self) -> bool {
!self.is_ok()
}
pub fn expect(self, msg: &str) -> T {
match self {
ApiResult::Err { error } => panic!("{}: {:?}", msg, error),
ApiResult::Ok(x) => x,
}
}
pub fn unwrap(self) -> T {
self.expect("called `ApiResult::unwrap()` on an `ApiResult::Err` value")
}
pub fn map<U, F>(self, op: F) -> ApiResult<U>
where
F: FnOnce(T) -> U,
{
match self {
ApiResult::Ok(x) => ApiResult::Ok(op(x)),
ApiResult::Err { error } => ApiResult::Err { error },
}
}
}