fs_err/open_options.rs
1use std::{fs, io, path::PathBuf};
2
3use crate::errors::{Error, ErrorKind};
4
5#[derive(Clone, Debug)]
6/// Wrapper around [`std::fs::OpenOptions`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html)
7pub struct OpenOptions(fs::OpenOptions);
8
9impl OpenOptions {
10 /// Creates a blank new set of options ready for configuration.
11 ///
12 /// Wrapper for [`std::fs::OpenOptions::new`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.new)
13 #[allow(clippy::new_without_default)]
14 pub fn new() -> Self {
15 OpenOptions(fs::OpenOptions::new())
16 }
17
18 /// Sets the option for read access.
19 ///
20 /// Wrapper for [`std::fs::OpenOptions::read`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.read)
21 pub fn read(&mut self, read: bool) -> &mut Self {
22 self.0.read(read);
23 self
24 }
25
26 /// Sets the option for write access.
27 ///
28 /// Wrapper for [`std::fs::OpenOptions::write`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.write)
29 pub fn write(&mut self, write: bool) -> &mut Self {
30 self.0.write(write);
31 self
32 }
33
34 /// Sets the option for the append mode.
35 ///
36 /// Wrapper for [`std::fs::OpenOptions::append`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.append)
37 pub fn append(&mut self, append: bool) -> &mut Self {
38 self.0.append(append);
39 self
40 }
41
42 /// Sets the option for truncating a previous file.
43 ///
44 /// Wrapper for [`std::fs::OpenOptions::truncate`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.truncate)
45 pub fn truncate(&mut self, truncate: bool) -> &mut Self {
46 self.0.truncate(truncate);
47 self
48 }
49
50 /// Sets the option to create a new file, or open it if it already exists.
51 ///
52 /// Wrapper for [`std::fs::OpenOptions::create`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.create)
53 pub fn create(&mut self, create: bool) -> &mut Self {
54 self.0.create(create);
55 self
56 }
57
58 /// Sets the option to create a new file, failing if it already exists.
59 ///
60 /// Wrapper for [`std::fs::OpenOptions::create_new`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.create_new)
61 pub fn create_new(&mut self, create_new: bool) -> &mut Self {
62 self.0.create_new(create_new);
63 self
64 }
65
66 /// Opens a file at `path` with the options specified by `self`.
67 ///
68 /// Wrapper for [`std::fs::OpenOptions::open`](https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.open)
69 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
81/// Methods added by fs-err that are not available on
82/// [`std::fs::OpenOptions`](https://doc.rust-lang.org/stable/std/fs/struct.OpenOptions.html).
83impl OpenOptions {
84 /// Constructs `Self` from [`std::fs::OpenOptions`](https://doc.rust-lang.org/stable/std/fs/struct.OpenOptions.html)
85 pub fn from_options(options: fs::OpenOptions) -> Self {
86 Self(options)
87 }
88
89 /// Returns a reference to the underlying [`std::fs::OpenOptions`](https://doc.rust-lang.org/stable/std/fs/struct.OpenOptions.html).
90 ///
91 /// Note that calling `open()` on this reference will NOT give you the improved errors from fs-err.
92 pub fn options(&self) -> &fs::OpenOptions {
93 &self.0
94 }
95
96 /// Returns a mutable reference to the underlying [`std::fs::OpenOptions`](https://doc.rust-lang.org/stable/std/fs/struct.OpenOptions.html).
97 ///
98 /// This allows you to change settings that don't yet have wrappers in fs-err.
99 /// Note that calling `open()` on this reference will NOT give you the improved errors from fs-err.
100 pub fn options_mut(&mut self) -> &mut fs::OpenOptions {
101 &mut self.0
102 }
103}
104
105#[cfg(unix)]
106mod unix {
107 use crate::os::unix::fs::OpenOptionsExt;
108 use std::os::unix::fs::OpenOptionsExt as _;
109 impl OpenOptionsExt for crate::OpenOptions {
110 fn mode(&mut self, mode: u32) -> &mut Self {
111 self.options_mut().mode(mode);
112 self
113 }
114
115 fn custom_flags(&mut self, flags: i32) -> &mut Self {
116 self.options_mut().custom_flags(flags);
117 self
118 }
119 }
120}
121
122#[cfg(windows)]
123mod windows {
124 use crate::os::windows::fs::OpenOptionsExt;
125 use std::os::windows::fs::OpenOptionsExt as _;
126
127 impl OpenOptionsExt for crate::OpenOptions {
128 fn access_mode(&mut self, access: u32) -> &mut Self {
129 self.options_mut().access_mode(access);
130 self
131 }
132
133 fn share_mode(&mut self, val: u32) -> &mut Self {
134 self.options_mut().share_mode(val);
135 self
136 }
137 fn custom_flags(&mut self, flags: u32) -> &mut Self {
138 self.options_mut().custom_flags(flags);
139 self
140 }
141
142 fn attributes(&mut self, val: u32) -> &mut Self {
143 self.options_mut().attributes(val);
144 self
145 }
146
147 fn security_qos_flags(&mut self, flags: u32) -> &mut Self {
148 self.options_mut().security_qos_flags(flags);
149 self
150 }
151 }
152}