cap_primitives/fs/
dir_options.rs

1#[cfg(not(target_os = "wasi"))]
2use crate::fs::DirOptionsExt;
3
4/// Options and flags which can be used to configure how a directory is
5/// created.
6///
7/// This is to `create_dir` what to `OpenOptions` is to `open`.
8#[derive(Debug, Clone)]
9pub struct DirOptions {
10    #[cfg(not(target_os = "wasi"))]
11    #[allow(dead_code)]
12    pub(crate) ext: DirOptionsExt,
13}
14
15impl DirOptions {
16    /// Creates a blank new set of options ready for configuration.
17    #[allow(clippy::new_without_default)]
18    #[inline]
19    pub const fn new() -> Self {
20        Self {
21            #[cfg(not(target_os = "wasi"))]
22            ext: DirOptionsExt::new(),
23        }
24    }
25}
26
27#[cfg(unix)]
28impl crate::fs::DirBuilderExt for DirOptions {
29    #[inline]
30    fn mode(&mut self, mode: u32) -> &mut Self {
31        self.ext.mode(mode);
32        self
33    }
34}
35
36#[cfg(target_os = "vxworks")]
37impl crate::fs::DirBuilderExt for DirOptions {
38    #[inline]
39    fn mode(&mut self, mode: u32) -> &mut Self {
40        self.ext.mode(mode);
41        self
42    }
43}
44
45#[cfg(feature = "arbitrary")]
46impl arbitrary::Arbitrary<'_> for DirOptions {
47    fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
48        #[cfg(any(unix, target_os = "vxworks"))]
49        use crate::fs::DirBuilderExt;
50
51        #[allow(unused_mut)]
52        let mut dir_options = Self::new();
53
54        #[cfg(any(unix, target_os = "vxworks"))]
55        dir_options.mode(u.int_in_range(0..=0o777)?);
56
57        // Unix is currently the only platform with a `DirBuilderExt`.
58        #[cfg(not(any(unix, target_os = "vxworks")))]
59        let _ = u;
60
61        Ok(dir_options)
62    }
63}