Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions serde-diff-derive/src/serde_diff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ fn generate(
match (self, other) {
#(#diff_match_arms)*
}
if __changed__ {
ctx.save_exit()?;
}
Ok(__changed__)
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/difference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand Down
14 changes: 14 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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. }),
Expand Down