amq_protocol/frame/
structs.rs1use crate::{frame::parsing::traits::ParsableInput, protocol::*, types::*};
2use std::fmt;
3
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub enum AMQPChannel {
7 Global,
9 Id(ChannelId),
11}
12
13impl AMQPChannel {
14 pub fn get_id(self) -> ChannelId {
16 match self {
17 AMQPChannel::Global => 0,
18 AMQPChannel::Id(id) => id,
19 }
20 }
21}
22
23impl From<ChannelId> for AMQPChannel {
24 fn from(id: ChannelId) -> AMQPChannel {
25 match id {
26 0 => AMQPChannel::Global,
27 id => AMQPChannel::Id(id),
28 }
29 }
30}
31
32#[derive(Clone, Copy, Debug, PartialEq, Eq)]
34pub enum AMQPFrameType {
35 ProtocolHeader,
37 Method,
39 Header,
41 Body,
43 Heartbeat,
45}
46
47#[derive(Clone, Debug, PartialEq)]
49pub enum AMQPFrame {
50 ProtocolHeader(ProtocolVersion),
52 Method(ChannelId, AMQPClass),
54 Header(ChannelId, Identifier, Box<AMQPContentHeader>),
56 Body(ChannelId, Vec<u8>),
58 Heartbeat(ChannelId),
60}
61
62impl AMQPFrame {
63 pub fn is_header(&self) -> bool {
65 matches!(self, AMQPFrame::Header(..))
66 }
67}
68
69impl fmt::Display for AMQPFrame {
70 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71 match self {
72 AMQPFrame::ProtocolHeader(version) => {
73 f.write_fmt(format_args!("AMQPFrame::ProtocolHeader({version})"))
74 }
75 AMQPFrame::Method(_, klass) => {
76 f.write_fmt(format_args!("AMQPFrame::Method({klass:?})"))
77 }
78 AMQPFrame::Header(..) => f.write_str("AMQPFrame::Header"),
79 AMQPFrame::Body(..) => f.write_str("AMQPFrame::Body"),
80 AMQPFrame::Heartbeat(_) => f.write_str("AMQPFrame::Heartbeat"),
81 }
82 }
83}
84
85#[derive(Clone, Copy, Debug, PartialEq, Eq)]
87pub struct ProtocolVersion {
88 pub major: ShortShortUInt,
90 pub minor: ShortShortUInt,
92 pub revision: ShortShortUInt,
94}
95
96impl ProtocolVersion {
97 pub fn amqp_0_9_1() -> Self {
99 Self {
100 major: metadata::MAJOR_VERSION,
101 minor: metadata::MINOR_VERSION,
102 revision: metadata::REVISION,
103 }
104 }
105}
106
107impl fmt::Display for ProtocolVersion {
108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109 f.write_fmt(format_args!(
110 "{}.{}.{}",
111 self.major, self.minor, self.revision
112 ))
113 }
114}
115
116#[derive(Clone, Copy, Debug, PartialEq, Eq)]
118pub struct AMQPRawFrame<I: ParsableInput> {
119 pub frame_type: AMQPFrameType,
121 pub channel_id: ChannelId,
123 pub payload: I,
125}
126
127#[derive(Clone, Debug, PartialEq)]
129pub struct AMQPContentHeader {
130 pub class_id: Identifier,
132 pub body_size: PayloadSize,
134 pub properties: basic::AMQPProperties,
136}