Skip to main content

qubit_fs/temp/
temp_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//! Temporary resource creation options.
9
10use crate::path::Path;
11
12/// Options shared by temporary file and temporary directory creation.
13///
14/// # Examples
15///
16/// ```rust
17/// use qubit_fs::temp::TempOptions;
18///
19/// assert_eq!(TempOptions::default(), TempOptions::new());
20/// ```
21#[non_exhaustive]
22#[derive(Clone, Debug, Eq, PartialEq)]
23pub struct TempOptions {
24    /// Parent directory or prefix for the temporary resource.
25    parent: Option<Path>,
26    /// Generated resource name prefix.
27    prefix: String,
28    /// Generated resource name suffix.
29    suffix: String,
30    /// Whether a missing parent directory is created.
31    create_parent: bool,
32}
33
34impl TempOptions {
35    /// Creates empty temporary-resource options without parent creation.
36    #[inline]
37    #[must_use]
38    pub fn new() -> Self {
39        Self {
40            parent: None,
41            prefix: String::new(),
42            suffix: String::new(),
43            create_parent: false,
44        }
45    }
46
47    /// Returns the optional parent directory or prefix.
48    #[inline]
49    #[must_use]
50    pub const fn parent(&self) -> Option<&Path> {
51        self.parent.as_ref()
52    }
53
54    /// Returns the generated resource name prefix.
55    #[inline]
56    #[must_use]
57    pub fn prefix(&self) -> &str {
58        &self.prefix
59    }
60
61    /// Returns the generated resource name suffix.
62    #[inline]
63    #[must_use]
64    pub fn suffix(&self) -> &str {
65        &self.suffix
66    }
67
68    /// Returns whether missing parent directories are created.
69    #[inline]
70    #[must_use]
71    pub const fn creates_parent(&self) -> bool {
72        self.create_parent
73    }
74
75    /// Replaces the optional parent directory or prefix.
76    #[inline]
77    #[must_use]
78    pub fn with_parent(mut self, parent: Option<Path>) -> Self {
79        self.parent = parent;
80        self
81    }
82
83    /// Replaces the generated resource name prefix.
84    #[inline]
85    #[must_use]
86    pub fn with_prefix(mut self, prefix: impl Into<String>) -> Self {
87        self.prefix = prefix.into();
88        self
89    }
90
91    /// Replaces the generated resource name suffix.
92    #[inline]
93    #[must_use]
94    pub fn with_suffix(mut self, suffix: impl Into<String>) -> Self {
95        self.suffix = suffix.into();
96        self
97    }
98
99    /// Replaces whether missing parent directories are created.
100    #[inline]
101    #[must_use]
102    pub const fn with_create_parent(mut self, create: bool) -> Self {
103        self.create_parent = create;
104        self
105    }
106}
107
108impl Default for TempOptions {
109    /// Creates empty temporary-resource options without parent creation.
110    #[inline]
111    fn default() -> Self {
112        Self::new()
113    }
114}
115
116#[cfg(test)]
117mod tests {
118    use super::TempOptions;
119    use crate::path::Path;
120
121    #[test]
122    fn option_accessors_are_executed_at_runtime() {
123        let parent = Path::parse("/tmp").expect("valid parent path");
124        let options = TempOptions::new()
125            .with_parent(Some(parent.clone()))
126            .with_prefix("prefix")
127            .with_suffix("suffix")
128            .with_create_parent(true);
129
130        assert_eq!(options.parent(), Some(&parent));
131        assert_eq!(options.prefix(), "prefix");
132        assert_eq!(options.suffix(), "suffix");
133        assert!(options.creates_parent());
134    }
135}