use crate::path::Path;
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TempOptions {
parent: Option<Path>,
prefix: String,
suffix: String,
create_parent: bool,
}
impl TempOptions {
#[inline]
#[must_use]
pub fn new() -> Self {
Self {
parent: None,
prefix: String::new(),
suffix: String::new(),
create_parent: false,
}
}
#[inline]
#[must_use]
pub const fn parent(&self) -> Option<&Path> {
self.parent.as_ref()
}
#[inline]
#[must_use]
pub fn prefix(&self) -> &str {
&self.prefix
}
#[inline]
#[must_use]
pub fn suffix(&self) -> &str {
&self.suffix
}
#[inline]
#[must_use]
pub const fn creates_parent(&self) -> bool {
self.create_parent
}
#[inline]
#[must_use]
pub fn with_parent(mut self, parent: Option<Path>) -> Self {
self.parent = parent;
self
}
#[inline]
#[must_use]
pub fn with_prefix(mut self, prefix: impl Into<String>) -> Self {
self.prefix = prefix.into();
self
}
#[inline]
#[must_use]
pub fn with_suffix(mut self, suffix: impl Into<String>) -> Self {
self.suffix = suffix.into();
self
}
#[inline]
#[must_use]
pub const fn with_create_parent(mut self, create: bool) -> Self {
self.create_parent = create;
self
}
}
impl Default for TempOptions {
#[inline]
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::TempOptions;
use crate::path::Path;
#[test]
fn option_accessors_are_executed_at_runtime() {
let parent = Path::parse("/tmp").expect("valid parent path");
let options = TempOptions::new()
.with_parent(Some(parent.clone()))
.with_prefix("prefix")
.with_suffix("suffix")
.with_create_parent(true);
assert_eq!(options.parent(), Some(&parent));
assert_eq!(options.prefix(), "prefix");
assert_eq!(options.suffix(), "suffix");
assert!(options.creates_parent());
}
}