Skip to content

Commit

Permalink
fix nested struct serialization (FactbirdHQ#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
c-h-johnson authored Jul 21, 2023
1 parent 2ac0523 commit 2e5538f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
36 changes: 33 additions & 3 deletions serde_at/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl fmt::Display for Error {
pub(crate) struct Serializer<'a> {
buf: &'a mut [u8],
written: usize,
nested_struct: bool,
cmd: &'a str,
options: SerializeOptions<'a>,
}
Expand All @@ -75,6 +76,7 @@ impl<'a> Serializer<'a> {
Serializer {
buf,
written: 0,
nested_struct: false,
cmd,
options,
}
Expand Down Expand Up @@ -357,9 +359,16 @@ impl<'a, 'b> ser::Serializer for &'a mut Serializer<'b> {
}

fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
self.extend_from_slice(self.options.cmd_prefix.as_bytes())?;
self.extend_from_slice(self.cmd.as_bytes())?;
Ok(SerializeStruct::new(self))
let ser_struct = if !self.nested_struct {
// all calls to serialize_struct after this one will be nested structs
self.nested_struct = true;
self.extend_from_slice(self.options.cmd_prefix.as_bytes())?;
self.extend_from_slice(self.cmd.as_bytes())?;
SerializeStruct::new(self, false)
} else {
SerializeStruct::new(self, true)
};
Ok(ser_struct)
}

fn serialize_struct_variant(
Expand Down Expand Up @@ -612,6 +621,27 @@ mod tests {
assert_eq!(s, String::<32>::from("AT+CMD=Some bytes\r\n"));
}

#[test]
fn nested_struct() {
#[derive(Clone, PartialEq, Serialize)]
pub struct Inner<'a> {
s: &'a str,
}

#[derive(Clone, PartialEq, Serialize)]
pub struct Outer<'a> {
inner: Inner<'a>,
}

let value = Outer {
inner: Inner { s: "value" },
};

let s: String<32> = to_string(&value, "+CMD", SerializeOptions::default()).unwrap();

assert_eq!(s, String::<32>::from("AT+CMD=\"value\"\r\n"));
}

#[test]
fn hex_str_serialize() {
#[derive(Clone, PartialEq, Serialize)]
Expand Down
17 changes: 12 additions & 5 deletions serde_at/src/ser/struct_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ use serde::ser;
#[allow(clippy::module_name_repetitions)]
pub struct SerializeStruct<'a, 'b> {
ser: &'a mut Serializer<'b>,
nested: bool,
first: bool,
}

impl<'a, 'b> SerializeStruct<'a, 'b> {
pub(crate) fn new(ser: &'a mut Serializer<'b>) -> Self {
SerializeStruct { ser, first: true }
pub(crate) fn new(ser: &'a mut Serializer<'b>, nested: bool) -> Self {
SerializeStruct {
ser,
nested,
first: true,
}
}
}

Expand All @@ -22,7 +27,7 @@ impl<'a, 'b> ser::SerializeStruct for SerializeStruct<'a, 'b> {
T: ser::Serialize + ?Sized,
{
if self.first {
if self.ser.options.value_sep {
if !self.nested && self.ser.options.value_sep {
self.ser.push(b'=')?;
}
} else {
Expand All @@ -35,8 +40,10 @@ impl<'a, 'b> ser::SerializeStruct for SerializeStruct<'a, 'b> {
}

fn end(self) -> Result<Self::Ok> {
self.ser
.extend_from_slice(self.ser.options.termination.as_bytes())?;
if !self.nested {
self.ser
.extend_from_slice(self.ser.options.termination.as_bytes())?;
}
Ok(())
}
}

0 comments on commit 2e5538f

Please sign in to comment.