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
151
152
153
154
155
156
157
158
159
160
pub use crate::types::parsing::traits;
use crate::{
frame::*,
protocol::{basic::parse_properties, *},
types::parsing::*,
};
use nom::{
bytes::streaming::{tag, take},
combinator::{all_consuming, flat_map, map, map_opt, map_res},
error::context,
sequence::{pair, tuple},
};
use traits::ParsableInput;
pub fn parse_channel<I: ParsableInput>(i: I) -> ParserResult<I, AMQPChannel> {
context("parse_channel", map(parse_id, From::from))(i)
}
pub fn parse_protocol_header<I: ParsableInput>(i: I) -> ParserResult<I, ProtocolVersion> {
context(
"parse_protocol_header",
map(
tuple((
tag(&metadata::NAME.as_bytes()[1..]),
tag(&[0][..]),
parse_short_short_uint,
parse_short_short_uint,
parse_short_short_uint,
)),
|(_, _, major, minor, revision)| ProtocolVersion {
major,
minor,
revision,
},
),
)(i)
}
pub fn parse_frame_type<I: ParsableInput>(i: I) -> ParserResult<I, AMQPFrameType> {
context(
"parse_frame_type",
map_opt(parse_short_short_uint, |method| match method {
c if c == metadata::NAME.as_bytes()[0] => Some(AMQPFrameType::ProtocolHeader),
constants::FRAME_METHOD => Some(AMQPFrameType::Method),
constants::FRAME_HEADER => Some(AMQPFrameType::Header),
constants::FRAME_BODY => Some(AMQPFrameType::Body),
constants::FRAME_HEARTBEAT => Some(AMQPFrameType::Heartbeat),
_ => None,
}),
)(i)
}
pub fn parse_frame<I: ParsableInput>(i: I) -> ParserResult<I, AMQPFrame> {
context(
"parse_frame",
flat_map(parse_frame_type, move |frame_type| {
move |i: I| match frame_type {
AMQPFrameType::ProtocolHeader => {
map(parse_protocol_header, AMQPFrame::ProtocolHeader)(i)
}
frame_type => map_res(
parse_raw_frame(frame_type),
|AMQPRawFrame {
channel_id,
frame_type,
payload,
}: AMQPRawFrame<I>| match frame_type {
AMQPFrameType::ProtocolHeader => {
Ok(AMQPFrame::ProtocolHeader(ProtocolVersion::amqp_0_9_1()))
}
AMQPFrameType::Method => all_consuming(parse_class)(payload)
.map(|(_, m)| AMQPFrame::Method(channel_id, m)),
AMQPFrameType::Header => all_consuming(parse_content_header)(payload)
.map(|(_, h)| AMQPFrame::Header(channel_id, h.class_id, Box::new(h))),
AMQPFrameType::Body => Ok(AMQPFrame::Body(
channel_id,
payload.iter_elements().collect(),
)),
AMQPFrameType::Heartbeat => Ok(AMQPFrame::Heartbeat(channel_id)),
},
)(i),
}
}),
)(i)
}
pub fn parse_raw_frame<I: ParsableInput>(
frame_type: AMQPFrameType,
) -> impl FnMut(I) -> ParserResult<I, AMQPRawFrame<I>> {
move |i: I| {
context(
"parse_raw_frame",
flat_map(
pair(parse_id, parse_long_uint),
move |(channel_id, size)| {
map(
pair(take(size), tag(&[constants::FRAME_END][..])),
move |(payload, _)| AMQPRawFrame {
frame_type,
channel_id,
payload,
},
)
},
),
)(i)
}
}
pub fn parse_content_header<I: ParsableInput>(i: I) -> ParserResult<I, AMQPContentHeader> {
context(
"parse_content_header",
map(
tuple((
parse_id,
parse_short_uint,
parse_long_long_uint,
context("parse_properties", parse_properties),
)),
|(class_id, _weight, body_size, properties)| AMQPContentHeader {
class_id,
body_size,
properties,
},
),
)(i)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_protocol_header() {
assert_eq!(
parse_frame(&[b'A', b'M', b'Q', b'P', 0, 0, 9, 1][..]),
Ok((
&[][..],
AMQPFrame::ProtocolHeader(ProtocolVersion::amqp_0_9_1())
))
);
}
#[test]
fn test_heartbeat() {
assert_eq!(
parse_frame(&[8, 0, 1, 0, 0, 0, 0, 206][..]),
Ok((&[][..], AMQPFrame::Heartbeat(1)))
);
}
}