-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathserialization.rs
173 lines (154 loc) · 4.61 KB
/
serialization.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2023, Igor Shaula
// Licensed under the MIT License <LICENSE or
// http://opensource.org/licenses/MIT>. This file
// may not be copied, modified, or distributed
// except according to those terms.
#![cfg(feature = "serialization-serde")]
use serde_derive::{Deserialize, Serialize};
use std::collections::HashMap;
mod common;
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Coords {
x: u32,
y: u32,
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Size {
w: u32,
h: u32,
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct Rectangle {
coords: Option<Coords>,
size: Size,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct AllFields {
t_bool: bool,
t_u8: u8,
t_u16: u16,
t_u32: u32,
t_u64: u64,
t_usize: usize,
t_rect_no_coords: Rectangle,
t_rect_with_coords: Rectangle,
t_string: String,
t_map: HashMap<String, HashMap<String, u32>>,
t_optional_u32_none: Option<u32>,
t_optional_u32_some: Option<u32>,
t_optional_map_none: Option<HashMap<String, u32>>,
t_optional_map_some: Option<HashMap<String, u32>>,
t_i8: i8,
t_i16: i16,
t_i32: i32,
t_i64: i64,
t_isize: isize,
t_f64: f64,
t_f32: f32,
t_char: char,
#[serde(with = "serde_bytes")]
t_bytes: Vec<u8>,
}
impl AllFields {
pub fn test_val() -> Self {
let mut k1 = HashMap::new();
k1.insert("val1".to_owned(), 32);
k1.insert("val2".to_owned(), 64);
k1.insert("val3".to_owned(), 128);
let mut k2 = HashMap::new();
k2.insert("val1".to_owned(), 256);
k2.insert("val2".to_owned(), 512);
k2.insert("val3".to_owned(), 1024);
let mut map = HashMap::new();
map.insert("key1".to_owned(), k1.clone());
map.insert("key2".to_owned(), k2);
AllFields {
t_bool: false,
t_u8: 127,
t_u16: 32768,
t_u32: 123_456_789,
t_u64: 123_456_789_101_112,
t_usize: 1_234_567_891,
t_rect_no_coords: Rectangle {
coords: None,
size: Size { w: 320, h: 240 },
},
t_rect_with_coords: Rectangle {
coords: Some(Coords { x: 55, y: 77 }),
size: Size { w: 500, h: 300 },
},
t_map: map,
t_optional_u32_none: None,
t_optional_u32_some: Some(1234),
t_optional_map_none: None,
t_optional_map_some: Some(k1),
t_string: "Test123 \n$%^&|+-*/\\()".to_owned(),
t_i8: -123,
t_i16: -2049,
t_i32: 20100,
t_i64: -12_345_678_910,
t_isize: -1_234_567_890,
t_f64: -0.01,
t_f32: 3.15,
t_char: 'a',
t_bytes: vec![0xDE, 0xAD, 0xBE, 0xEF],
}
}
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct SomeFields {
t_usize: usize,
t_rect_no_coords: Rectangle,
t_string: String,
t_u32: Option<u32>,
t_none: Option<u32>,
}
impl PartialEq<AllFields> for SomeFields {
fn eq(&self, other: &AllFields) -> bool {
*self.t_string == other.t_string
&& self.t_usize == other.t_usize
&& self.t_rect_no_coords == other.t_rect_no_coords
&& self.t_u32 == Some(other.t_u32)
&& self.t_none.is_none()
}
}
#[test]
fn test_serialization_some() {
let v1 = AllFields::test_val();
with_key!(key, "SerializationSome" => {
key.encode(&v1).unwrap();
let v2: SomeFields = key.decode().unwrap();
assert_eq!(v2, v1);
});
}
#[test]
fn test_serialization_all() {
let v1 = AllFields::test_val();
with_key!(key, "SerializationAll" => {
key.encode(&v1).unwrap();
let v2: AllFields = key.decode().unwrap();
assert_eq!(v2, v1);
});
}
#[test]
fn test_serialization_some_transacted() {
let v1 = AllFields::test_val();
with_key!(key, "SerializationSomeTransacted" => {
let transaction = winreg::transaction::Transaction::new().unwrap();
key.encode_transacted(&v1, &transaction).unwrap();
transaction.commit().unwrap();
let v2: SomeFields = key.decode().unwrap();
assert_eq!(v2, v1);
});
}
#[test]
fn test_serialization_all_transacted() {
let v1 = AllFields::test_val();
with_key!(key, "SerializationAllTransacted" => {
let transaction = winreg::transaction::Transaction::new().unwrap();
key.encode_transacted(&v1, &transaction).unwrap();
transaction.commit().unwrap();
let v2: AllFields = key.decode().unwrap();
assert_eq!(v2, v1);
});
}