amqpr_codec/frame/content_header/
mod.rs

1pub mod decoder;
2pub mod encoder;
3
4pub use self::decoder::decode_payload;
5pub use self::encoder::encode_payload;
6
7use args::{AmqpString, FieldArgument};
8
9use std::collections::HashMap;
10
11
12#[derive(PartialEq, Clone, Debug)]
13pub struct ContentHeaderPayload {
14    pub class_id: u16,
15    pub body_size: u64,
16    pub properties: Properties,
17}
18
19
20#[derive(PartialEq, Clone, Debug)]
21pub struct Properties {
22    pub content_type: Option<AmqpString>,
23    pub content_encoding: Option<AmqpString>,
24    pub headers: Option<HashMap<AmqpString, FieldArgument>>,
25    pub delivery_mode: Option<u8>,
26    pub priority: Option<u8>,
27    pub correlation_id: Option<AmqpString>,
28    pub reply_to: Option<AmqpString>,
29    pub expiration: Option<AmqpString>,
30    pub message_id: Option<AmqpString>,
31    pub timestamp: Option<i64>,
32    pub type_: Option<AmqpString>,
33    pub user_id: Option<AmqpString>,
34    pub app_id: Option<AmqpString>,
35}
36
37impl Properties {
38    pub fn new() -> Properties {
39        Properties {
40            content_type: None,
41            content_encoding: None,
42            headers: None,
43            delivery_mode: None,
44            priority: None,
45            correlation_id: None,
46            reply_to: None,
47            expiration: None,
48            message_id: None,
49            timestamp: None,
50            type_: None,
51            user_id: None,
52            app_id: None,
53        }
54    }
55}
56
57
58// TESTS {{{
59#[cfg(test)]
60mod tests {
61    use super::*;
62    use bytes::BytesMut;
63
64    #[test]
65    fn encode_and_decode_without_properties() {
66        let payload = ContentHeaderPayload {
67            class_id: 42,
68            body_size: 10000,
69            properties: Properties::new(),
70        };
71
72        let cloned = payload.clone();
73
74        let mut encoded = BytesMut::from(encode_payload(payload));
75
76        let decoded = decode_payload(&mut encoded);
77
78        assert_eq!(decoded, cloned);
79        assert_eq!(encoded.len(), 0);
80    }
81
82    #[test]
83    fn encode_and_decode_with_properties() {
84        let properties = {
85            let mut ps = Properties::new();
86            ps.content_type = Some(AmqpString::from("application/text"));
87            ps.priority = Some(42);
88            ps
89        };
90        let payload = ContentHeaderPayload {
91            class_id: 42,
92            body_size: 10000,
93            properties: properties,
94        };
95
96        let cloned = payload.clone();
97
98        let mut encoded = BytesMut::from(encode_payload(payload));
99
100        let decoded = decode_payload(&mut encoded);
101
102        assert_eq!(decoded, cloned);
103        assert_eq!(encoded.len(), 0);
104    }
105
106}
107// }}}