| 1 |
// Copyright 2015, 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 |
#[derive(Debug, Serialize, Deserialize, PartialEq)] |
| 32 |
struct Test { |
| 33 |
t_bool: bool, |
| 34 |
t_u8: u8, |
| 35 |
t_u16: u16, |
| 36 |
t_u32: u32, |
| 37 |
t_u64: u64, |
| 38 |
t_usize: usize, |
| 39 |
t_struct: Rectangle, |
| 40 |
t_map: HashMap<String, u32>, |
| 41 |
t_string: String, |
| 42 |
#[serde(rename = "")] // empty name becomes the (Default) value in the registry |
| 43 |
t_char: char, |
| 44 |
t_i8: i8, |
| 45 |
t_i16: i16, |
| 46 |
t_i32: i32, |
| 47 |
t_i64: i64, |
| 48 |
t_isize: isize, |
| 49 |
t_f64: f64, |
| 50 |
t_f32: f32, |
| 51 |
} |
| 52 |
|
| 53 |
fn main() -> Result<(), Box<dyn Error>> { |
| 54 |
let hkcu = winreg::RegKey::predef(HKEY_CURRENT_USER); |
| 55 |
let (key, _disp) = hkcu.create_subkey("Software\\RustEncode")?; |
| 56 |
|
| 57 |
let mut map = HashMap::new(); |
| 58 |
map.insert("".to_owned(), 0); // empty name becomes the (Default) value in the registry |
| 59 |
map.insert("v1".to_owned(), 1); |
| 60 |
map.insert("v2".to_owned(), 2); |
| 61 |
map.insert("v3".to_owned(), 3); |
| 62 |
|
| 63 |
let v1 = Test { |
| 64 |
t_bool: false, |
| 65 |
t_u8: 127, |
| 66 |
t_u16: 32768, |
| 67 |
t_u32: 123_456_789, |
| 68 |
t_u64: 123_456_789_101_112, |
| 69 |
t_usize: 1_234_567_891, |
| 70 |
t_struct: Rectangle { |
| 71 |
coords: Coords { x: 55, y: 77 }, |
| 72 |
size: Size { w: 500, h: 300 }, |
| 73 |
}, |
| 74 |
t_map: map, |
| 75 |
t_string: "test 123!".to_owned(), |
| 76 |
t_char: 'a', |
| 77 |
t_i8: -123, |
| 78 |
t_i16: -2049, |
| 79 |
t_i32: 20100, |
| 80 |
t_i64: -12_345_678_910, |
| 81 |
t_isize: -1_234_567_890, |
| 82 |
t_f64: -0.01, |
| 83 |
t_f32: 3.15, |
| 84 |
}; |
| 85 |
|
| 86 |
key.encode(&v1)?; |
| 87 |
|
| 88 |
let v2: Test = key.decode()?; |
| 89 |
println!("Decoded {:?}", v2); |
| 90 |
|
| 91 |
println!("Equal to encoded: {:?}", v1 == v2); |
| 92 |
Ok(()) |
| 93 |
} |