celery_rs_core/message_protocol/
message_body.rs

1/*
2Message body for the broker
3
4Author Andrew Evans
5*/
6
7use serde_json::{Map, Value};
8
9
10/// Message body structure
11pub struct MessageBody{
12    pub chord: Option<String>,
13    pub chain: Option<String>,
14    pub callbacks: Option<Vec<String>>,
15    pub errbacks: Option<Vec<String>>,
16}
17
18
19/// implementation of message body
20impl MessageBody{
21
22    /// covnert to a json map
23    pub fn convert_to_json_map(&self) -> Map<String, Value>{
24        let mut m = Map::new();
25        if self.chord.is_some() {
26            m.insert(String::from("chord"), Value::from(self.chord.clone().unwrap()));
27        }else{
28            m.insert(String::from("chord"), Value::Null);
29        }
30        if self.chain.is_some() {
31            m.insert(String::from("chain"), Value::from(self.chain.clone().unwrap()));
32        }else{
33            m.insert(String::from("chain"), Value::Null);
34        }
35        if self.callbacks.is_some() {
36            m.insert(String::from("callbacks"), Value::from(self.callbacks.clone().unwrap()));
37        }else{
38            m.insert(String::from("callbacks"), Value::Null);
39        }
40        if self.errbacks.is_some() {
41            m.insert(String::from("errbacks"), Value::from(self.errbacks.clone().unwrap()));
42        }else{
43            m.insert(String::from("errbacks"), Value::Null);
44        }
45        m
46    }
47
48    /// create a new message body
49    pub fn new(chord: Option<String>, chain: Option<String>, callbacks: Option<Vec<String>>, errbacks: Option<Vec<String>>) -> MessageBody{
50        MessageBody{
51            chord: chord,
52            chain: chain,
53            callbacks: callbacks,
54            errbacks: errbacks,
55        }
56    }
57}
58
59
60#[cfg(test)]
61mod tests{
62    use crate::message_protocol::message_body::MessageBody;
63
64    #[test]
65    fn should_convert_to_json_map(){
66        let mb = MessageBody::new(Some(String::from("chord")), None, None, None);
67        let cjm = mb.convert_to_json_map();
68        let ch = cjm.get("chord");
69        let cv = ch.unwrap().to_owned();
70        assert!(String::from(cv.as_str().unwrap()).eq("chord"));
71    }
72
73    #[test]
74    fn should_create_new_message_body(){
75        let mb = MessageBody::new(None, None, None, None);
76        assert!(mb.errbacks.is_none());
77    }
78}
79