use crate::tempdir::DIR_PREFIX;
use crate::tempfile::FILE_PREFIX;
use crate::{Error, TempDir, TempFile};
use std::path::PathBuf;
#[derive(Debug, Clone)]
pub struct TempFileBuilder {
dir: Option<PathBuf>,
prefix: String,
suffix: String,
}
impl TempFileBuilder {
pub(crate) fn new() -> Self {
Self {
dir: None,
prefix: FILE_PREFIX.to_string(),
suffix: String::new(),
}
}
pub fn prefix<S: Into<String>>(mut self, prefix: S) -> Self {
self.prefix = prefix.into();
self
}
pub fn suffix<S: Into<String>>(mut self, suffix: S) -> Self {
self.suffix = suffix.into();
self
}
pub fn dir<P: Into<PathBuf>>(mut self, dir: P) -> Self {
self.dir = Some(dir.into());
self
}
pub async fn create(self) -> Result<TempFile, Error> {
let dir = self.dir.unwrap_or_else(std::env::temp_dir);
TempFile::create_with_affixes(&dir, &self.prefix, &self.suffix).await
}
}
#[derive(Debug, Clone)]
pub struct TempDirBuilder {
root: Option<PathBuf>,
prefix: String,
suffix: String,
}
impl TempDirBuilder {
pub(crate) fn new() -> Self {
Self {
root: None,
prefix: DIR_PREFIX.to_string(),
suffix: String::new(),
}
}
pub fn prefix<S: Into<String>>(mut self, prefix: S) -> Self {
self.prefix = prefix.into();
self
}
pub fn suffix<S: Into<String>>(mut self, suffix: S) -> Self {
self.suffix = suffix.into();
self
}
pub fn dir<P: Into<PathBuf>>(mut self, root: P) -> Self {
self.root = Some(root.into());
self
}
pub async fn create(self) -> Result<TempDir, Error> {
let root = self.root.unwrap_or_else(std::env::temp_dir);
TempDir::create_with_affixes(&root, &self.prefix, &self.suffix).await
}
}