1use serde::{Deserialize, Serialize};
6use serde_json::Value;
7
8#[derive(Debug, Serialize, Deserialize)]
10pub struct CommandResponse {
11 #[serde(rename = "schemaVersion")]
13 pub schema_version: u32,
14
15 #[serde(rename = "type")]
17 pub response_type: String,
18
19 pub ok: bool,
21
22 pub response: Value,
24
25 pub meta: CommandMeta,
27}
28
29#[derive(Debug, Serialize, Deserialize)]
31pub struct CommandMeta {
32 pub profile_name: Option<String>,
33 pub team_id: String,
34 pub user_id: String,
35 pub method: String,
36 pub command: String,
37 #[serde(skip_serializing_if = "Option::is_none")]
38 pub token_type: Option<String>,
39 #[serde(skip_serializing_if = "Option::is_none")]
40 pub idempotency_key: Option<String>,
41 #[serde(skip_serializing_if = "Option::is_none")]
42 pub idempotency_status: Option<String>,
43}
44
45impl CommandResponse {
46 pub fn new(
48 response: Value,
49 profile_name: Option<String>,
50 team_id: String,
51 user_id: String,
52 method: String,
53 command: String,
54 ) -> Self {
55 let ok = response
57 .as_object()
58 .and_then(|obj| obj.get("ok"))
59 .and_then(|v| v.as_bool())
60 .unwrap_or(true);
61
62 let response_type = method.clone();
64
65 Self {
66 schema_version: 1,
67 response_type,
68 ok,
69 response,
70 meta: CommandMeta {
71 profile_name,
72 team_id,
73 user_id,
74 method,
75 command,
76 token_type: None,
77 idempotency_key: None,
78 idempotency_status: None,
79 },
80 }
81 }
82
83 pub fn with_token_type(
85 response: Value,
86 profile_name: Option<String>,
87 team_id: String,
88 user_id: String,
89 method: String,
90 command: String,
91 token_type: Option<String>,
92 ) -> Self {
93 let ok = response
95 .as_object()
96 .and_then(|obj| obj.get("ok"))
97 .and_then(|v| v.as_bool())
98 .unwrap_or(true);
99
100 let response_type = method.clone();
102
103 Self {
104 schema_version: 1,
105 response_type,
106 ok,
107 response,
108 meta: CommandMeta {
109 profile_name,
110 team_id,
111 user_id,
112 method,
113 command,
114 token_type,
115 idempotency_key: None,
116 idempotency_status: None,
117 },
118 }
119 }
120
121 pub fn with_idempotency(mut self, key: String, status: String) -> Self {
123 self.meta.idempotency_key = Some(key);
124 self.meta.idempotency_status = Some(status);
125 self
126 }
127}