vertx_tcp_eventbus_bridge_client_rust/
request.rs

1#![allow(non_snake_case)]
2
3use std::collections::HashMap;
4use serde_json::Value;
5
6/// Message sent from client to vert.x eventbus, can be serialized to json.
7///
8/// The body is a json value provided by [serde_json::value](https://docs.serde.rs/serde_json/value/enum.Value.html).
9/// For more explain for each fields, please visit
10/// # Examples
11/// ```
12/// use vertx_tcp_eventbus_bridge_client_rust::request::Request;
13/// let req = Request::Send {
14///     address: "address".to_string(),
15///     body: json!(...),
16///     headers: None,
17///     replyAddress: None
18/// };
19/// ```
20pub enum Request {
21    Send {
22        address: String,
23        body: Value,
24        headers: Option<Value>,
25        replyAddress: Option<String>,
26    },
27    Publish {
28        address: String,
29        body: Value,
30        headers: Option<Value>,
31        replyAddress: Option<String>,
32    },
33    Register {
34        address: String,
35        headers: Option<Value>,
36    },
37    Unregister {
38        address: String,
39        headers: Option<Value>,
40    },
41    Ping,
42}
43
44impl Request {
45    pub fn to_json(&self) -> Value {
46        match self {
47            Request::Send {
48                address,
49                body,
50                headers,
51                replyAddress
52            } => {
53                let mut v: HashMap<String, Value> = HashMap::new();
54                v.insert("type".to_owned(), json!("send"));
55                v.insert("address".to_owned(), json!(address));
56                v.insert("body".to_owned(), body.clone());
57                if headers.is_some() {
58                    v.insert("headers".to_owned(), headers.clone().unwrap());
59                }
60                if replyAddress.is_some() {
61                    v.insert("replyAddress".to_owned(), json!(replyAddress.clone().unwrap()));
62                }
63                json!(v)
64            }
65            Request::Publish {
66                address,
67                body,
68                headers,
69                replyAddress
70            } => {
71                let mut v: HashMap<String, Value> = HashMap::new();
72                v.insert("type".to_owned(), json!("publish"));
73                v.insert("address".to_owned(), json!(address));
74                v.insert("body".to_owned(), body.clone());
75                if headers.is_some() {
76                    v.insert("headers".to_owned(), headers.clone().unwrap());
77                }
78                if replyAddress.is_some() {
79                    v.insert("replyAddress".to_owned(), json!(replyAddress.clone().unwrap()));
80                }
81                json!(v)
82            }
83            Request::Register {
84                address,
85                headers
86            } => {
87                let mut v: HashMap<String, Value> = HashMap::new();
88                v.insert("type".to_owned(), json!("register"));
89                v.insert("address".to_owned(), json!(address));
90                if headers.is_some() {
91                    v.insert("headers".to_owned(), headers.clone().unwrap());
92                }
93                json!(v)
94            }
95            Request::Unregister {
96                address,
97                headers
98            } => {
99                let mut v: HashMap<String, Value> = HashMap::new();
100                v.insert("type".to_owned(), json!("unregister"));
101                v.insert("address".to_owned(), json!(address));
102                if headers.is_some() {
103                    v.insert("headers".to_owned(), headers.clone().unwrap());
104                }
105                json!(v)
106            }
107            &Request::Ping => {
108                json!({
109                    "type":"ping"
110                })
111            }
112        }
113    }
114}