use rand::{distr::Alphanumeric, rng, Rng};
use std::env;
use std::path::PathBuf;
#[cfg(feature = "yaml")]
use serde::Deserialize;
#[cfg(feature = "yaml")]
use serde::Serialize;
#[derive(Debug)]
pub struct Tree {
pub root: PathBuf,
pub drop: bool,
}
impl Drop for Tree {
fn drop(&mut self) {
if self.drop {
let _ = std::fs::remove_dir_all(&self.root);
}
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "yaml", derive(Deserialize, Serialize))]
#[derive(Default)]
pub struct Settings {
#[cfg_attr(
feature = "yaml",
serde(default, skip_serializing_if = "std::ops::Not::not")
)]
pub readonly: bool,
}
impl Settings {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub const fn readonly(mut self, value: bool) -> Self {
self.readonly = value;
self
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "yaml", derive(Deserialize))]
#[cfg_attr(feature = "yaml", serde(tag = "type"))]
pub enum Kind {
#[cfg_attr(feature = "yaml", serde(rename = "directory"))]
Directory,
#[cfg_attr(feature = "yaml", serde(rename = "empty_file"))]
EmptyFile,
#[cfg_attr(feature = "yaml", serde(rename = "text_file"))]
TextFile { content: String },
}
#[derive(Debug)]
#[cfg_attr(feature = "yaml", derive(Deserialize))]
pub struct Entry {
pub path: PathBuf,
#[cfg_attr(feature = "yaml", serde(flatten))]
pub kind: Kind,
#[cfg_attr(
feature = "yaml",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub settings: Option<Settings>,
}
pub fn temp_dir() -> PathBuf {
let random_string: String = rng()
.sample_iter(&Alphanumeric)
.take(5)
.map(char::from)
.collect();
env::temp_dir().join(random_string)
}