Skip to content

Commit

Permalink
fix clippy warning
Browse files Browse the repository at this point in the history
  • Loading branch information
gftea committed Oct 21, 2023
1 parent 15a869e commit 233c978
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 65 deletions.
90 changes: 50 additions & 40 deletions amqp_serde/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,19 @@ struct DataSequence<'a, 'de: 'a> {

impl<'a, 'de> DataSequence<'a, 'de> {
fn new(de: &'a mut Deserializer<'de>, len: usize) -> Self {
DataSequence { de, len, is_struct: false }
DataSequence {
de,
len,
is_struct: false,
}
}

fn new_struct(de: &'a mut Deserializer<'de>, len: usize) -> Self {
DataSequence { de, len, is_struct: true }
DataSequence {
de,
len,
is_struct: true,
}
}
}

Expand Down Expand Up @@ -602,44 +610,46 @@ mod tests {
#[test]
fn test_array() {
let input = vec![
0x41, 0x00, 0x00, 0x01, 0x7c, 0x53, 0x00, 0x00, 0x00, 0x32, 0x7b, 0x3c, 0x3c, 0x22, 0x63, 0x6f,
0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3e, 0x3e,
0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x30, 0x45, 0x30, 0x45,
0x30, 0x45, 0x42, 0x30, 0x42, 0x30, 0x32, 0x30, 0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00,
0x26, 0x7b, 0x3c, 0x3c, 0x22, 0x63, 0x6f, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x22, 0x3e,
0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x47, 0x61, 0x6c,
0x67, 0x75, 0x73, 0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, 0x25, 0x7b, 0x3c, 0x3c, 0x22,
0x68, 0x6f, 0x73, 0x74, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67,
0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x47, 0x61, 0x6c, 0x67, 0x75, 0x73, 0x22, 0x3e, 0x3e,
0x7d, 0x53, 0x00, 0x00, 0x00, 0x21, 0x7b, 0x3c, 0x3c, 0x22, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63,
0x74, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22,
0x43, 0x48, 0x54, 0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, 0x38, 0x7b, 0x3c, 0x3c, 0x22,
0x69, 0x6e, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x3e, 0x3e, 0x2c, 0x6c,
0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a,
0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x67, 0x61, 0x6c, 0x67, 0x75, 0x73, 0x2e, 0x6e, 0x65, 0x74,
0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, 0x29, 0x7b, 0x3c, 0x3c, 0x22, 0x76, 0x65, 0x72,
0x73, 0x69, 0x6f, 0x6e, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c,
0x3c, 0x3c, 0x22, 0x4d, 0x6f, 0x63, 0x6b, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x22, 0x3e,
0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, 0x29, 0x7b, 0x3c, 0x3c, 0x22, 0x70, 0x6c, 0x61, 0x74, 0x66,
0x6f, 0x72, 0x6d, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c,
0x3c, 0x22, 0x41, 0x58, 0x31, 0x38, 0x30, 0x30, 0x59, 0x43, 0x58, 0x54, 0x22, 0x3e, 0x3e, 0x7d,
0x53, 0x00, 0x00, 0x00, 0x2c, 0x7b, 0x3c, 0x3c, 0x22, 0x65, 0x63, 0x6f, 0x76, 0x65, 0x72, 0x73,
0x69, 0x6f, 0x6e, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c,
0x3c, 0x22, 0x4d, 0x6f, 0x63, 0x6b, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x22, 0x3e, 0x3e,
0x7d,
0x41, 0x00, 0x00, 0x01, 0x7c, 0x53, 0x00, 0x00, 0x00, 0x32, 0x7b, 0x3c, 0x3c, 0x22,
0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x6e, 0x61, 0x6d,
0x65, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c,
0x3c, 0x22, 0x30, 0x45, 0x30, 0x45, 0x30, 0x45, 0x42, 0x30, 0x42, 0x30, 0x32, 0x30,
0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, 0x26, 0x7b, 0x3c, 0x3c, 0x22, 0x63,
0x6f, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f,
0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x47, 0x61, 0x6c, 0x67, 0x75,
0x73, 0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, 0x25, 0x7b, 0x3c, 0x3c, 0x22,
0x68, 0x6f, 0x73, 0x74, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f,
0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x47, 0x61, 0x6c, 0x67, 0x75,
0x73, 0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, 0x21, 0x7b, 0x3c, 0x3c, 0x22,
0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e,
0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x43, 0x48, 0x54, 0x22, 0x3e, 0x3e,
0x7d, 0x53, 0x00, 0x00, 0x00, 0x38, 0x7b, 0x3c, 0x3c, 0x22, 0x69, 0x6e, 0x66, 0x6f,
0x72, 0x6d, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e,
0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a,
0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x67, 0x61, 0x6c, 0x67, 0x75, 0x73, 0x2e, 0x6e,
0x65, 0x74, 0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, 0x29, 0x7b, 0x3c, 0x3c,
0x22, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f,
0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x4d, 0x6f, 0x63, 0x6b, 0x53,
0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00,
0x29, 0x7b, 0x3c, 0x3c, 0x22, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x22,
0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22,
0x41, 0x58, 0x31, 0x38, 0x30, 0x30, 0x59, 0x43, 0x58, 0x54, 0x22, 0x3e, 0x3e, 0x7d,
0x53, 0x00, 0x00, 0x00, 0x2c, 0x7b, 0x3c, 0x3c, 0x22, 0x65, 0x63, 0x6f, 0x76, 0x65,
0x72, 0x73, 0x69, 0x6f, 0x6e, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73,
0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x4d, 0x6f, 0x63, 0x6b, 0x53, 0x65, 0x72, 0x76,
0x69, 0x63, 0x65, 0x22, 0x3e, 0x3e, 0x7d,
];
let f_array = FieldArray::try_from(
vec!(
FieldValue::from("{<<\"connection_name\">>,longstr,<<\"0E0E0EB0B020\">>}"),
FieldValue::from("{<<\"copyright\">>,longstr,<<\"Galgus\">>}"),
FieldValue::from("{<<\"hostname\">>,longstr,<<\"Galgus\">>}"),
FieldValue::from("{<<\"product\">>,longstr,<<\"CHT\">>}"),
FieldValue::from("{<<\"information\">>,longstr,<<\"https://www.galgus.net\">>}"),
FieldValue::from("{<<\"version\">>,longstr,<<\"MockService\">>}"),
FieldValue::from("{<<\"platform\">>,longstr,<<\"AX1800YCXT\">>}"),
FieldValue::from("{<<\"ecoversion\">>,longstr,<<\"MockService\">>}"),
)
).unwrap();
let f_array = FieldArray::try_from(vec![
FieldValue::from("{<<\"connection_name\">>,longstr,<<\"0E0E0EB0B020\">>}"),
FieldValue::from("{<<\"copyright\">>,longstr,<<\"Galgus\">>}"),
FieldValue::from("{<<\"hostname\">>,longstr,<<\"Galgus\">>}"),
FieldValue::from("{<<\"product\">>,longstr,<<\"CHT\">>}"),
FieldValue::from("{<<\"information\">>,longstr,<<\"https://www.galgus.net\">>}"),
FieldValue::from("{<<\"version\">>,longstr,<<\"MockService\">>}"),
FieldValue::from("{<<\"platform\">>,longstr,<<\"AX1800YCXT\">>}"),
FieldValue::from("{<<\"ecoversion\">>,longstr,<<\"MockService\">>}"),
])
.unwrap();
let expected = FieldValue::A(f_array);
let result: FieldValue = from_bytes(&input).unwrap();
assert_eq!(expected, result);
Expand Down Expand Up @@ -803,6 +813,6 @@ mod tests {
assert_eq!('€', result.m_char.1);
assert_eq!(b"beef".to_vec(), result.m_owned_bytes.1);
assert_eq!(Some(b'o'), result.m_opt);
assert_eq!((), result.m_unit);
assert_eq!(0, std::mem::size_of_val(&result.m_unit));
}
}
58 changes: 33 additions & 25 deletions amqp_serde/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
//! See [RabbitMQ's Definition](https://github.com/rabbitmq/rabbitmq-codegen/blob/main/amqp-rabbitmq-0.9.1.json).
//!
//! See [RabbitMQ errata](https://www.rabbitmq.com/amqp-0-9-1-errata.html)
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
convert::TryFrom,
fmt::{self, Debug},
mem::{size_of, size_of_val},
num::TryFromIntError,
};
use serde::{Deserialize, Serialize};

