Debian Codegraph @

third_party/rust/winreg/src/encoder/mod.rs

raw
1 // Copyright 2017, 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 use self::EncoderState::*;
7 use super::enums::*;
8 use super::transaction::Transaction;
9 use super::RegKey;
10 use std::error::Error;
11 use std::fmt;
12 use std::io;
13 use winapi::shared::minwindef::DWORD;
14
15 macro_rules! emit_value {
16 ($s:ident, $v:ident) => {
17 match mem::replace(&mut $s.state, Start) {
18 NextKey(ref s) => $s.keys[$s.keys.len() - 1]
19 .set_value(s, &$v)
20 .map_err(EncoderError::IoError),
21 Start => Err(EncoderError::NoFieldName),
22 }
23 };
24 }
25
26 macro_rules! no_impl {
27 ($e:expr) => {
28 Err(EncoderError::EncodeNotImplemented($e.to_owned()))
29 };
30 }
31
32 #[cfg(feature = "serialization-serde")]
33 mod serialization_serde;
34
35 #[derive(Debug)]
36 pub enum EncoderError {
37 EncodeNotImplemented(String),
38 SerializerError(String),
39 IoError(io::Error),
40 NoFieldName,
41 KeyMustBeAString,
42 }
43
44 impl fmt::Display for EncoderError {
45 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46 write!(f, "{:?}", self)
47 }
48 }
49
50 impl Error for EncoderError {}
51
52 pub type EncodeResult<T> = Result<T, EncoderError>;
53
54 impl From<io::Error> for EncoderError {
55 fn from(err: io::Error) -> EncoderError {
56 EncoderError::IoError(err)
57 }
58 }
59
60 #[derive(Debug)]
61 enum EncoderState {
62 Start,
63 NextKey(String),
64 // NextMapKey,
65 }
66
67 #[derive(Debug)]
68 pub struct Encoder {
69 keys: Vec<RegKey>,
70 tr: Transaction,
71 state: EncoderState,
72 }
73
74 const ENCODER_SAM: DWORD = KEY_CREATE_SUB_KEY | KEY_SET_VALUE;
75
76 impl Encoder {
77 pub fn from_key(key: &RegKey) -> EncodeResult<Encoder> {
78 let tr = Transaction::new()?;
79 key.open_subkey_transacted_with_flags("", &tr, ENCODER_SAM)
80 .map(|k| Encoder::new(k, tr))
81 .map_err(EncoderError::IoError)
82 }
83
84 fn new(key: RegKey, tr: Transaction) -> Encoder {
85 let mut keys = Vec::with_capacity(5);
86 keys.push(key);
87 Encoder {
88 keys,
89 tr,
90 state: Start,
91 }
92 }
93
94 pub fn commit(&mut self) -> EncodeResult<()> {
95 self.tr.commit().map_err(EncoderError::IoError)
96 }
97 }