Debian Codegraph @

third_party/rust/winreg/src/decoder/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 super::enums::*;
7 use super::RegKey;
8 use std::error::Error;
9 use std::fmt;
10 use std::io;
11 use winapi::shared::minwindef::DWORD;
12
13 macro_rules! read_value {
14 ($s:ident) => {
15 match mem::replace(&mut $s.f_name, None) {
16 Some(ref s) => $s.key.get_value(s).map_err(DecoderError::IoError),
17 None => Err(DecoderError::NoFieldName),
18 }
19 };
20 }
21
22 macro_rules! parse_string {
23 ($s:ident) => {{
24 let s: String = read_value!($s)?;
25 s.parse()
26 .map_err(|e| DecoderError::ParseError(format!("{:?}", e)))
27 }};
28 }
29
30 macro_rules! no_impl {
31 ($e:expr) => {
32 Err(DecoderError::DecodeNotImplemented($e.to_owned()))
33 };
34 }
35
36 #[cfg(feature = "serialization-serde")]
37 mod serialization_serde;
38
39 #[derive(Debug)]
40 pub enum DecoderError {
41 DecodeNotImplemented(String),
42 DeserializerError(String),
43 IoError(io::Error),
44 ParseError(String),
45 NoFieldName,
46 }
47
48 impl fmt::Display for DecoderError {
49 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50 write!(f, "{:?}", self)
51 }
52 }
53
54 impl Error for DecoderError {}
55
56 impl From<io::Error> for DecoderError {
57 fn from(err: io::Error) -> DecoderError {
58 DecoderError::IoError(err)
59 }
60 }
61
62 pub type DecodeResult<T> = Result<T, DecoderError>;
63
64 #[derive(Debug)]
65 enum DecoderReadingState {
66 WaitingForKey,
67 WaitingForValue,
68 }
69
70 #[derive(Debug)]
71 enum DecoderEnumerationState {
72 EnumeratingKeys(DWORD),
73 EnumeratingValues(DWORD),
74 }
75
76 #[derive(Debug)]
77 pub struct Decoder {
78 key: RegKey,
79 f_name: Option<String>,
80 reading_state: DecoderReadingState,
81 enumeration_state: DecoderEnumerationState,
82 }
83
84 const DECODER_SAM: DWORD = KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS;
85
86 impl Decoder {
87 pub fn from_key(key: &RegKey) -> DecodeResult<Decoder> {
88 key.open_subkey_with_flags("", DECODER_SAM)
89 .map(Decoder::new)
90 .map_err(DecoderError::IoError)
91 }
92
93 fn new(key: RegKey) -> Decoder {
94 Decoder {
95 key,
96 f_name: None,
97 reading_state: DecoderReadingState::WaitingForKey,
98 enumeration_state: DecoderEnumerationState::EnumeratingKeys(0),
99 }
100 }
101 }