ntex_amqp_codec/types/
mod.rs1use 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(Clone, Eq, Ord, PartialOrd, PartialEq)]
117pub struct Str(ByteString);
118
119impl Str {
120 #[allow(clippy::should_implement_trait)]
121 pub fn from_str(s: &str) -> Str {
122 Str(ByteString::from(s))
123 }
124
125 pub const fn from_static(s: &'static str) -> Str {
126 Str(ByteString::from_static(s))
127 }
128
129 pub fn as_bytes(&self) -> &[u8] {
130 self.0.as_bytes().as_ref()
131 }
132
133 pub fn as_str(&self) -> &str {
134 self.0.as_str()
135 }
136
137 pub fn to_bytes_str(&self) -> ByteString {
138 self.0.clone()
139 }
140
141 pub fn len(&self) -> usize {
142 self.0.len()
143 }
144}
145
146impl From<&'static str> for Str {
147 fn from(s: &'static str) -> Str {
148 Str(ByteString::from_static(s))
149 }
150}
151
152impl From<ByteString> for Str {
153 fn from(s: ByteString) -> Str {
154 Str(s)
155 }
156}
157
158impl From<String> for Str {
159 fn from(s: String) -> Str {
160 Str(ByteString::from(s))
161 }
162}
163
164impl<'a> From<&'a ByteString> for Str {
165 fn from(s: &'a ByteString) -> Str {
166 Str(s.clone())
167 }
168}
169
170impl hash::Hash for Str {
171 fn hash<H: hash::Hasher>(&self, state: &mut H) {
172 self.0.hash(state);
173 }
174}
175
176impl borrow::Borrow<str> for Str {
177 fn borrow(&self) -> &str {
178 self.as_str()
179 }
180}
181
182impl PartialEq<str> for Str {
183 fn eq(&self, other: &str) -> bool {
184 self.0.eq(&other)
185 }
186}
187
188impl fmt::Debug for Str {
189 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
190 self.0.fmt(f)
191 }
192}