Debian Codegraph @

third_party/rust/winreg/examples/map_key_serialization.rs

raw
1 // Copyright 2020, Igor Shaula
2 // Licensed under the MIT License <LICENSE or
3 // http://opensource.org/licenses/MIT>. This file
4 // may not be copied, modified, or distributed
5 // except according to those terms.
6 #[macro_use]
7 extern crate serde_derive;
8 extern crate winreg;
9 use std::collections::HashMap;
10 use std::error::Error;
11 use winreg::enums::*;
12
13 #[derive(Debug, Serialize, Deserialize, PartialEq)]
14 struct Coords {
15 x: u32,
16 y: u32,
17 }
18
19 #[derive(Debug, Serialize, Deserialize, PartialEq)]
20 struct Size {
21 w: u32,
22 h: u32,
23 }
24
25 #[derive(Debug, Serialize, Deserialize, PartialEq)]
26 struct Rectangle {
27 coords: Coords,
28 size: Size,
29 }
30
31 fn main() -> Result<(), Box<dyn Error>> {
32 let hkcu = winreg::RegKey::predef(HKEY_CURRENT_USER);
33 let (key, _disp) = hkcu.create_subkey("Software\\RustEncodeMapKey")?;
34 let mut v1 = HashMap::new();
35 v1.insert(
36 "first".to_owned(),
37 Rectangle {
38 coords: Coords { x: 55, y: 77 },
39 size: Size { w: 500, h: 300 },
40 },
41 );
42 v1.insert(
43 "second".to_owned(),
44 Rectangle {
45 coords: Coords { x: 11, y: 22 },
46 size: Size { w: 1000, h: 600 },
47 },
48 );
49
50 key.encode(&v1)?;
51
52 let v2: HashMap<String, Rectangle> = key.decode()?;
53 println!("Decoded {:?}", v2);
54
55 println!("Equal to encoded: {:?}", v1 == v2);
56 Ok(())
57 }