1 use crate::errors::{Error, ErrorKind};
2 use crate::tokio::File;
3 use std::io;
4 use std::path::Path;
5 use tokio::fs::OpenOptions as TokioOpenOptions;
6 
7 /// Options and flags which can be used to configure how a file is opened.
8 ///
9 /// This is a wrapper around [`tokio::fs::OpenOptions`].
10 #[derive(Clone, Debug, Default)]
11 #[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
12 pub struct OpenOptions {
13     tokio: TokioOpenOptions,
14 }
15 
16 impl OpenOptions {
17     /// Creates a blank new set of options ready for configuration.
18     ///
19     /// All options are initially set to `false`.
20     ///
21     /// This is a wrapped version of [`tokio::fs::OpenOptions::new`]
22     ///
23     /// # Examples
24     ///
25     /// ```no_run
26     /// use fs_err::tokio::OpenOptions;
27     ///
28     /// let mut options = OpenOptions::new();
29     /// let future = options.read(true).open("foo.txt");
30     /// ```
new() -> OpenOptions31     pub fn new() -> OpenOptions {
32         OpenOptions {
33             tokio: TokioOpenOptions::new(),
34         }
35     }
36 
37     /// Sets the option for read access.
38     ///
39     /// Wrapper for [`tokio::fs::OpenOptions::read`].
read(&mut self, read: bool) -> &mut OpenOptions40     pub fn read(&mut self, read: bool) -> &mut OpenOptions {
41         self.tokio.read(read);
42         self
43     }
44 
45     /// Sets the option for write access.
46     ///
47     /// Wrapper for [`tokio::fs::OpenOptions::write`].
write(&mut self, write: bool) -> &mut OpenOptions48     pub fn write(&mut self, write: bool) -> &mut OpenOptions {
49         self.tokio.write(write);
50         self
51     }
52 
53     /// Sets the option for the append mode.
54     ///
55     /// Wrapper for [`tokio::fs::OpenOptions::append`].
append(&mut self, append: bool) -> &mut OpenOptions56     pub fn append(&mut self, append: bool) -> &mut OpenOptions {
57         self.tokio.append(append);
58         self
59     }
60 
61     /// Sets the option for truncating a previous file.
62     ///
63     /// Wrapper for [`tokio::fs::OpenOptions::truncate`].
truncate(&mut self, truncate: bool) -> &mut OpenOptions64     pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
65         self.tokio.truncate(truncate);
66         self
67     }
68 
69     /// Sets the option for creating a new file.
70     ///
71     /// Wrapper for [`tokio::fs::OpenOptions::create`].
create(&mut self, create: bool) -> &mut OpenOptions72     pub fn create(&mut self, create: bool) -> &mut OpenOptions {
73         self.tokio.create(create);
74         self
75     }
76 
77     /// Sets the option to always create a new file.
78     ///
79     /// Wrapper for [`tokio::fs::OpenOptions::create_new`].
create_new(&mut self, create_new: bool) -> &mut OpenOptions80     pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
81         self.tokio.create_new(create_new);
82         self
83     }
84 
85     /// Opens a file at `path` with the options specified by `self`.
86     ///
87     /// Wrapper for [`tokio::fs::OpenOptions::open`].
open(&self, path: impl AsRef<Path>) -> io::Result<File>88     pub async fn open(&self, path: impl AsRef<Path>) -> io::Result<File> {
89         let path = path.as_ref();
90         self.tokio
91             .open(path)
92             .await
93             .map(|f| File::from_parts(f, path))
94             .map_err(|err| Error::build(err, ErrorKind::OpenFile, path))
95     }
96 }
97 
98 #[cfg(unix)]
99 impl OpenOptions {
100     /// Sets the mode bits that a new file will be created with.
101     ///
102     /// Wrapper for [`tokio::fs::OpenOptions::mode`].
mode(&mut self, mode: u32) -> &mut OpenOptions103     pub fn mode(&mut self, mode: u32) -> &mut OpenOptions {
104         self.tokio.mode(mode);
105         self
106     }
107 
108     /// Passes custom flags to the `flags` argument of `open`.
109     ///
110     /// Wrapper for [`tokio::fs::OpenOptions::custom_flags`].
custom_flags(&mut self, flags: i32) -> &mut OpenOptions111     pub fn custom_flags(&mut self, flags: i32) -> &mut OpenOptions {
112         self.tokio.custom_flags(flags);
113         self
114     }
115 }
116 
117 impl From<std::fs::OpenOptions> for OpenOptions {
from(std: std::fs::OpenOptions) -> Self118     fn from(std: std::fs::OpenOptions) -> Self {
119         OpenOptions { tokio: std.into() }
120     }
121 }
122 
123 impl From<TokioOpenOptions> for OpenOptions {
from(tokio: TokioOpenOptions) -> Self124     fn from(tokio: TokioOpenOptions) -> Self {
125         OpenOptions { tokio }
126     }
127 }
128