Skip to main content

ntex_amqp_codec/types/
mod.rs

1use std::{borrow, fmt, hash, ops, str};
2
3use ntex_bytes::ByteString;
4
5mod array;
6mod symbol;
7mod variant;
8
9use crate::AmqpParseError;
10
11pub use self::array::Array;
12pub use self::symbol::{StaticSymbol, Symbol};
13pub use self::variant::{DescribedCompound, Variant, VariantMap, VecStringMap, VecSymbolMap};
14
15#[derive(Debug, PartialEq, Eq, Clone, Hash)]
16pub enum Descriptor {
17    Ulong(u64),
18    Symbol(Symbol),
19}
20
21#[derive(Debug, PartialEq, Eq, Clone, Hash)]
22pub enum Constructor {
23    FormatCode(u8),
24    Described {
25        descriptor: Descriptor,
26        format_code: u8,
27    },
28}
29
30impl Constructor {
31    pub fn format_code(&self) -> u8 {
32        match self {
33            Constructor::FormatCode(code) => *code,
34            Constructor::Described { format_code, .. } => *format_code,
35        }
36    }
37
38    pub fn descriptor(&self) -> Option<&Descriptor> {
39        match self {
40            Constructor::FormatCode(_) => None,
41            Constructor::Described { descriptor, .. } => Some(descriptor),
42        }
43    }
44
45    pub fn ensure_described(&self, descriptor: &Descriptor) -> Result<(), AmqpParseError> {
46        match self {
47            Constructor::Described { descriptor: d, .. } if d == descriptor => Ok(()),
48            Constructor::Described { descriptor: d, .. } => {
49                Err(AmqpParseError::InvalidDescriptor(Box::new(d.clone())))
50            }
51            Constructor::FormatCode(fmt) => Err(AmqpParseError::InvalidFormatCode(*fmt)),
52        }
53    }
54}
55
56#[derive(Debug, PartialEq, Eq, Clone, Hash, From)]
57pub struct Multiple<T>(pub Vec<T>);
58
59impl<T> Multiple<T> {
60    pub fn len(&self) -> usize {
61        self.0.len()
62    }
63
64    pub fn is_empty(&self) -> bool {
65        self.0.is_empty()
66    }
67
68    pub fn iter(&self) -> ::std::slice::Iter<'_, T> {
69        self.0.iter()
70    }
71}
72
73impl<T> Default for Multiple<T> {
74    fn default() -> Multiple<T> {
75        Multiple(Vec::new())
76    }
77}
78
79impl<T> ops::Deref for Multiple<T> {
80    type Target = Vec<T>;
81
82    fn deref(&self) -> &Self::Target {
83        &self.0
84    }
85}
86
87impl<T> ops::DerefMut for Multiple<T> {
88    fn deref_mut(&mut self) -> &mut Self::Target {
89        &mut self.0
90    }
91}
92
93#[derive(Debug, PartialEq, Eq, Clone, Hash)]
94pub struct List(pub Vec<Variant>);
95
96impl List {
97    pub fn len(&self) -> usize {
98        self.0.len()
99    }
100
101    pub fn is_empty(&self) -> bool {
102        self.0.is_empty()
103    }
104
105    pub fn iter(&self) -> ::std::slice::Iter<'_, Variant> {
106        self.0.iter()
107    }
108}
109
110impl From<Vec<Variant>> for List {
111    fn from(data: Vec<Variant>) -> List {
112        List(data)
113    }
114}
115
116#[derive(Debug, PartialEq, Eq, Clone, Hash)]
117pub struct ListDescribed<T>(pub Vec<T>);
118
119impl<T> Default for ListDescribed<T> {
120    fn default() -> Self {
121        Self(Vec::new())
122    }
123}
124
125impl<T> ListDescribed<T> {
126    pub fn new(items: Vec<T>) -> Self {
127        Self(items)
128    }
129
130    pub fn len(&self) -> usize {
131        self.0.len()
132    }
133
134    pub fn is_empty(&self) -> bool {
135        self.0.is_empty()
136    }
137
138    pub fn iter(&self) -> ::std::slice::Iter<'_, T> {
139        self.0.iter()
140    }
141}
142
143impl<T> From<Vec<T>> for ListDescribed<T> {
144    fn from(data: Vec<T>) -> Self {
145        Self(data)
146    }
147}
148
149#[derive(Clone, Eq, Ord, PartialOrd, PartialEq)]
150pub struct Str(ByteString);
151
152impl Str {
153    #[allow(clippy::should_implement_trait)]
154    pub fn from_str(s: &str) -> Str {
155        Str(ByteString::from(s))
156    }
157
158    pub const fn from_static(s: &'static str) -> Str {
159        Str(ByteString::from_static(s))
160    }
161
162    pub fn as_bytes(&self) -> &[u8] {
163        self.0.as_bytes().as_ref()
164    }
165
166    pub fn as_str(&self) -> &str {
167        self.0.as_str()
168    }
169
170    pub fn to_bytes_str(&self) -> ByteString {
171        self.0.clone()
172    }
173
174    pub fn len(&self) -> usize {
175        self.0.len()
176    }
177}
178
179impl From<&'static str> for Str {
180    fn from(s: &'static str) -> Str {
181        Str(ByteString::from_static(s))
182    }
183}
184
185impl From<ByteString> for Str {
186    fn from(s: ByteString) -> Str {
187        Str(s)
188    }
189}
190
191impl From<String> for Str {
192    fn from(s: String) -> Str {
193        Str(ByteString::from(s))
194    }
195}
196
197impl<'a> From<&'a ByteString> for Str {
198    fn from(s: &'a ByteString) -> Str {
199        Str(s.clone())
200    }
201}
202
203impl hash::Hash for Str {
204    fn hash<H: hash::Hasher>(&self, state: &mut H) {
205        self.0.hash(state);
206    }
207}
208
209impl borrow::Borrow<str> for Str {
210    fn borrow(&self) -> &str {
211        self.as_str()
212    }
213}
214
215impl PartialEq<str> for Str {
216    fn eq(&self, other: &str) -> bool {
217        self.0.eq(&other)
218    }
219}
220
221impl fmt::Debug for Str {
222    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
223        self.0.fmt(f)
224    }
225}