1use std::{fs, io, path::PathBuf};
2
3use crate::errors::{Error, ErrorKind};
4
5#[derive(Clone, Debug)]
6pub struct OpenOptions(fs::OpenOptions);
8
9impl OpenOptions {
10 #[allow(clippy::new_without_default)]
14 pub fn new() -> Self {
15 OpenOptions(fs::OpenOptions::new())
16 }
17
18 pub fn read(&mut self, read: bool) -> &mut Self {
22 self.0.read(read);
23 self
24 }
25
26 pub fn write(&mut self, write: bool) -> &mut Self {
30 self.0.write(write);
31 self
32 }
33
34 pub fn append(&mut self, append: bool) -> &mut Self {
38 self.0.append(append);
39 self
40 }
41
42 pub fn truncate(&mut self, truncate: bool) -> &mut Self {
46 self.0.truncate(truncate);
47 self
48 }
49
50 pub fn create(&mut self, create: bool) -> &mut Self {
54 self.0.create(create);
55 self
56 }
57
58 pub fn create_new(&mut self, create_new: bool) -> &mut Self {
62 self.0.create_new(create_new);
63 self
64 }
65
66 pub fn open<P>(&self, path: P) -> io::Result<crate::File>
70 where
71 P: Into<PathBuf>,
72 {
73 let path = path.into();
74 match self.0.open(&path) {
75 Ok(file) => Ok(crate::File::from_parts(file, path)),
76 Err(source) => Err(Error::build(source, ErrorKind::OpenFile, path)),
77 }
78 }
79}
80
81impl OpenOptions {
83 pub fn from_options(options: fs::OpenOptions) -> Self {
85 Self(options)
86 }
87
88 pub fn options(&self) -> &fs::OpenOptions {
92 &self.0
93 }
94
95 pub fn options_mut(&mut self) -> &mut fs::OpenOptions {
100 &mut self.0
101 }
102}
103
104#[cfg(unix)]
105mod unix {
106 use crate::os::unix::fs::OpenOptionsExt;
107 use std::os::unix::fs::OpenOptionsExt as _;
108 impl OpenOptionsExt for crate::OpenOptions {
109 fn mode(&mut self, mode: u32) -> &mut Self {
110 self.options_mut().mode(mode);
111 self
112 }
113
114 fn custom_flags(&mut self, flags: i32) -> &mut Self {
115 self.options_mut().custom_flags(flags);
116 self
117 }
118 }
119}
120
121#[cfg(windows)]
122mod windows {
123 use crate::os::windows::fs::OpenOptionsExt;
124 use std::os::windows::fs::OpenOptionsExt as _;
125
126 impl OpenOptionsExt for crate::OpenOptions {
127 fn access_mode(&mut self, access: u32) -> &mut Self {
128 self.options_mut().access_mode(access);
129 self
130 }
131
132 fn share_mode(&mut self, val: u32) -> &mut Self {
133 self.options_mut().share_mode(val);
134 self
135 }
136 fn custom_flags(&mut self, flags: u32) -> &mut Self {
137 self.options_mut().custom_flags(flags);
138 self
139 }
140
141 fn attributes(&mut self, val: u32) -> &mut Self {
142 self.options_mut().attributes(val);
143 self
144 }
145
146 fn security_qos_flags(&mut self, flags: u32) -> &mut Self {
147 self.options_mut().security_qos_flags(flags);
148 self
149 }
150 }
151}