amq_protocol/frame/
structs.rs

1use crate::{frame::parsing::traits::ParsableInput, protocol::*, types::*};
2use std::fmt;
3
4/// Enum representing an AMQP channel
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub enum AMQPChannel {
7    /// The Global (id 0) AMQP channel used for creating other channels and for heartbeat
8    Global,
9    /// A regular AMQP channel
10    Id(ChannelId),
11}
12
13impl AMQPChannel {
14    /// Get the channel id
15    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/// The type of AMQP Frame
33#[derive(Clone, Copy, Debug, PartialEq, Eq)]
34pub enum AMQPFrameType {
35    /// The Protocol Header,
36    ProtocolHeader,
37    /// Call a method
38    Method,
39    /// Content header
40    Header,
41    /// Content body
42    Body,
43    /// Heartbeat frame
44    Heartbeat,
45}
46
47/// The different possible frames
48#[derive(Clone, Debug, PartialEq)]
49pub enum AMQPFrame {
50    /// Protocol header frame
51    ProtocolHeader(ProtocolVersion),
52    /// Method call
53    Method(ChannelId, AMQPClass),
54    /// Content header
55    Header(ChannelId, Identifier, Box<AMQPContentHeader>),
56    /// Content body
57    Body(ChannelId, Vec<u8>),
58    /// Heartbeat frame
59    Heartbeat(ChannelId),
60}
61
62impl AMQPFrame {
63    /// Return whether this frame is an AMQPFrame::Header or not
64    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/// Protocol version used
86#[derive(Clone, Copy, Debug, PartialEq, Eq)]
87pub struct ProtocolVersion {
88    /// Major version of the protocol
89    pub major: ShortShortUInt,
90    /// Minor version of the protocol
91    pub minor: ShortShortUInt,
92    /// Revision of the protocol
93    pub revision: ShortShortUInt,
94}
95
96impl ProtocolVersion {
97    /// AMQP 0.9.1
98    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/// Raw AMQP Frame
117#[derive(Clone, Copy, Debug, PartialEq, Eq)]
118pub struct AMQPRawFrame<I: ParsableInput> {
119    /// The type of frame
120    pub frame_type: AMQPFrameType,
121    /// The id this frame was received on
122    pub channel_id: ChannelId,
123    /// The payload of the frame
124    pub payload: I,
125}
126
127/// Content header
128#[derive(Clone, Debug, PartialEq)]
129pub struct AMQPContentHeader {
130    /// The class of content
131    pub class_id: Identifier,
132    /// The size of the content's body
133    pub body_size: PayloadSize,
134    /// The AMQP properties associated with the content
135    pub properties: basic::AMQPProperties,
136}