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, AMQPContentHeader),
56    /// Content body
57    Body(ChannelId, Vec<u8>),
58    /// Heartbeat frame (Channel 0 enforced as per specifications)
59    Heartbeat,
60    /// Invalid Heartbeat frame (With configurable ChannelId, not following specifications)
61    InvalidHeartbeat(ChannelId),
62}
63
64impl AMQPFrame {
65    /// Return whether this frame is an AMQPFrame::Header or not
66    pub fn is_header(&self) -> bool {
67        matches!(self, AMQPFrame::Header(..))
68    }
69
70    /// Returns the channel id associated with this frame
71    pub fn channel_id(&self) -> ChannelId {
72        match self {
73            AMQPFrame::ProtocolHeader(_) => 0,
74            AMQPFrame::Method(id, _) => *id,
75            AMQPFrame::Header(id, _) => *id,
76            AMQPFrame::Body(id, _) => *id,
77            AMQPFrame::Heartbeat => 0,
78            AMQPFrame::InvalidHeartbeat(id) => *id,
79        }
80    }
81}
82
83impl fmt::Display for AMQPFrame {
84    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85        match self {
86            AMQPFrame::ProtocolHeader(version) => {
87                f.write_fmt(format_args!("AMQPFrame::ProtocolHeader({version})"))
88            }
89            AMQPFrame::Method(_, klass) => {
90                f.write_fmt(format_args!("AMQPFrame::Method({klass:?})"))
91            }
92            AMQPFrame::Header(..) => f.write_str("AMQPFrame::Header"),
93            AMQPFrame::Body(..) => f.write_str("AMQPFrame::Body"),
94            AMQPFrame::Heartbeat => f.write_str("AMQPFrame::Heartbeat"),
95            AMQPFrame::InvalidHeartbeat(id) => {
96                f.write_fmt(format_args!("AMQPFrame::InvalidHeartbeat({id})"))
97            }
98        }
99    }
100}
101
102/// Protocol version used
103#[derive(Clone, Copy, Debug, PartialEq, Eq)]
104pub struct ProtocolVersion {
105    /// Major version of the protocol
106    pub major: ShortShortUInt,
107    /// Minor version of the protocol
108    pub minor: ShortShortUInt,
109    /// Revision of the protocol
110    pub revision: ShortShortUInt,
111}
112
113impl ProtocolVersion {
114    /// AMQP 0.9.1
115    pub fn amqp_0_9_1() -> Self {
116        Self {
117            major: metadata::MAJOR_VERSION,
118            minor: metadata::MINOR_VERSION,
119            revision: metadata::REVISION,
120        }
121    }
122}
123
124impl fmt::Display for ProtocolVersion {
125    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126        f.write_fmt(format_args!(
127            "{}.{}.{}",
128            self.major, self.minor, self.revision
129        ))
130    }
131}
132
133/// Raw AMQP Frame
134#[derive(Clone, Copy, Debug, PartialEq, Eq)]
135pub struct AMQPRawFrame<I: ParsableInput> {
136    /// The type of frame
137    pub frame_type: AMQPFrameType,
138    /// The id this frame was received on
139    pub channel_id: ChannelId,
140    /// The payload of the frame
141    pub payload: I,
142}
143
144/// Content header
145#[derive(Clone, Debug, PartialEq)]
146pub struct AMQPContentHeader {
147    /// The class of content
148    pub class_id: Identifier,
149    /// The size of the content's body
150    pub body_size: PayloadSize,
151    /// The AMQP properties associated with the content
152    pub properties: basic::AMQPProperties,
153}