easydonate_api/result/
mod.rs

1use serde::{Deserialize, Serialize};
2
3/// Успешный запрос
4///
5/// ```json
6/// {
7///   "success": true,
8///   "response": {
9///     "message": "hello!"
10///   }
11/// }
12/// ```
13#[derive(Deserialize, Serialize, Debug)]
14pub struct EasySuccessfullResponse<T: Serialize> {
15  success: bool,
16  response: T,
17}
18
19/// Неудачный запрос
20///
21/// ```json
22/// {
23///   "success": false,
24///   "response": "goodbye",
25///   "error_code": 1
26/// }
27/// ```
28#[derive(Deserialize, Serialize, Debug)]
29pub struct EasyErrorResponse {
30  success: bool,
31  response: String,
32  error_code: i64
33}
34
35#[derive(Deserialize, Serialize, Debug)]
36#[serde(untagged)]
37pub enum EasyResponse<T: Serialize> {
38  Error(EasyErrorResponse),
39  Successfull(EasySuccessfullResponse<T>),
40}
41
42impl<T: Serialize> EasyResponse<T> {
43  pub fn result(self) -> anyhow::Result<T> {
44    match self {
45      EasyResponse::Successfull(success) => Ok(success.response),
46      EasyResponse::Error(err) => Err(anyhow::anyhow!("{} (error_code {})", err.response, err.error_code)),
47    }
48  }
49}
50
51pub type EasyResult<T> = Result<T, anyhow::Error>;