fs_err/tokio/
open_options.rs

1use crate::errors::{Error, ErrorKind};
2use crate::tokio::File;
3use std::io;
4use std::path::Path;
5use 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")))]
12pub struct OpenOptions {
13    tokio: TokioOpenOptions,
14}
15
16impl 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    /// ```
31    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`].
40    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`].
48    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`].
56    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`].
64    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`].
72    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`].
80    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`].
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)]
99impl OpenOptions {
100    /// Sets the mode bits that a new file will be created with.
101    ///
102    /// Wrapper for [`tokio::fs::OpenOptions::mode`].
103    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`].
111    pub fn custom_flags(&mut self, flags: i32) -> &mut OpenOptions {
112        self.tokio.custom_flags(flags);
113        self
114    }
115}
116
117impl From<std::fs::OpenOptions> for OpenOptions {
118    fn from(std: std::fs::OpenOptions) -> Self {
119        OpenOptions { tokio: std.into() }
120    }
121}
122
123impl From<TokioOpenOptions> for OpenOptions {
124    fn from(tokio: TokioOpenOptions) -> Self {
125        OpenOptions { tokio }
126    }
127}