-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
87 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
[package] | ||
name = "serde-diff" | ||
version = "0.4.1" | ||
authors = ["Karl Bergström <[email protected]>", "Philip Degarmo <[email protected]>"] | ||
authors = [ | ||
"Karl Bergström <[email protected]>", | ||
"Philip Degarmo <[email protected]>", | ||
] | ||
readme = "README.md" | ||
exclude = ["examples/*"] | ||
license = "Apache-2.0 OR MIT" | ||
|
@@ -17,10 +20,11 @@ maintenance = { status = "experimental" } | |
|
||
[dependencies] | ||
serde-diff-derive = { version = "0.4.0", path = "serde-diff-derive" } | ||
serde = { version = "1", features = [ "derive" ] } | ||
serde_derive = { version = "1", features = ["deserialize_in_place"]} | ||
serde = { version = "1", features = ["derive"] } | ||
serde_derive = { version = "1", features = ["deserialize_in_place"] } | ||
|
||
[dev-dependencies] | ||
serde_json = "1.0" | ||
bincode = "1.2" | ||
rmp-serde = "0.15.0" | ||
maplit = "1.0.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use maplit::hashmap; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_diff::{Apply, Diff, SerdeDiff}; | ||
use std::collections::HashMap; | ||
|
||
#[derive(SerdeDiff, Serialize, Deserialize, Debug, Default, PartialEq, Clone)] | ||
struct TestValue { | ||
v: String, | ||
} | ||
|
||
#[derive(SerdeDiff, Serialize, Deserialize, Debug, Default, PartialEq, Clone)] | ||
struct TestStruct { | ||
test: bool, | ||
map: HashMap<String, HashMap<i32, TestValue>>, | ||
} | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let mut empty = TestStruct::default(); | ||
empty.test = true; | ||
|
||
let mut hello_world = TestStruct::default(); | ||
hello_world.map.insert( | ||
"hello".to_string(), | ||
hashmap![0 => TestValue { v: "world".to_string()}], | ||
); | ||
|
||
let mut hi_world = TestStruct::default(); | ||
hi_world.map.insert( | ||
"hi".to_string(), | ||
hashmap![0 => TestValue { v: "world".to_string()}], | ||
); | ||
|
||
let mut hi_world_and_planet = TestStruct::default(); | ||
hi_world_and_planet.map.insert( | ||
"hi".to_string(), | ||
hashmap![0 => TestValue { v: "world".to_string()}, 1 => TestValue { v: "planet".to_string()}], | ||
); | ||
|
||
let mut hi_planet = TestStruct::default(); | ||
hi_planet.map.insert( | ||
"hi".to_string(), | ||
hashmap![0 => TestValue { v: "planet".to_string()}], | ||
); | ||
|
||
let mut hi_planet_hello_world = TestStruct::default(); | ||
hi_planet_hello_world.map.insert( | ||
"hi".to_string(), | ||
hashmap![0 => TestValue { v: "planet".to_string()}], | ||
); | ||
hi_planet_hello_world.map.insert( | ||
"hello".to_string(), | ||
hashmap![0 => TestValue { v: "world".to_string()}], | ||
); | ||
|
||
let add_hello = serde_json::to_string(&Diff::serializable(&empty, &hello_world))?; | ||
let hello_to_hi = serde_json::to_string(&Diff::serializable(&hello_world, &hi_world))?; | ||
let add_planet = serde_json::to_string(&Diff::serializable(&hi_world, &hi_world_and_planet))?; | ||
let del_world = serde_json::to_string(&Diff::serializable(&hi_world_and_planet, &hi_planet))?; | ||
let no_change = serde_json::to_string(&Diff::serializable(&hi_planet, &hi_planet))?; | ||
let add_world = serde_json::to_string(&Diff::serializable(&hi_planet, &hi_planet_hello_world))?; | ||
|
||
let mut built = TestStruct::default(); | ||
for (diff, after) in &[ | ||
(add_hello, hello_world), | ||
(hello_to_hi, hi_world), | ||
(add_planet, hi_world_and_planet), | ||
(del_world, hi_planet.clone()), | ||
(no_change, hi_planet), | ||
(add_world, hi_planet_hello_world), | ||
] { | ||
println!("{}", diff); | ||
|
||
let mut deserializer = serde_json::Deserializer::from_str(&diff); | ||
Apply::apply(&mut deserializer, &mut built)?; | ||
|
||
assert_eq!(after, &built); | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters