corund_lib/ryz/
err.rs

1use serde::{Deserialize, Serialize};
2
3use super::res::Res;
4
5#[derive(Debug, Serialize, Deserialize)]
6pub struct Error {
7    code: String,
8    msg: String,
9}
10
11impl Default for Error {
12    fn default() -> Self {
13        Self {
14            code: "err".to_string(),
15            msg: "".to_string(),
16        }
17    }
18}
19
20impl Error {
21    pub fn new(code: &str, msg: &str) -> Self {
22        Self {
23            code: code.to_string(),
24            msg: msg.to_string(),
25        }
26    }
27
28    pub fn new_code(code: &str) -> Self {
29        Self {
30            code: code.to_string(),
31            ..Default::default()
32        }
33    }
34
35    pub fn new_msg(msg: &str) -> Self {
36        Self {
37            msg: msg.to_string(),
38            ..Default::default()
39        }
40    }
41}
42
43pub fn res_default<T>() -> Res<T> {
44    Err(Error::default())
45}
46
47pub fn res_code<T>(code: &str) -> Res<T> {
48    Err(Error::new_code(code))
49}
50
51pub fn res_msg<T>(msg: &str) -> Res<T> {
52    Err(Error::new_msg(msg))
53}
54
55pub fn res<T>(code: &str, msg: &str) -> Res<T> {
56    Err(Error::new(code, msg))
57}