/// DO NOT USE. No primitive rust type to represent single bit.
///
Expand Down Expand Up @@ -209,7 +209,9 @@ impl TryFrom<Vec<FieldValue>> for FieldArray {
type Error = TryFromIntError;

fn try_from(values: Vec<FieldValue>) -> Result<Self, Self::Error> {
let total_bytes = values.iter().fold(0, |acc, v| acc + FieldValue::TAG_SIZE + v.len());
let total_bytes = values
.iter()
.fold(0, |acc, v| acc + FieldValue::TAG_SIZE + v.len());
let len = LongUint::try_from(total_bytes)?;
Ok(Self(len, values))
}
Expand Down Expand Up @@ -282,23 +284,23 @@ impl FieldValue {

fn len(&self) -> usize {
match self {
Self::V => 0, // fixed size
Self::t(_) => size_of::<Boolean>(), // fixed size
Self::b(_) => size_of::<ShortShortInt>(), // fixed size
Self::B(_) => size_of::<ShortShortUint>(), // fixed size
Self::s(_) => size_of::<ShortInt>(), // fixed size
Self::u(_) => size_of::<ShortUint>(), // fixed size
Self::I(_) => size_of::<LongInt>(), // fixed size
Self::i(_) => size_of::<LongUint>(), // fixed size
Self::l(_) => size_of::<LongLongInt>(), // fixed size
Self::f(_) => size_of::<Float>(), // fixed size
Self::d(_) => size_of::<Double>(), // fixed size
Self::T(_) => size_of::<TimeStamp>(), // fixed size
Self::V => 0, // fixed size
Self::t(_) => size_of::<Boolean>(), // fixed size
Self::b(_) => size_of::<ShortShortInt>(), // fixed size
Self::B(_) => size_of::<ShortShortUint>(), // fixed size
Self::s(_) => size_of::<ShortInt>(), // fixed size
Self::u(_) => size_of::<ShortUint>(), // fixed size
Self::I(_) => size_of::<LongInt>(), // fixed size
Self::i(_) => size_of::<LongUint>(), // fixed size
Self::l(_) => size_of::<LongLongInt>(), // fixed size
Self::f(_) => size_of::<Float>(), // fixed size
Self::d(_) => size_of::<Double>(), // fixed size
Self::T(_) => size_of::<TimeStamp>(), // fixed size
Self::D(v) => size_of_val(&v.0) + size_of_val(&v.1), // fixed size
Self::S(v) => size_of_val(&v.0) + v.0 as usize, // variable size
Self::A(v) => size_of_val(&v.0) + v.0 as usize, // variable size
Self::F(v) => size_of_val(&v.0) + v.0 as usize, // variable size
Self::x(v) => size_of_val(&v.0) + v.0 as usize, // variable size
Self::S(v) => size_of_val(&v.0) + v.0 as usize, // variable size
Self::A(v) => size_of_val(&v.0) + v.0 as usize, // variable size
Self::F(v) => size_of_val(&v.0) + v.0 as usize, // variable size
Self::x(v) => size_of_val(&v.0) + v.0 as usize, // variable size
}
}
}
Expand Down Expand Up @@ -384,7 +386,6 @@ impl<'a> TryInto<&'a str> for &'a FieldValue {
}
}


