Skip to content

Commit acbab07

Browse files
changed Encoded from struct to enum
1 parent 7ab9dd9 commit acbab07

File tree

3 files changed

+68
-39
lines changed

3 files changed

+68
-39
lines changed

amqp-lib/src/types/amqp_type.rs

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,63 @@ pub trait Encode {
1616

1717
pub trait Decode<'a>: From<&'a [u8]> + Encode {}
1818

19-
pub struct Encoded {
20-
constructor: u8,
21-
data: Option<Vec<u8>>,
19+
20+
21+
pub enum Encoded {
22+
Empty(u8), // Constructor
23+
Fixed(u8, Vec<u8>), // Constructor, Data
24+
Variable(u8, Vec<u8>), // Constructor, Data, size is computed from data
25+
Compound(u8, u32, Vec<u8>), // Constructor, count, data
26+
Array(u8, u32, u8, Vec<u8>) // Constructor, count, element constructor, data
2227
}
2328

29+
2430
impl Encoded {
25-
pub fn new(constructor: u8, data: Option<Vec<u8>>) -> Self {
26-
Encoded { constructor, data }
31+
pub fn new_empty(constructor: u8) -> Self {
32+
Encoded::Empty(constructor)
33+
}
34+
35+
pub fn new_fixed(constructor: u8, data: Vec<u8>) -> Self {
36+
Encoded::Fixed(constructor, data)
37+
}
38+
39+
pub fn new_variable(constructor: u8, data: Vec<u8>) -> Self {
40+
Encoded::Variable(constructor, data)
41+
}
42+
43+
pub fn new_compound(constructor: u8, count: u32, data: Vec<u8>) -> Self {
44+
Encoded::Compound(constructor, count, data)
45+
}
46+
47+
pub fn new_array(constructor: u8, count: u32, element_constructor: u8, data: Vec<u8>) -> Self {
48+
Encoded::Array(constructor, count, element_constructor, data)
2749
}
2850

2951
pub fn constructor(&self) -> u8 {
30-
self.constructor
52+
match self {
53+
Self::Empty(c) => c.to_owned(),
54+
Self::Fixed(c, _) => c.to_owned(),
55+
Self::Variable(c, _) => c.to_owned(),
56+
Self::Compound(c, _, _) => c.to_owned(),
57+
Self::Array(c, _, _, _) => c.to_owned()
58+
}
3159
}
3260

3361
pub fn data_len(&self) -> usize {
34-
match &self.data {
35-
Some(data) => data.len(),
36-
None => 0,
62+
match self {
63+
Self::Empty(_) => 0,
64+
Self::Fixed(_, data) => data.len(),
65+
Self::Variable(_, data) => data.len(),
66+
Self::Compound(_, _, data) => data.len(),
67+
Self::Array(_, _, _, data) => data.len(),
68+
3769
}
3870
}
3971
}
4072

4173
impl From<u8> for Encoded {
4274
fn from(value: u8) -> Self {
43-
Encoded {
44-
constructor: value,
45-
data: None,
46-
}
75+
Encoded::Empty(value)
4776
}
4877
}
4978

@@ -146,81 +175,81 @@ impl Encode for bool {
146175
#[cfg(not(feature = "zero-length-bools"))]
147176
fn encode(&self) -> Encoded {
148177
match self {
149-
true => Encoded::new(0x56, Some(vec![0x01])),
150-
false => Encoded::new(0x56, Some(vec![0x00]))
178+
true => Encoded::new_fixed(0x56, vec![0x01]),
179+
false => Encoded::new_fixed(0x56, vec![0x00])
151180
}
152181
}
153182
}
154183

155184
impl Encode for u8 {
156185
fn encode(&self) -> Encoded {
157-
Encoded::new(0x50, Some(self.to_be_bytes().to_vec()))
186+
Encoded::new_fixed(0x50, self.to_be_bytes().to_vec())
158187
}
159188
}
160189

161190
impl Encode for u16 {
162191
fn encode(&self) -> Encoded {
163-
Encoded::new(0x60, Some(self.to_be_bytes().to_vec()))
192+
Encoded::new_fixed(0x60, self.to_be_bytes().to_vec())
164193
}
165194
}
166195

167196
impl Encode for i8 {
168197
fn encode(&self) -> Encoded {
169-
Encoded::new(0x51, Some(self.to_be_bytes().to_vec()))
198+
Encoded::new_fixed(0x51, self.to_be_bytes().to_vec())
170199
}
171200
}
172201

173202
impl Encode for i16 {
174203
fn encode(&self) -> Encoded {
175-
Encoded::new(0x61, Some(self.to_be_bytes().to_vec()))
204+
Encoded::new_fixed(0x61, self.to_be_bytes().to_vec())
176205
}
177206
}
178207

179208
impl Encode for char {
180209
fn encode(&self) -> Encoded {
181-
Encoded::new(0x73, Some(self.to_string().into_bytes()))
210+
Encoded::new_fixed(0x73, self.to_string().into_bytes())
182211
}
183212
}
184213

185214
impl Encode for Uuid {
186215
fn encode(&self) -> Encoded {
187-
Encoded::new(0x98, Some(self.0.into_bytes().to_vec()))
216+
Encoded::new_fixed(0x98, self.0.into_bytes().to_vec())
188217
}
189218
}
190219
impl Encode for u32 {
191220
fn encode(&self) -> Encoded {
192221
match self {
193-
0 => Encoded::new(0x43, None),
194-
x if x > &0 && x <= &255 => Encoded::new(0x52, Some(x.to_be_bytes().to_vec())),
195-
_ => Encoded::new(0x70, Some(self.to_be_bytes().to_vec())),
222+
0 => Encoded::new_empty(0x43),
223+
x if x > &0 && x <= &255 => Encoded::new_fixed(0x52, x.to_be_bytes().to_vec()),
224+
_ => Encoded::new_fixed(0x70, self.to_be_bytes().to_vec()),
196225
}
197226
}
198227
}
199228

200229
impl Encode for u64 {
201230
fn encode(&self) -> Encoded {
202231
match self {
203-
0 => Encoded::new(0x44, None),
204-
x if x > &&0 && x <= &255 => Encoded::new(0x53, Some(x.to_be_bytes().to_vec())),
205-
_ => Encoded::new(0x80, Some(self.to_be_bytes().to_vec()))
232+
0 => Encoded::new_empty(0x44),
233+
x if x > &&0 && x <= &255 => Encoded::new_fixed(0x53, x.to_be_bytes().to_vec()),
234+
_ => Encoded::new_fixed(0x80,self.to_be_bytes().to_vec())
206235
}
207236
}
208237
}
209238

210239
impl Encode for i32 {
211240
fn encode(&self) -> Encoded {
212241
match self {
213-
x if x >= &-128 && x <= &127 => Encoded::new(0x54, Some(x.to_be_bytes().to_vec())),
214-
_ => Encoded::new(0x71, Some(self.to_be_bytes().to_vec()))
242+
x if x >= &-128 && x <= &127 => Encoded::new_fixed(0x54, x.to_be_bytes().to_vec()),
243+
_ => Encoded::new_fixed(0x71, self.to_be_bytes().to_vec())
215244
}
216245
}
217246
}
218247

219248
impl Encode for i64 {
220249
fn encode(&self) -> Encoded {
221250
match self {
222-
x if x >= &-128 && x <= &127 => Encoded::new(0x55, Some(x.to_be_bytes().to_vec())),
223-
_ => Encoded::new(0x81, Some(self.to_be_bytes().to_vec()))
251+
x if x >= &-128 && x <= &127 => Encoded::new_fixed(0x55, x.to_be_bytes().to_vec()),
252+
_ => Encoded::new_fixed(0x81, self.to_be_bytes().to_vec())
224253
}
225254
}
226255
}
@@ -229,18 +258,18 @@ impl Encode for String {
229258
fn encode(&self) -> Encoded {
230259
match self.len() {
231260
x if x >= 0 as usize && x <= 255 as usize => {
232-
Encoded::new(0xa1, Some(self.as_bytes().to_vec()))
261+
Encoded::new_variable(0xa1, self.as_bytes().to_vec())
233262
}
234-
_ => Encoded::new(0xb1, Some(self.as_bytes().to_vec()))
263+
_ => Encoded::new_variable(0xb1, self.as_bytes().to_vec())
235264
}
236265
}
237266
}
238267

239268
impl Encode for Symbol {
240269
fn encode(&self) -> Encoded {
241270
match self.0.len() {
242-
x if x <= 255 => Encoded::new(0xa3, Some(self.0.as_bytes().to_vec())),
243-
_ => Encoded::new(0xb1, Some(self.0.as_bytes().to_vec())),
271+
x if x <= 255 => Encoded::new_variable(0xa3, self.0.as_bytes().to_vec()),
272+
_ => Encoded::new_variable(0xb1, self.0.as_bytes().to_vec()),
244273
}
245274
}
246275
}

amqp-lib/src/types/binary.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ pub struct Binary(Vec<u8>);
88
impl Encode for Binary {
99
fn encode(&self) -> Encoded {
1010
match self.0.len() {
11-
x if x <= 255 => Encoded::new(0xa0, Some(self.0.to_owned())),
12-
_ => Encoded::new(0xb0, Some(self.0.to_owned()))
11+
x if x <= 255 => Encoded::new_variable(0xa0, self.0.to_owned()),
12+
_ => Encoded::new_variable(0xb0, self.0.to_owned())
1313
}
1414
}
1515
}

amqp-lib/src/types/floating_point.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ impl PartialEq for Double {
4949

5050
impl Encode for Float {
5151
fn encode(&self) -> Encoded {
52-
Encoded::new(0x72, Some(self.0.to_be_bytes().to_vec()))
52+
Encoded::new_fixed(0x72, self.0.to_be_bytes().to_vec())
5353
}
5454
}
5555

5656
impl Encode for Double {
5757
fn encode(&self) -> Encoded {
58-
Encoded::new(0x82, Some(self.0.to_be_bytes().to_vec()))
58+
Encoded::new_variable(0x82, self.0.to_be_bytes().to_vec())
5959
}
6060
}
6161

0 commit comments

Comments
 (0)