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
use serde::{Deserialize, Serialize};
use ts_rs_forge::TS;
#[derive(Debug, Serialize, Deserialize, TS)]
pub struct ApiResponse<T, E = T> {
success: bool,
data: Option<T>,
error_data: Option<E>,
message: Option<String>,
}
impl<T, E> ApiResponse<T, E> {
/// Creates a successful response, with `data` and no message.
pub fn success(data: T) -> Self {
ApiResponse {
success: true,
data: Some(data),
message: None,
error_data: None,
}
}
/// Creates an error response, with `message` and no data.
pub fn error(message: &str) -> Self {
ApiResponse {
success: false,
data: None,
message: Some(message.to_string()),
error_data: None,
}
}
/// Creates an error response, with no `data`, no `message`, but with arbitrary `error_data`.
pub fn error_with_data(data: E) -> Self {
ApiResponse {
success: false,
data: None,
error_data: Some(data),
message: None,
}
}
/// Returns true if the response was successful.
pub fn is_success(&self) -> bool {
self.success
}
/// Consumes the response and returns the data if present.
pub fn into_data(self) -> Option<T> {
self.data
}
/// Returns a reference to the error message if present.
pub fn message(&self) -> Option<&str> {
self.message.as_deref()
}
}