arora_websocket/
messages.rs1use crate::method::MethodInfo;
7use crate::slot::SlotInfo;
8use arora_types::value::Value;
9use serde::{Deserialize, Serialize};
10use std::collections::HashMap;
11
12#[derive(Debug, Clone, Deserialize)]
17#[serde(tag = "type", rename_all = "snake_case")]
18pub enum Incoming {
19 SetSlotValues {
23 values: HashMap<String, Value>,
25 },
26
27 GetSlotValues {
31 slots: Vec<String>,
33 },
34
35 ListSlots {
39 #[serde(default)]
41 path: Option<String>,
42 },
43
44 ListMethods {
48 #[serde(default)]
50 path: Option<String>,
51 },
52
53 Invoke {
57 method: String,
59
60 #[serde(default)]
62 args: HashMap<String, Value>,
63
64 #[serde(default)]
66 request_id: Option<String>,
67 },
68}
69
70#[derive(Debug, Clone, Serialize)]
74#[serde(tag = "type", rename_all = "snake_case")]
75pub enum Outgoing {
76 SetSlotValuesResp {
80 success: bool,
82
83 #[serde(skip_serializing_if = "Option::is_none")]
85 message: Option<String>,
86 },
87
88 GetSlotValuesResp {
92 values: HashMap<String, Value>,
94 },
95
96 ListSlotsResp {
100 slots: Vec<SlotInfo>,
102 },
103
104 ListMethodsResp {
108 methods: Vec<MethodInfo>,
110 },
111
112 InvokeResp {
116 success: bool,
118
119 #[serde(skip_serializing_if = "Option::is_none")]
121 request_id: Option<String>,
122
123 #[serde(skip_serializing_if = "Option::is_none")]
125 value: Option<Value>,
126
127 #[serde(skip_serializing_if = "Option::is_none")]
129 message: Option<String>,
130 },
131
132 Error {
136 #[serde(skip_serializing_if = "Option::is_none")]
138 request_id: Option<String>,
139
140 message: String,
142 },
143
144 SlotValuesChanged {
149 values: HashMap<String, Value>,
151 },
152}
153
154#[cfg(test)]
155mod tests {
156 use super::*;
157
158 #[test]
159 fn test_incoming_set_slot_values_deserialize() {
160 let json = r#"{"type": "set_slot_values", "values": {"test/path": {"f64": 0.5}}}"#;
161 let msg: Incoming = serde_json::from_str(json).unwrap();
162 match msg {
163 Incoming::SetSlotValues { values } => {
164 assert!(values.contains_key("test/path"));
165 }
166 _ => panic!("Expected SetSlotValues message"),
167 }
168 }
169
170 #[test]
171 fn test_incoming_get_slot_values_deserialize() {
172 let json = r#"{"type": "get_slot_values", "slots": ["face/mouth", "face/eyes"]}"#;
173 let msg: Incoming = serde_json::from_str(json).unwrap();
174 match msg {
175 Incoming::GetSlotValues { slots } => {
176 assert_eq!(slots, vec!["face/mouth", "face/eyes"]);
177 }
178 _ => panic!("Expected GetSlotValues message"),
179 }
180 }
181
182 #[test]
183 fn test_incoming_list_slots_deserialize() {
184 let json = r#"{"type": "list_slots", "path": "face"}"#;
185 let msg: Incoming = serde_json::from_str(json).unwrap();
186 match msg {
187 Incoming::ListSlots { path } => {
188 assert_eq!(path, Some("face".to_string()));
189 }
190 _ => panic!("Expected ListSlots message"),
191 }
192 }
193
194 #[test]
195 fn test_incoming_invoke_deserialize() {
196 let json = r#"{"type": "invoke", "method": "reset", "request_id": "req-1"}"#;
197 let msg: Incoming = serde_json::from_str(json).unwrap();
198 match msg {
199 Incoming::Invoke {
200 method,
201 args,
202 request_id,
203 } => {
204 assert_eq!(method, "reset");
205 assert!(args.is_empty());
206 assert_eq!(request_id, Some("req-1".to_string()));
207 }
208 _ => panic!("Expected Invoke message"),
209 }
210 }
211
212 #[test]
213 fn test_outgoing_set_slot_values_resp_serialize() {
214 let msg = Outgoing::SetSlotValuesResp {
215 success: true,
216 message: None,
217 };
218 let json = serde_json::to_string(&msg).unwrap();
219 assert!(json.contains(r#""type":"set_slot_values_resp""#));
220 assert!(json.contains(r#""success":true"#));
221 assert!(!json.contains("message"));
222 }
223
224 #[test]
225 fn test_outgoing_invoke_resp_serialize() {
226 let msg = Outgoing::InvokeResp {
227 success: false,
228 request_id: Some("req-1".to_string()),
229 value: None,
230 message: Some("Method not found".to_string()),
231 };
232 let json = serde_json::to_string(&msg).unwrap();
233 assert!(json.contains(r#""type":"invoke_resp""#));
234 assert!(json.contains(r#""request_id":"req-1""#));
235 assert!(json.contains(r#""message":"Method not found""#));
236 }
237}