celery_rs_core/message_protocol/
properties.rs

1/*
2Properties for the message
3
4Author Andrew Evans
5*/
6
7use amiquip::AmqpProperties;
8use uuid::Uuid;
9
10
11/// Properties
12pub struct Properties{
13    pub correlation_id: String,
14    pub content_type: String,
15    pub content_encoding: String,
16    pub reply_to: Option<String>,
17    pub priority: u8,
18    pub delivery_mode: u8,
19}
20
21
22impl Properties{
23
24    /// convert to amqp properties
25    pub fn convert_to_amqp_properties(&self) -> AmqpProperties{
26        let uid =  Uuid::new_v4();
27        let message_id = format!("{}", uid);
28        let mut props = AmqpProperties::default();
29        props = props.with_message_id(message_id);
30        props = props.with_correlation_id(self.correlation_id.clone());
31        props = props.with_content_type(self.content_type.clone());
32        props = props.with_content_encoding(self.content_encoding.clone());
33        props = props.with_priority(self.priority.clone());
34        props = props.with_delivery_mode(self.delivery_mode);
35        if self.reply_to.is_some() {
36            let rt = self.reply_to.clone().unwrap();
37            props = props.with_reply_to(rt);
38        }
39        props
40    }
41
42    /// Create a new properties
43    pub fn new(correlation_id: String, content_type: String, content_encoding: String, reply_to: Option<String>) -> Properties{
44        Properties{
45            correlation_id: correlation_id,
46            content_type: content_type,
47            content_encoding: content_encoding,
48            reply_to: reply_to,
49            priority: 0,
50            delivery_mode: 2,
51        }
52    }
53}
54
55
56#[cfg(test)]
57mod tests{
58    use crate::message_protocol::properties::Properties;
59
60    #[test]
61    fn should_convert_to_amqp_properties(){
62        let correlation_id = String::from("test_correlation");
63        let content_type = String::from("test_content");
64        let content_encoding = String::from("test_encoding");
65        let props = Properties::new(correlation_id, content_type, content_encoding, None);
66        let aprops = props.convert_to_amqp_properties();
67        assert!(aprops.correlation_id().is_some());
68        assert!(aprops.correlation_id().to_owned().unwrap().eq("test_correlation"));
69        assert!(aprops.content_type().is_some());
70        assert!(aprops.content_type().to_owned().unwrap().eq("test_content"));
71        assert!(aprops.content_encoding().is_some());
72        assert!(aprops.content_encoding().to_owned().unwrap().eq("test_encoding"));
73    }
74
75    #[test]
76    fn should_create_new_properties(){
77        let correlation_id = String::from("test_correlation");
78        let content_type = String::from("test_content");
79        let content_encoding = String::from("test_encoding");
80        let props = Properties::new(correlation_id, content_type, content_encoding, None);
81        assert!(props.correlation_id.eq("test_correlation"));
82        assert!(props.content_type.eq("test_content"));
83        assert!(props.content_encoding.eq("test_encoding"));
84        assert!(props.reply_to.is_none());
85    }
86}