Debian Codegraph @

third_party/rust/fs-err/src/tokio/open_options.rs

raw
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 /// ```
31 pub fn new() -> OpenOptions {
32 OpenOptions {
33 tokio: TokioOpenOptions::new(),
34 }
35 }
36
37 /// Wrapper for [`tokio::fs::OpenOptions::read`].
38 pub fn read(&mut self, read: bool) -> &mut OpenOptions {
39 self.tokio.read(read);
40 self
41 }
42
43 /// Wrapper for [`tokio::fs::OpenOptions::write`].
44 pub fn write(&mut self, write: bool) -> &mut OpenOptions {
45 self.tokio.write(write);
46 self
47 }
48
49 /// Wrapper for [`tokio::fs::OpenOptions::append`].
50 pub fn append(&mut self, append: bool) -> &mut OpenOptions {
51 self.tokio.append(append);
52 self
53 }
54
55 /// Wrapper for [`tokio::fs::OpenOptions::truncate`].
56 pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
57 self.tokio.truncate(truncate);
58 self
59 }
60
61 /// Wrapper for [`tokio::fs::OpenOptions::create`].
62 pub fn create(&mut self, create: bool) -> &mut OpenOptions {
63 self.tokio.create(create);
64 self
65 }
66
67 /// Wrapper for [`tokio::fs::OpenOptions::create_new`].
68 pub fn create_new(&mut self, create_new: bool) -> &mut OpenOptions {
69 self.tokio.create_new(create_new);
70 self
71 }
72
73 /// Wrapper for [`tokio::fs::OpenOptions::open`].
74 pub async fn open(&self, path: impl AsRef<Path>) -> io::Result<File> {
75 let path = path.as_ref();
76 self.tokio
77 .open(path)
78 .await
79 .map(|f| File::from_parts(f, path))
80 .map_err(|err| Error::build(err, ErrorKind::OpenFile, path))
81 }
82 }
83
84 #[cfg(unix)]
85 impl OpenOptions {
86 /// Wrapper for [`tokio::fs::OpenOptions::mode`].
87 pub fn mode(&mut self, mode: u32) -> &mut OpenOptions {
88 self.tokio.mode(mode);
89 self
90 }
91
92 /// Wrapper for [`tokio::fs::OpenOptions::custom_flags`].
93 pub fn custom_flags(&mut self, flags: i32) -> &mut OpenOptions {
94 self.tokio.custom_flags(flags);
95 self
96 }
97 }
98
99 impl From<std::fs::OpenOptions> for OpenOptions {
100 fn from(std: std::fs::OpenOptions) -> Self {
101 OpenOptions { tokio: std.into() }
102 }
103 }
104
105 impl From<TokioOpenOptions> for OpenOptions {
106 fn from(tokio: TokioOpenOptions) -> Self {
107 OpenOptions { tokio }
108 }
109 }