exosphere_core/
operation.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4#[derive(Serialize, Deserialize, Debug, Clone)]
5pub struct Operation {
6    pub operation: String,
7    #[serde(skip_serializing_if = "Option::is_none")]
8    pub id: Option<i64>,
9    #[serde(skip_serializing_if = "Option::is_none")]
10    pub request: Option<Value>,
11}
12
13#[derive(Serialize, Deserialize, Debug, Clone)]
14pub struct OperationReply {
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub id: Option<i64>,
17    pub success: bool,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub results: Option<Value>,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub error: Option<String>,
22}
23
24impl Operation {
25    pub fn new(operation: impl ToString, id: Option<i64>, request: Option<Value>) -> Self {
26        let operation = operation.to_string();
27        Operation {
28            operation,
29            id,
30            request,
31        }
32    }
33
34    pub fn make_result(&self, results: Option<Value>) -> OperationReply {
35        OperationReply::new_result(self.id, results)
36    }
37
38    pub fn make_error(&self, error: impl ToString) -> OperationReply {
39        OperationReply::new_error(self.id, error.to_string())
40    }
41}
42
43impl OperationReply {
44    pub fn new_result(id: Option<i64>, results: Option<Value>) -> Self {
45        Self {
46            id,
47            success: true,
48            results,
49            error: None,
50        }
51    }
52
53    pub fn new_error(id: Option<i64>, error: impl ToString) -> Self {
54        Self {
55            id,
56            success: false,
57            results: None,
58            error: Some(error.to_string()),
59        }
60    }
61}