Skip to content

Commit d1d73c0

Browse files
moved decimal types to separate modules, fixed all remaining warnings, formatting
1 parent 3966f79 commit d1d73c0

File tree

17 files changed

+179
-154
lines changed

17 files changed

+179
-154
lines changed

amqp-type/src/amqp_type.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::hash::Hash;
22

3-
43
use crate::array::array::Array;
54
use crate::compound::list::List;
65
use crate::compound::map::Map;
7-
use crate::fixed_width::decimal::*;
6+
use crate::fixed_width::decimal128::Decimal128;
7+
use crate::fixed_width::decimal32::Decimal32;
8+
use crate::fixed_width::decimal64::Decimal64;
89
use crate::fixed_width::double::*;
910
use crate::fixed_width::float::Float;
1011
use crate::fixed_width::timestamp::Timestamp;
@@ -74,6 +75,15 @@ impl Encode for AmqpType {
7475
}
7576
}
7677

78+
impl From<Option<AmqpType>> for AmqpType {
79+
fn from(value: Option<AmqpType>) -> Self {
80+
match value {
81+
Some(val) => val,
82+
None => AmqpType::Null,
83+
}
84+
}
85+
}
86+
7787
impl From<bool> for AmqpType {
7888
fn from(value: bool) -> Self {
7989
AmqpType::Boolean(value)
@@ -200,6 +210,24 @@ impl From<Array> for AmqpType {
200210
}
201211
}
202212

213+
impl From<Decimal32> for AmqpType {
214+
fn from(value: Decimal32) -> Self {
215+
AmqpType::Decimal32(value)
216+
}
217+
}
218+
219+
impl From<Decimal64> for AmqpType {
220+
fn from(value: Decimal64) -> Self {
221+
AmqpType::Decimal64(value)
222+
}
223+
}
224+
225+
impl From<Decimal128> for AmqpType {
226+
fn from(value: Decimal128) -> Self {
227+
AmqpType::Decimal128(value)
228+
}
229+
}
230+
203231
#[cfg(test)]
204232
mod tests {
205233
use super::*;

amqp-type/src/compound/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
mod encoded_vec;
32
pub(crate) mod list;
43
pub(crate) mod map;

amqp-type/src/fixed_width/byte.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::error::AppError;
2-
use crate::serde::encode::{Encode, Encoded};
32
use crate::serde::decode::Decode;
3+
use crate::serde::encode::{Encode, Encoded};
44

55
const DEFAULT_CONSTR: u8 = 0x51;
66

@@ -14,15 +14,16 @@ impl Decode for i8 {
1414
fn can_decode(iter: impl Iterator<Item = u8>) -> bool {
1515
match iter.peekable().peek() {
1616
Some(&DEFAULT_CONSTR) => true,
17-
_ => false
17+
_ => false,
1818
}
1919
}
2020

2121
fn try_decode(mut iter: impl Iterator<Item = u8>) -> Result<Self, crate::error::AppError>
22-
where
23-
Self: Sized {
22+
where
23+
Self: Sized,
24+
{
2425
match iter.next() {
25-
Some(DEFAULT_CONSTR) => Ok(parse_i8(iter)?),
26+
Some(DEFAULT_CONSTR) => Ok(parse_i8(iter)?),
2627
Some(c) => Err(AppError::DeserializationIllegalConstructorError(c)),
2728
None => Err(AppError::IteratorEmptyOrTooShortError),
2829
}
@@ -37,7 +38,6 @@ fn parse_i8(mut iter: impl Iterator<Item = u8>) -> Result<i8, AppError> {
3738
}
3839
}
3940

40-
4141
#[cfg(test)]
4242
mod test {
4343

@@ -49,7 +49,7 @@ mod test {
4949
assert_eq!(val.encode().constructor(), 0x51);
5050
}
5151

52-
#[test]
52+
#[test]
5353
fn can_decode_returns_true_if_constructor_is_valid() {
5454
let val = vec![0x51];
5555
assert_eq!(i8::can_decode(val.into_iter()), true);

amqp-type/src/fixed_width/decimal.rs

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use bigdecimal::BigDecimal;
2+
3+
use crate::serde::encode::{Encode, Encoded};
4+
5+
#[derive(Hash, Eq, PartialEq)]
6+
pub struct Decimal128(BigDecimal);
7+
8+
impl Encode for Decimal128 {
9+
fn encode(&self) -> Encoded {
10+
0x94.into()
11+
}
12+
}
13+
14+
impl From<f64> for Decimal128 {
15+
fn from(value: f64) -> Self {
16+
Decimal128(BigDecimal::try_from(value).unwrap())
17+
}
18+
}
19+
20+
#[cfg(test)]
21+
mod test {
22+
use super::*;
23+
24+
#[test]
25+
fn construct_decimal_128() {
26+
let val: Decimal128 = 128.0.into();
27+
assert_eq!(val.encode().constructor(), 0x94);
28+
}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use bigdecimal::BigDecimal;
2+
3+
use crate::serde::encode::{Encode, Encoded};
4+
5+
#[derive(Hash, Eq, PartialEq)]
6+
pub struct Decimal32(BigDecimal);
7+
8+
impl Encode for Decimal32 {
9+
fn encode(&self) -> Encoded {
10+
0x74.into()
11+
}
12+
}
13+
impl From<f32> for Decimal32 {
14+
fn from(value: f32) -> Self {
15+
Decimal32(BigDecimal::try_from(value).unwrap())
16+
}
17+
}
18+
#[cfg(test)]
19+
mod test {
20+
use super::*;
21+
22+
#[test]
23+
fn construct_decimal_32() {
24+
let val: Decimal32 = 32.0.into();
25+
assert_eq!(val.encode().constructor(), 0x74);
26+
}
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use bigdecimal::BigDecimal;
2+
3+
use crate::serde::encode::{Encode, Encoded};
4+
5+
#[derive(Hash, Eq, PartialEq)]
6+
pub struct Decimal64(BigDecimal);
7+
8+
impl Encode for Decimal64 {
9+
fn encode(&self) -> Encoded {
10+
0x84.into()
11+
}
12+
}
13+
14+
impl From<f64> for Decimal64 {
15+
fn from(value: f64) -> Self {
16+
Decimal64(BigDecimal::try_from(value).unwrap())
17+
}
18+
}
19+
20+
#[cfg(test)]
21+
mod test {
22+
use super::*;
23+
24+
#[test]
25+
fn construct_decimal_64() {
26+
let val: Decimal64 = 64.0.into();
27+
assert_eq!(val.encode().constructor(), 0x84);
28+
}
29+
}

amqp-type/src/fixed_width/double.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use crate::serde::encode::{Encode, Encoded};
22
use std::hash::Hash;
33

4+
use crate::error::AppError;
45
use crate::serde::decode::Decode;
56
use crate::verify::verify_bytes_read_eq;
6-
use crate::error::AppError;
7-
87

98
/// Crate assumes nothing about the values being passed to it.
109
/// Any kind of f64 value is handled as is.
@@ -46,19 +45,18 @@ impl From<f64> for Double {
4645
}
4746
}
4847

49-
5048
impl Decode for f64 {
51-
5249
fn can_decode(iter: impl Iterator<Item = u8>) -> bool {
5350
match iter.peekable().peek() {
5451
Some(&DEFAULT_CONSTR) => true,
55-
_ => false
52+
_ => false,
5653
}
5754
}
5855

5956
fn try_decode(mut iter: impl Iterator<Item = u8>) -> Result<Self, crate::error::AppError>
60-
where
61-
Self: Sized {
57+
where
58+
Self: Sized,
59+
{
6260
match iter.next() {
6361
Some(DEFAULT_CONSTR) => Ok(parse_f64(iter)?),
6462
Some(c) => Err(AppError::DeserializationIllegalConstructorError(c)),
@@ -78,21 +76,18 @@ fn parse_f64(iter: impl Iterator<Item = u8>) -> Result<f64, AppError> {
7876
Ok(f64::from_be_bytes(byte_vals))
7977
}
8078

81-
8279
#[cfg(test)]
8380
mod test {
8481

8582
use super::*;
8683

87-
8884
#[test]
8985
fn construct_double() {
9086
let val: Double = 64.0.into();
9187
assert_eq!(val.encode().constructor(), 0x82);
9288
}
9389

94-
95-
#[test]
90+
#[test]
9691
fn can_deocde_returns_true_if_constructor_is_valid() {
9792
let val_norm = vec![0x82];
9893
assert_eq!(f64::can_decode(val_norm.into_iter()), true);
@@ -106,8 +101,8 @@ mod test {
106101

107102
#[test]
108103
fn try_decode_returns_correct_value() {
109-
let val = vec![0x82, 0x40, 0x20, 0x00, 0x00, 0x41, 0x70,0x00, 0x10];
110-
assert_eq!(f64::try_decode(val.into_iter()).unwrap(), 8.0000019501895) ;
104+
let val = vec![0x82, 0x40, 0x20, 0x00, 0x00, 0x41, 0x70, 0x00, 0x10];
105+
assert_eq!(f64::try_decode(val.into_iter()).unwrap(), 8.0000019501895);
111106
}
112107

113108
#[test]
@@ -121,6 +116,4 @@ mod test {
121116
let val = vec![0x82, 0x00, 0x01];
122117
assert!(f64::try_decode(val.into_iter()).is_err());
123118
}
124-
125-
126-
}
119+
}

0 commit comments

Comments
 (0)