1use std::{fs, io, path::PathBuf};
2#[derive(Clone, Debug)]
3/// Wrapper around [`std::fs::OptionOptions`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html)
4pub struct OpenOptions(fs::OpenOptions);
5
6impl OpenOptions {
7 /// Wrapper for [`std::fs::OpenOptions::new`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.new)
8 #[allow(clippy::new_without_default)]
9 pub fn new() -> Self {
10 OpenOptions(fs::OpenOptions::new())
11 }
12
13 /// Wrapper for [`std::fs::OpenOptions::read`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.read)
14 pub fn read(&mut self, read: bool) -> &mut Self {
15 self.0.read(read);
16 self
17 }
18
19 /// Wrapper for [`std::fs::OpenOptions::write`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.write)
20 pub fn write(&mut self, write: bool) -> &mut Self {
21 self.0.write(write);
22 self
23 }
24
25 /// Wrapper for [`std::fs::OpenOptions::append`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.append)
26 pub fn append(&mut self, append: bool) -> &mut Self {
27 self.0.append(append);
28 self
29 }
30
31 /// Wrapper for [`std::fs::OpenOptions::truncate`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.truncate)
32 pub fn truncate(&mut self, truncate: bool) -> &mut Self {
33 self.0.truncate(truncate);
34 self
35 }
36
37 /// Wrapper for [`std::fs::OpenOptions::create`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.create)
38 pub fn create(&mut self, create: bool) -> &mut Self {
39 self.0.create(create);
40 self
41 }
42
43 /// Wrapper for [`std::fs::OpenOptions::create_new`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.create_new)
44 pub fn create_new(&mut self, create_new: bool) -> &mut Self {
45 self.0.create_new(create_new);
46 self
47 }
48
49 /// Wrapper for [`std::fs::OpenOptions::open`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.open)
50 pub fn open<P>(&self, path: P) -> io::Result<crate::File>
51 where
52 P: Into<PathBuf>,
53 {
54 // We have to either duplicate the logic or call the deprecated method here.
55 // We can't let the deprecated function call this method, because we can't construct
56 // `&fs_err::OpenOptions` from `&fs::OpenOptions` without cloning
57 // (although cloning would probably be cheap).
58 #[allow(deprecated)]
59 crate::File::from_options(path.into(), self.options())
60 }
61}
62
63/// Methods added by fs-err that are not available on
64/// [`std::fs::OpenOptions`](https://doc.rust-lang.org/stable/std/fs/struct.OpenOptions.html).
65impl OpenOptions {
66 /// Constructs `Self` from [`std::fs::OpenOptions`](https://doc.rust-lang.org/stable/std/fs/struct.OpenOptions.html)
67 pub fn from_options(options: fs::OpenOptions) -> Self {
68 Self(options)
69 }
70
71 /// Returns a reference to the underlying [`std::fs::OpenOptions`](https://doc.rust-lang.org/stable/std/fs/struct.OpenOptions.html).
72 ///
73 /// Note that calling `open()` on this reference will NOT give you the improved errors from fs-err.
74 pub fn options(&self) -> &fs::OpenOptions {
75 &self.0
76 }
77
78 /// Returns a mutable reference to the underlying [`std::fs::OpenOptions`](https://doc.rust-lang.org/stable/std/fs/struct.OpenOptions.html).
79 ///
80 /// This allows you to change settings that don't yet have wrappers in fs-err.
81 /// Note that calling `open()` on this reference will NOT give you the improved errors from fs-err.
82 pub fn options_mut(&mut self) -> &mut fs::OpenOptions {
83 &mut self.0
84 }
85}
86
87#[cfg(unix)]
88mod unix {
89 use crate::os::unix::fs::OpenOptionsExt;
90 use std::os::unix::fs::OpenOptionsExt as _;
91 impl OpenOptionsExt for crate::OpenOptions {
92 fn mode(&mut self, mode: u32) -> &mut Self {
93 self.options_mut().mode(mode);
94 self
95 }
96
97 fn custom_flags(&mut self, flags: i32) -> &mut Self {
98 self.options_mut().custom_flags(flags);
99 self
100 }
101 }
102}
103
104#[cfg(windows)]
105mod windows {
106 use crate::os::windows::fs::OpenOptionsExt;
107 use std::os::windows::fs::OpenOptionsExt as _;
108
109 impl OpenOptionsExt for crate::OpenOptions {
110 fn access_mode(&mut self, access: u32) -> &mut Self {
111 self.options_mut().access_mode(access);
112 self
113 }
114
115 fn share_mode(&mut self, val: u32) -> &mut Self {
116 self.options_mut().share_mode(val);
117 self
118 }
119 fn custom_flags(&mut self, flags: u32) -> &mut Self {
120 self.options_mut().custom_flags(flags);
121 self
122 }
123
124 fn attributes(&mut self, val: u32) -> &mut Self {
125 self.options_mut().attributes(val);
126 self
127 }
128
129 fn security_qos_flags(&mut self, flags: u32) -> &mut Self {
130 self.options_mut().security_qos_flags(flags);
131 self
132 }
133 }
134}
135