vertx_tcp_eventbus_bridge_client_rust/
response.rs1#![allow(non_snake_case)]
2
3use serde_json;
4use serde_json::Value;
5
6#[derive(Debug, Clone)]
11pub enum Response {
12 ERR(ErrorType),
14 MESSAGE(ResponseMessageObject),
16 MessageFail(ResponseFailObject),
18 PONG,
20}
21
22#[derive(Debug, Clone)]
24pub enum ErrorType {
25 AccessDenied,
26 AddressRequired,
27 UnknownAddress,
28 UnknownType,
29}
30
31#[derive(Deserialize, Debug, Clone)]
33pub struct ResponseMessageObject {
34 pub address: String,
35 pub body: Value,
36 pub headers: Option<Value>,
37 pub replyAddress: Option<String>,
38 pub send: bool,
39}
40
41#[derive(Deserialize, Debug, Clone)]
43pub struct ResponseFailObject {
44 pub failureCode: i32,
45 pub failureType: String,
46 pub message: String,
47 pub sourceAddress: String,
48}
49
50impl Response {
51 pub fn from_slice(s: &[u8]) -> (Response, String) {
52 let v: Value = serde_json::from_slice(s).unwrap();
53 let typ = v["type"].as_str().expect("type should be string");
54 let addr = v["address"].as_str().expect("address should be string").to_string();
55 match typ.as_ref() {
56 "pong" => (Response::PONG, "".to_string()),
57 "err" => {
58 let err_msg = v["message"].as_str().expect("message should be string");
59 match err_msg.as_ref() {
60 "access_denied" => (Response::ERR(ErrorType::AccessDenied), addr),
61 "address_required" => (Response::ERR(ErrorType::AddressRequired), addr),
62 "unknown_address" => (Response::ERR(ErrorType::UnknownAddress), addr),
63 "unknown_type" => (Response::ERR(ErrorType::UnknownType), addr),
64 _ => {
65 let j: ResponseFailObject = serde_json::from_slice(s).unwrap();
66 (Response::MessageFail(j), addr)
67 }
68 }
69 }
70 "message" => {
71 let j: ResponseMessageObject = serde_json::from_slice(s).unwrap();
72 (Response::MESSAGE(j), addr)
73 }
74 _ => panic!(""),
75 }
76 }
77}