Skip to content

Commit

Permalink
Fix SizeSerializer not creating new instance of SizeSerializer fo…
Browse files Browse the repository at this point in the history
…r nested types (#280)

* fix seq element not serialized by new SizeSerializer

* always create new SizeSerializer when serialize_field etc

* cargo fmt

* bumped version and updated changelog
  • Loading branch information
minghuaw authored Aug 23, 2024
1 parent 236be00 commit 660feb7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
2 changes: 1 addition & 1 deletion serde_amqp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde_amqp"
version = "0.12.1"
version = "0.12.2"
edition = "2021"
description = "A serde implementation of AMQP1.0 protocol."
license = "MIT/Apache-2.0"
Expand Down
4 changes: 4 additions & 0 deletions serde_amqp/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 0.12.2

1. Fixed bug caused by not creating new `SizeSerializer` in `SerializeSeq::serialize_element` (issue #279)

## 0.12.1

1. Fixed empty `Array` not deserialized properly (issue #277)
Expand Down
54 changes: 42 additions & 12 deletions serde_amqp/src/size_ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl<'a> ser::Serializer for &'a mut SizeSerializer {
IsArrayElement::FirstElement => Ok(9),
IsArrayElement::OtherElement => Ok(8),
},
_ => unreachable!(),
_ => unreachable!("serialize_i64 is only used for Long and Timestamp"),
}
}

Expand Down Expand Up @@ -221,15 +221,15 @@ impl<'a> ser::Serializer for &'a mut SizeSerializer {
U8_MAX..=U32_MAX_MINUS_4 => Ok(5 + v.len()),
_ => Err(Error::too_long()),
},
_ => unreachable!(),
_ => unreachable!("serialize_str is only used for Symbol and String"),
},
IsArrayElement::FirstElement => match self.new_type {
NewType::Symbol | NewType::SymbolRef | NewType::None => Ok(5 + v.len()),
_ => unreachable!(),
_ => unreachable!("serialize_str is only used for Symbol and String"),
},
IsArrayElement::OtherElement => match self.new_type {
NewType::Symbol | NewType::SymbolRef | NewType::None => Ok(4 + v.len()),
_ => unreachable!(),
_ => unreachable!("serialize_str is only used for Symbol and String"),
},
}
}
Expand Down Expand Up @@ -270,7 +270,7 @@ impl<'a> ser::Serializer for &'a mut SizeSerializer {
| NewType::Array
| NewType::Symbol
| NewType::SymbolRef
| NewType::TransparentVec => unreachable!(),
| NewType::TransparentVec => unreachable!("serialize_bytes is only used for Binary, Decimal32, Decimal64, Decimal128, and Uuid"),
}
}

Expand Down Expand Up @@ -452,7 +452,8 @@ impl<'a> ser::SerializeSeq for SeqSerializer<'a> {
{
match self.se.new_type {
NewType::None => {
self.cumulated_size += value.serialize(&mut *self.se)?;
let mut serializer = SizeSerializer::new();
self.cumulated_size += value.serialize(&mut serializer)?;
}
NewType::Array => {
let mut serializer = SizeSerializer::new();
Expand All @@ -472,7 +473,9 @@ impl<'a> ser::SerializeSeq for SeqSerializer<'a> {
| NewType::Symbol
| NewType::SymbolRef
| NewType::Timestamp
| NewType::Uuid => unreachable!(),
| NewType::Uuid => {
unreachable!("SeqSerializer is only used for List, Array, and TransparentVec")
}
}

self.idx += 1;
Expand All @@ -495,7 +498,9 @@ impl<'a> ser::SerializeSeq for SeqSerializer<'a> {
| NewType::Symbol
| NewType::SymbolRef
| NewType::Timestamp
| NewType::Uuid => unreachable!(),
| NewType::Uuid => {
unreachable!("SeqSerializer is only used for List, Array, and TransparentVec")
}
}
}
}
Expand Down Expand Up @@ -702,7 +707,9 @@ impl<'a> ser::SerializeTupleStruct for TupleStructSerializer<'a> {
self.cumulated_size += value.serialize(&mut serializer)?;
Ok(())
}
StructEncoding::DescribedMap => unreachable!(),
StructEncoding::DescribedMap => {
unreachable!("TupleStructSerializer is NOT used for DescribedMap")
}
},
}
}
Expand All @@ -720,7 +727,9 @@ impl<'a> ser::SerializeTupleStruct for TupleStructSerializer<'a> {
let _ = self.se.struct_encoding.pop();
Ok(self.cumulated_size)
}
StructEncoding::DescribedMap => unreachable!(),
StructEncoding::DescribedMap => {
unreachable!("TupleStructSerializer is NOT used for DescribedMap")
}
}
}
}
Expand Down Expand Up @@ -774,7 +783,8 @@ impl<'a> ser::SerializeStruct for StructSerializer<'a> {
Ok(())
}
StructEncoding::DescribedBasic => {
self.cumulated_size += value.serialize(&mut *self.se)?;
let mut serializer = SizeSerializer::new();
self.cumulated_size += value.serialize(&mut serializer)?;
Ok(())
}
}
Expand Down Expand Up @@ -829,7 +839,8 @@ impl<'a> ser::SerializeTupleVariant for VariantSerializer<'a> {
where
T: ser::Serialize + ?Sized,
{
self.cumulated_size += value.serialize(&mut *self.se)?;
let mut serializer = SizeSerializer::new();
self.cumulated_size += value.serialize(&mut serializer)?;
Ok(())
}

Expand Down Expand Up @@ -1288,4 +1299,23 @@ mod tests {
let buf = to_vec(&value).unwrap();
assert_eq!(ssize, buf.len());
}

#[test]
fn serialized_size_of_described_list_of_timestamps() {
use crate::{described::Described, descriptor::Descriptor, primitives::*, Value};

let timestamp = Timestamp::from_milliseconds(12345);
let mut list = List::new();
list.push(Value::Timestamp(timestamp));

let described = Described {
descriptor: Descriptor::Code(0x73),
value: Value::List(list),
};

let value = Value::Described(Box::new(described));

let size_result = serialized_size(&value);
assert!(size_result.is_ok());
}
}

0 comments on commit 660feb7

Please sign in to comment.