Skip to content

Commit c4095b0

Browse files
implemented some encode functions
1 parent 1910c54 commit c4095b0

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

amqp-lib/src/types/amqp_type.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::types::collection::*;
66
use crate::types::decimal::*;
77
use crate::types::floating_point::*;
88
use bigdecimal::BigDecimal;
9+
use bigdecimal::num_traits::ToBytes;
910
use indexmap::IndexMap;
1011

1112
pub trait Hashable: std::hash::Hash {}
@@ -144,55 +145,46 @@ impl Encode for bool {
144145

145146
#[cfg(not(feature = "zero-length-bools"))]
146147
fn encode(&self) -> Encoded {
147-
0x56.into()
148+
match self {
149+
true => Encoded::new(0x56, Some(vec![0x01])),
150+
false => Encoded::new(0x56, Some(vec![0x00]))
151+
}
148152
}
149153
}
150154

151155
impl Encode for u8 {
152156
fn encode(&self) -> Encoded {
153-
0x50.into()
157+
Encoded::new(0x50, Some(self.to_be_bytes().to_vec()))
154158
}
155159
}
156160

157161
impl Encode for u16 {
158162
fn encode(&self) -> Encoded {
159-
0x60.into()
163+
Encoded::new(0x60, Some(self.to_be_bytes().to_vec()))
160164
}
161165
}
162166

163167
impl Encode for i8 {
164168
fn encode(&self) -> Encoded {
165-
0x51.into()
169+
Encoded::new(0x51, Some(self.to_be_bytes().to_vec()))
166170
}
167171
}
168172

169173
impl Encode for i16 {
170174
fn encode(&self) -> Encoded {
171-
0x61.into()
172-
}
173-
}
174-
175-
impl Encode for Float {
176-
fn encode(&self) -> Encoded {
177-
0x72.into()
178-
}
179-
}
180-
181-
impl Encode for Double {
182-
fn encode(&self) -> Encoded {
183-
0x82.into()
175+
Encoded::new(0x61, Some(self.to_be_bytes().to_vec()))
184176
}
185177
}
186178

187179
impl Encode for char {
188180
fn encode(&self) -> Encoded {
189-
0x73.into()
181+
Encoded::new(0x73, Some(self.to_string().into_bytes()))
190182
}
191183
}
192184

193185
impl Encode for Uuid {
194186
fn encode(&self) -> Encoded {
195-
0x98.into()
187+
Encoded::new(0x98, Some(self.0.into_bytes().to_vec()))
196188
}
197189
}
198190
impl Encode for u32 {

amqp-lib/src/types/floating_point.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::hash::Hash;
2+
use crate::types::amqp_type::{Encode, Encoded};
23

34
pub struct Float(f32);
45
pub struct Double(f64);
@@ -44,6 +45,22 @@ impl PartialEq for Double {
4445
}
4546
}
4647

48+
49+
50+
impl Encode for Float {
51+
fn encode(&self) -> Encoded {
52+
Encoded::new(0x72, Some(self.0.to_be_bytes().to_vec()))
53+
}
54+
}
55+
56+
impl Encode for Double {
57+
fn encode(&self) -> Encoded {
58+
Encoded::new(0x82, Some(self.0.to_be_bytes().to_vec()))
59+
}
60+
}
61+
62+
63+
4764
impl From<f32> for Float {
4865
fn from(value: f32) -> Self {
4966
Float(value)

0 commit comments

Comments
 (0)