Skip to main content

qubit_fs/directory/
create_directory_options.rs

1// =============================================================================
2//    Copyright (c) 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8// facade tests.
9//! Directory creation options.
10
11use crate::metadata::NonSensitiveMetadata;
12use crate::metadata::UserMetadata;
13
14/// Options controlling directory or collection creation.
15///
16/// # Examples
17///
18/// ```rust
19/// use qubit_fs::directory::CreateDirectoryOptions;
20///
21/// assert!(!CreateDirectoryOptions::default().recursive());
22/// ```
23#[non_exhaustive]
24#[derive(Clone, Debug, PartialEq)]
25pub struct CreateDirectoryOptions {
26    /// Whether missing parent directories should be created.
27    recursive: bool,
28    /// Whether an existing directory should be accepted.
29    exists_ok: bool,
30    /// User-defined metadata with validated non-sensitive structural keys.
31    user_metadata: NonSensitiveMetadata,
32}
33
34impl Default for CreateDirectoryOptions {
35    /// Creates non-recursive options that reject an existing directory.
36    #[inline]
37    fn default() -> Self {
38        Self {
39            recursive: false,
40            exists_ok: false,
41            user_metadata: NonSensitiveMetadata::new(),
42        }
43    }
44}
45
46impl CreateDirectoryOptions {
47    /// Returns whether missing parent directories should be created.
48    #[inline]
49    #[must_use]
50    pub const fn recursive(&self) -> bool {
51        self.recursive
52    }
53
54    /// Returns whether an existing directory should be accepted.
55    #[inline]
56    #[must_use]
57    pub const fn exists_ok(&self) -> bool {
58        self.exists_ok
59    }
60
61    /// Returns validated user-defined metadata.
62    #[inline]
63    #[must_use]
64    pub const fn user_metadata(&self) -> &NonSensitiveMetadata {
65        &self.user_metadata
66    }
67
68    /// Replaces recursive parent creation.
69    #[inline]
70    #[must_use]
71    pub const fn with_recursive(mut self, recursive: bool) -> Self {
72        self.recursive = recursive;
73        self
74    }
75
76    /// Replaces acceptance of an existing directory.
77    #[inline]
78    #[must_use]
79    pub const fn with_exists_ok(mut self, exists_ok: bool) -> Self {
80        self.exists_ok = exists_ok;
81        self
82    }
83
84    /// Replaces user-defined metadata that has already passed key validation.
85    #[inline]
86    #[must_use]
87    pub fn with_user_metadata(mut self, metadata: UserMetadata) -> Self {
88        self.user_metadata = NonSensitiveMetadata::from(metadata);
89        self
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use std::hint::black_box;
96
97    use super::CreateDirectoryOptions;
98    use crate::metadata::UserMetadata;
99
100    #[test]
101    fn option_accessors_are_executed_at_runtime() {
102        let constructor: fn() -> CreateDirectoryOptions = black_box(Default::default);
103        let recursive: fn(&CreateDirectoryOptions) -> bool = black_box(CreateDirectoryOptions::recursive);
104        let exists_ok: fn(&CreateDirectoryOptions) -> bool = black_box(CreateDirectoryOptions::exists_ok);
105        let user_metadata: fn(&CreateDirectoryOptions) -> &_ = black_box(CreateDirectoryOptions::user_metadata);
106        let with_recursive: fn(CreateDirectoryOptions, bool) -> CreateDirectoryOptions =
107            black_box(CreateDirectoryOptions::with_recursive);
108        let with_exists_ok: fn(CreateDirectoryOptions, bool) -> CreateDirectoryOptions =
109            black_box(CreateDirectoryOptions::with_exists_ok);
110        let with_user_metadata: fn(CreateDirectoryOptions, UserMetadata) -> CreateDirectoryOptions =
111            black_box(CreateDirectoryOptions::with_user_metadata);
112
113        let options = with_user_metadata(
114            with_exists_ok(with_recursive(constructor(), true), true),
115            UserMetadata::new(),
116        );
117        assert!(recursive(&options));
118        assert!(exists_ok(&options));
119        assert!(user_metadata(&options).is_empty());
120    }
121}