windows_registry/
open_options.rs1use super::*;
2
3#[derive(Debug)]
5pub struct OpenOptions<'a> {
6 parent: &'a Key,
7 access: u32,
8 create: bool,
9 transaction: Option<&'a Transaction>,
10 options: u32,
11}
12
13impl<'a> OpenOptions<'a> {
14 pub(crate) fn new(parent: &'a Key) -> Self {
15 Self {
16 parent,
17 access: 0,
18 create: false,
19 transaction: None,
20 options: REG_OPTION_NON_VOLATILE,
21 }
22 }
23
24 pub fn read(&mut self) -> &mut Self {
26 self.access |= KEY_READ;
27 self
28 }
29
30 pub fn write(&mut self) -> &mut Self {
32 self.access |= KEY_WRITE;
33 self
34 }
35
36 pub fn access(&mut self, access: u32) -> &mut Self {
38 self.access |= access;
39 self
40 }
41
42 pub fn create(&mut self) -> &mut Self {
44 self.create = true;
45 self
46 }
47
48 pub fn transaction(&mut self, transaction: &'a Transaction) -> &mut Self {
50 self.transaction = Some(transaction);
51 self
52 }
53
54 pub fn volatile(&mut self) -> &mut Self {
56 self.options |= REG_OPTION_VOLATILE;
57 self
58 }
59
60 pub fn open<T: AsRef<str>>(&self, path: T) -> Result<Key> {
62 let mut handle = null_mut();
63
64 let result = unsafe {
65 if let Some(transaction) = self.transaction {
66 if self.create {
67 RegCreateKeyTransactedW(
68 self.parent.0,
69 pcwstr(path).as_ptr(),
70 0,
71 null(),
72 self.options,
73 self.access,
74 null(),
75 &mut handle,
76 null_mut(),
77 transaction.0,
78 null(),
79 )
80 } else {
81 RegOpenKeyTransactedW(
82 self.parent.0,
83 pcwstr(path).as_ptr(),
84 0,
85 self.access,
86 &mut handle,
87 transaction.0,
88 null(),
89 )
90 }
91 } else if self.create {
92 RegCreateKeyExW(
93 self.parent.0,
94 pcwstr(path).as_ptr(),
95 0,
96 null(),
97 self.options,
98 self.access,
99 null(),
100 &mut handle,
101 null_mut(),
102 )
103 } else {
104 RegOpenKeyExW(
105 self.parent.0,
106 pcwstr(path).as_ptr(),
107 0,
108 self.access,
109 &mut handle,
110 )
111 }
112 };
113
114 win32_error(result).map(|_| Key(handle))
115 }
116}