Skip to content
Clone

refactor(export): refresh data implementations

zhenyi committed
parent 0689490 commit 4add9a0 Browse files

1 file changed

+41 −2

Modified lib/export/data.rs View lib/export/data.rs
@@ -8,7 +8,7 @@
88use std::ops::{Deref, DerefMut};
99use utoipa::ToSchema;
1010
11#[derive(Debug, ToSchema)]
11#[derive(Debug, Serialize, Deserialize, ToSchema)]
1212pub struct NoData;
1313
1414#[derive(Serialize, Deserialize, Debug, ToSchema)]
@@ -36,7 +36,7 @@
3636{
3737 pub fn ok(data: T) -> Self {
3838 AppResponseData {
39 code: 0,
39 code: 200,
4040 message: "success".to_string(),
4141 data: Some(data),
4242 error: None,
@@ -77,6 +77,16 @@
7777 }
7878 }
7979
80 pub fn with_code(mut self, code: i32) -> Self {
81 self.code = code;
82 self
83 }
84
85 pub fn with_message(mut self, message: impl Into<String>) -> Self {
86 self.message = message.into();
87 self
88 }
89
8090 pub fn with_error(mut self, error: impl Into<String>) -> Self {
8191 self.error = Some(error.into());
8292 self
@@ -103,6 +113,35 @@
103113 }
104114}
105115
116impl AppResponseData<NoData> {
117 pub fn empty() -> Self {
118 AppResponseData {
119 code: 200,
120 message: "success".to_string(),
121 data: None,
122 error: None,
123 duration: String::new(),
124 request_id: String::new(),
125 at: String::new(),
126 log_id: String::new(),
127 }
128 }
129
130 pub fn err_plain(code: i32, message: impl Into<String>) -> Self {
131 let msg = message.into();
132 AppResponseData {
133 code,
134 message: msg.clone(),
135 data: None,
136 error: Some(msg),
137 duration: String::new(),
138 request_id: String::new(),
139 at: String::new(),
140 log_id: String::new(),
141 }
142 }
143}
144
106145impl fmt::Display for AppResponseData<()> {
107146 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108147 write!(f, "API error: code={}, message={}", self.code, self.message)