fs_err/tokio/
dir_builder.rs

1use crate::errors::{Error, ErrorKind};
2use std::io;
3use 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")))]
10pub struct DirBuilder {
11    inner: tokio::fs::DirBuilder,
12}
13
14impl 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    /// Indicates whether to create directories recursively (including all parent
32    /// directories). Parents that do not exist are created with the same security and
33    /// permissions settings.
34    ///
35    /// Wrapper around [`tokio::fs::DirBuilder::recursive`].
36    pub fn recursive(&mut self, recursive: bool) -> &mut Self {
37        self.inner.recursive(recursive);
38        self
39    }
40
41    /// Creates the specified directory with the configured options.
42    ///
43    /// Wrapper around [`tokio::fs::DirBuilder::create`].
44    pub async fn create(&self, path: impl AsRef<Path>) -> io::Result<()> {
45        let path = path.as_ref();
46        self.inner
47            .create(path)
48            .await
49            .map_err(|err| Error::build(err, ErrorKind::CreateDir, path))
50    }
51}
52
53#[cfg(unix)]
54impl DirBuilder {
55    /// Sets the mode to create new directories with.
56    ///
57    /// Wrapper around [`tokio::fs::DirBuilder::mode`].
58    pub fn mode(&mut self, mode: u32) -> &mut Self {
59        self.inner.mode(mode);
60        self
61    }
62}