Debian Codegraph @

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

raw
1 use crate::errors::{Error, ErrorKind};
2 use std::io;
3 use std::path::Path;
4
5 /// A builder for creating directories in various manners.
6 ///
7 /// This is a wrapper around [`tokio::fs::DirBuilder`].
8 #[derive(Debug, Default)]
9 #[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
10 pub struct DirBuilder {
11 inner: tokio::fs::DirBuilder,
12 }
13
14 impl DirBuilder {
15 /// Creates a new set of options with default mode/security settings for all
16 /// platforms and also non-recursive.
17 ///
18 /// This is a wrapper version of [`tokio::fs::DirBuilder::new`]
19 ///
20 /// # Examples
21 ///
22 /// ```no_run
23 /// use fs_err::tokio::DirBuilder;
24 ///
25 /// let builder = DirBuilder::new();
26 /// ```
27 pub fn new() -> Self {
28 Default::default()
29 }
30
31 /// Wrapper around [`tokio::fs::DirBuilder::recursive`].
32 pub fn recursive(&mut self, recursive: bool) -> &mut Self {
33 self.inner.recursive(recursive);
34 self
35 }
36
37 /// Wrapper around [`tokio::fs::DirBuilder::create`].
38 pub async fn create(&self, path: impl AsRef<Path>) -> io::Result<()> {
39 let path = path.as_ref();
40 self.inner
41 .create(path)
42 .await
43 .map_err(|err| Error::build(err, ErrorKind::CreateDir, path))
44 }
45 }
46
47 #[cfg(unix)]
48 impl DirBuilder {
49 /// Wrapper around [`tokio::fs::DirBuilder::mode`].
50 pub fn mode(&mut self, mode: u32) -> &mut Self {
51 self.inner.mode(mode);
52 self
53 }
54 }