bitwarden_json/
response.rs

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
use std::error::Error;

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Response<T: Serialize + JsonSchema> {
    /// Whether or not the SDK request succeeded.
    pub success: bool,
    /// A message for any error that may occur. Populated if `success` is false.
    pub error_message: Option<String>,
    /// The response data. Populated if `success` is true.
    pub data: Option<T>,
}

impl<T: Serialize + JsonSchema> Response<T> {
    pub fn new<TErr: Error>(response: Result<T, TErr>) -> Self {
        match response {
            Ok(data) => Self {
                success: true,
                error_message: None,
                data: Some(data),
            },
            Err(err) => Self {
                success: false,
                error_message: Some(err.to_string()),
                data: None,
            },
        }
    }
}

impl Response<()> {
    pub fn error(message: String) -> Self {
        Self {
            success: false,
            error_message: Some(message),
            data: None,
        }
    }
}

pub(crate) trait ResponseIntoString {
    fn into_string(self) -> String;
}

impl<T: Serialize + JsonSchema, E: Error> ResponseIntoString for Result<T, E> {
    fn into_string(self) -> String {
        Response::new(self).into_string()
    }
}

impl<T: Serialize + JsonSchema> ResponseIntoString for Response<T> {
    fn into_string(self) -> String {
        match serde_json::to_string(&self) {
            Ok(ser) => ser,
            Err(e) => {
                let error = Response::error(format!("Failed to serialize Response: {}", e));
                serde_json::to_string(&error).expect("Serialize should be infallible")
            }
        }
    }
}