/// RabbitMQ's field value support only long string variant, so rust string type
/// always converted to long string variant.
impl From<String> for FieldValue {
Expand Down Expand Up @@ -446,13 +447,18 @@ impl FieldTable {
}

pub fn insert(&mut self, k: FieldName, v: FieldValue) -> Option<FieldValue> {
self.0 += LongUint::try_from(size_of_val(&k.0) + k.0 as usize + FieldValue::TAG_SIZE + v.len()).unwrap();
self.0 +=
LongUint::try_from(size_of_val(&k.0) + k.0 as usize + FieldValue::TAG_SIZE + v.len())
.unwrap();
self.1.insert(k, v)
}

pub fn remove(&mut self, k: &FieldName) -> Option<FieldValue> {
if let Some(v) = self.1.remove(k) {
self.0 -= LongUint::try_from(size_of_val(&k.0) + k.0 as usize + FieldValue::TAG_SIZE + v.len()).unwrap();
self.0 -= LongUint::try_from(
size_of_val(&k.0) + k.0 as usize + FieldValue::TAG_SIZE + v.len(),
)
.unwrap();
Some(v)
} else {
None
Expand Down Expand Up @@ -588,8 +594,8 @@ pub type AmqpTimeStamp = TimeStamp;
/////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod tests {
use std::mem::size_of;
use crate::types::{ByteArray, DecimalValue, FieldArray, FieldValue, LongStr, LongUint};
use std::mem::size_of;

use super::{FieldTable, ShortStr};
#[test]
Expand Down Expand Up @@ -657,7 +663,7 @@ mod tests {
let exp = FieldValue::t(true);
assert_eq!(exp, true.into());
let t: bool = exp.try_into().unwrap();
assert_eq!(true, t);
assert!(t);

let exp = FieldValue::F(FieldTable::default());
assert_eq!(exp, FieldTable::default().into());
Expand Down Expand Up @@ -699,7 +705,8 @@ mod tests {
FieldValue::S(s4),
];

v.iter().for_each(|_fv| total_length += FieldValue::TAG_SIZE + size_of::<LongUint>());
v.iter()
.for_each(|_fv| total_length += FieldValue::TAG_SIZE + size_of::<LongUint>());

let a = FieldArray::try_from(v).unwrap();

Expand All @@ -723,7 +730,8 @@ mod tests {
];
let mut total_length = 0;
v_kv.into_iter().for_each(|(k, v)| {
total_length += k.1.len() + v.1.len() + 2 * FieldValue::TAG_SIZE + size_of::<LongUint>();
total_length +=
k.1.len() + v.1.len() + 2 * FieldValue::TAG_SIZE + size_of::<LongUint>();
ft.insert(k, FieldValue::S(v));
});

Expand Down

0 comments on commit 233c978

Please sign in to comment.