@@ -16,34 +16,63 @@ pub trait Encode {
16
16
17
17
pub trait Decode < ' a > : From < & ' a [ u8 ] > + Encode { }
18
18
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
22
27
}
23
28
29
+
24
30
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)
27
49
}
28
50
29
51
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
+ }
31
59
}
32
60
33
61
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
+
37
69
}
38
70
}
39
71
}
40
72
41
73
impl From < u8 > for Encoded {
42
74
fn from ( value : u8 ) -> Self {
43
- Encoded {
44
- constructor : value,
45
- data : None ,
46
- }
75
+ Encoded :: Empty ( value)
47
76
}
48
77
}
49
78
@@ -146,81 +175,81 @@ impl Encode for bool {
146
175
#[ cfg( not( feature = "zero-length-bools" ) ) ]
147
176
fn encode ( & self ) -> Encoded {
148
177
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 ] )
151
180
}
152
181
}
153
182
}
154
183
155
184
impl Encode for u8 {
156
185
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 ( ) )
158
187
}
159
188
}
160
189
161
190
impl Encode for u16 {
162
191
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 ( ) )
164
193
}
165
194
}
166
195
167
196
impl Encode for i8 {
168
197
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 ( ) )
170
199
}
171
200
}
172
201
173
202
impl Encode for i16 {
174
203
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 ( ) )
176
205
}
177
206
}
178
207
179
208
impl Encode for char {
180
209
fn encode ( & self ) -> Encoded {
181
- Encoded :: new ( 0x73 , Some ( self . to_string ( ) . into_bytes ( ) ) )
210
+ Encoded :: new_fixed ( 0x73 , self . to_string ( ) . into_bytes ( ) )
182
211
}
183
212
}
184
213
185
214
impl Encode for Uuid {
186
215
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 ( ) )
188
217
}
189
218
}
190
219
impl Encode for u32 {
191
220
fn encode ( & self ) -> Encoded {
192
221
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 ( ) ) ,
196
225
}
197
226
}
198
227
}
199
228
200
229
impl Encode for u64 {
201
230
fn encode ( & self ) -> Encoded {
202
231
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 ( ) )
206
235
}
207
236
}
208
237
}
209
238
210
239
impl Encode for i32 {
211
240
fn encode ( & self ) -> Encoded {
212
241
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 ( ) )
215
244
}
216
245
}
217
246
}
218
247
219
248
impl Encode for i64 {
220
249
fn encode ( & self ) -> Encoded {
221
250
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 ( ) )
224
253
}
225
254
}
226
255
}
@@ -229,18 +258,18 @@ impl Encode for String {
229
258
fn encode ( & self ) -> Encoded {
230
259
match self . len ( ) {
231
260
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 ( ) )
233
262
}
234
- _ => Encoded :: new ( 0xb1 , Some ( self . as_bytes ( ) . to_vec ( ) ) )
263
+ _ => Encoded :: new_variable ( 0xb1 , self . as_bytes ( ) . to_vec ( ) )
235
264
}
236
265
}
237
266
}
238
267
239
268
impl Encode for Symbol {
240
269
fn encode ( & self ) -> Encoded {
241
270
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 ( ) ) ,
244
273
}
245
274
}
246
275
}
0 commit comments