From b01b6c210d830c209ba3c50a5a6eb4cbeee1dfe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Pakalns?= Date: Wed, 8 Sep 2021 12:28:33 +0300 Subject: [PATCH 1/2] Fix bug in SerdeDerive macro implementation --- serde-diff-derive/src/serde_diff/mod.rs | 3 +++ src/difference.rs | 5 +++++ src/tests.rs | 14 ++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/serde-diff-derive/src/serde_diff/mod.rs b/serde-diff-derive/src/serde_diff/mod.rs index 561ad27..37185bc 100644 --- a/serde-diff-derive/src/serde_diff/mod.rs +++ b/serde-diff-derive/src/serde_diff/mod.rs @@ -332,6 +332,9 @@ fn generate( match (self, other) { #(#diff_match_arms)* } + if (__changed__) { + ctx.save_exit()?; + } Ok(__changed__) } } diff --git a/src/difference.rs b/src/difference.rs index ec80813..ff84c33 100644 --- a/src/difference.rs +++ b/src/difference.rs @@ -148,6 +148,11 @@ impl<'a, S: SerializeSeq> DiffContext<'a, S> { self.save_command(&DiffCommandRef::Value(value), true, true) } + /// Write exit command + pub fn save_exit(&mut self) -> Result<(), S::Error> { + self.save_command::<()>(&DiffCommandRef::Exit, true, false) + } + /// Stores an arbitrary DiffCommand to be handled by the type. /// Any custom sequence of DiffCommands must be followed by Exit. pub fn save_command<'b, T: Serialize>( diff --git a/src/tests.rs b/src/tests.rs index 082846b..19aa468 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,7 +1,9 @@ use crate as serde_diff; use crate::{Apply, Diff, SerdeDiff}; use serde::{Deserialize, Serialize}; +use std::collections::HashMap; use std::fmt::Debug; +use std::iter::FromIterator; #[derive(SerdeDiff, Serialize, Deserialize, PartialEq, Debug, Copy, Clone)] struct TestStruct { @@ -59,6 +61,18 @@ fn test_option() { Some(TestStruct { a: 52, b: 32. }), Some(TestStruct { a: 42, b: 12. }), ); + roundtrip( + HashMap::from_iter([ + (1, TestStruct { a: 1, b: 1. }), + (2, TestStruct { a: 2, b: 2. }), + (3, TestStruct { a: 3, b: 3. }), + ]), + HashMap::from_iter([ + (1, TestStruct { a: 1, b: 1. }), + (3, TestStruct { a: 4, b: 4. }), + (4, TestStruct { a: 1, b: 1. }), + ]), + ); partial( Some(TestStruct { a: 5, b: 2. }), From b6f0780713cb0e879ce97b11745c1facb49d66e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Pakalns?= Date: Thu, 13 Apr 2023 19:54:06 +0300 Subject: [PATCH 2/2] Update serde-diff-derive/src/serde_diff/mod.rs Co-authored-by: Alain Zscheile --- serde-diff-derive/src/serde_diff/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/serde-diff-derive/src/serde_diff/mod.rs b/serde-diff-derive/src/serde_diff/mod.rs index 37185bc..bef2376 100644 --- a/serde-diff-derive/src/serde_diff/mod.rs +++ b/serde-diff-derive/src/serde_diff/mod.rs @@ -332,7 +332,7 @@ fn generate( match (self, other) { #(#diff_match_arms)* } - if (__changed__) { + if __changed__ { ctx.save_exit()?; } Ok(__changed__)