1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use lapin::message::Delivery;
use lapin::types::{AMQPValue, FieldTable};
#[derive(Clone)]
pub struct Message {
pub data: Vec<u8>,
pub headers: FieldTable,
}
impl Message {
pub fn add_header(&mut self, key: &str, value: String) {
self.headers
.insert(key.into(), AMQPValue::LongString(value.into()))
}
pub fn header_string(&self, key: &str) -> Option<String> {
let h = self.headers.inner().get(key);
if let Some(h) = h {
return match h {
AMQPValue::ShortString(s) => Some(s.to_string()),
AMQPValue::LongString(s) => Some(s.to_string()),
AMQPValue::ShortInt(s) => Some(s.to_string()),
AMQPValue::ShortShortInt(s) => Some(s.to_string()),
AMQPValue::ShortShortUInt(s) => Some(s.to_string()),
AMQPValue::LongInt(s) => Some(s.to_string()),
AMQPValue::LongLongInt(s) => Some(s.to_string()),
_ => None,
};
}
None
}
pub fn header_string_default<F: FnOnce() -> String>(&self, key: &str, or_else: F) -> String {
let h = self.headers.inner().get(key);
if let Some(h) = h {
return match h {
AMQPValue::ShortString(s) => s.to_string(),
AMQPValue::LongString(s) => s.to_string(),
AMQPValue::ShortInt(s) => s.to_string(),
AMQPValue::ShortShortInt(s) => s.to_string(),
AMQPValue::ShortShortUInt(s) => s.to_string(),
AMQPValue::LongInt(s) => s.to_string(),
AMQPValue::LongLongInt(s) => s.to_string(),
_ => or_else(),
};
}
or_else()
}
pub fn header_i32(&self, key: &str) -> Option<i32> {
let h = self.headers.inner().get(key);
if let Some(h) = h {
return match h {
AMQPValue::ShortString(i) => i.to_string().parse::<i32>().ok(),
AMQPValue::LongString(i) => i.to_string().parse::<i32>().ok(),
AMQPValue::ShortInt(i) => Some(*i as i32),
AMQPValue::ShortShortInt(i) => Some(*i as i32),
AMQPValue::ShortShortUInt(i) => Some(*i as i32),
AMQPValue::LongInt(i) => Some(*i as i32),
AMQPValue::LongLongInt(i) => Some(*i as i32),
_ => None,
};
}
None
}
pub fn header_i32_default(&self, key: &str, default: i32) -> i32 {
let h = self.headers.inner().get(key);
if let Some(h) = h {
return match h {
AMQPValue::ShortString(i) => i.to_string().parse::<i32>().unwrap_or(0),
AMQPValue::LongString(i) => i.to_string().parse::<i32>().unwrap_or(0),
AMQPValue::ShortInt(i) => *i as i32,
AMQPValue::ShortShortInt(i) => *i as i32,
AMQPValue::ShortShortUInt(i) => *i as i32,
AMQPValue::LongInt(i) => *i as i32,
AMQPValue::LongLongInt(i) => *i as i32,
_ => default,
};
}
default
}
}
impl From<&Delivery> for Message {
fn from(delivery: &Delivery) -> Self {
let mut headers = FieldTable::default();
if let Some(props) = delivery.properties.headers() {
headers = props.clone();
}
Message {
data: delivery.data.clone(),
headers,
}
}
}