Skip to main content

fs_err/
open_options.rs

1use std::{fs, io, path::PathBuf};
2
3use crate::errors::{Error, ErrorKind};
4
5#[derive(Clone, Debug)]
6/// Wrapper for [`std::fs::OpenOptions`].
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`].
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`].
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`].
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`].
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`].
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`].
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`].
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`].
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 [`std::fs::OpenOptions`].
82impl OpenOptions {
83    /// Constructs `Self` from [`std::fs::OpenOptions`].
84    pub fn from_options(options: fs::OpenOptions) -> Self {
85        Self(options)
86    }
87
88    /// Returns a reference to the underlying [`std::fs::OpenOptions`].
89    ///
90    /// Note that calling `open()` on this reference will NOT give you the improved errors from fs-err.
91    pub fn options(&self) -> &fs::OpenOptions {
92        &self.0
93    }
94
95    /// Returns a mutable reference to the underlying [`std::fs::OpenOptions`].
96    ///
97    /// This allows you to change settings that don't yet have wrappers in fs-err.
98    /// Note that calling `open()` on this reference will NOT give you the improved errors from fs-err.
99    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}