qubit_fs/temp/
temp_options.rs1use crate::path::Path;
11
12#[non_exhaustive]
22#[derive(Clone, Debug, Eq, PartialEq)]
23pub struct TempOptions {
24 parent: Option<Path>,
26 prefix: String,
28 suffix: String,
30 create_parent: bool,
32}
33
34impl TempOptions {
35 #[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 #[inline]
49 #[must_use]
50 pub const fn parent(&self) -> Option<&Path> {
51 self.parent.as_ref()
52 }
53
54 #[inline]
56 #[must_use]
57 pub fn prefix(&self) -> &str {
58 &self.prefix
59 }
60
61 #[inline]
63 #[must_use]
64 pub fn suffix(&self) -> &str {
65 &self.suffix
66 }
67
68 #[inline]
70 #[must_use]
71 pub const fn creates_parent(&self) -> bool {
72 self.create_parent
73 }
74
75 #[inline]
77 #[must_use]
78 pub fn with_parent(mut self, parent: Option<Path>) -> Self {
79 self.parent = parent;
80 self
81 }
82
83 #[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 #[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 #[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 #[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}