use std::path::{Path, PathBuf};
#[derive(Debug, Clone)]
pub struct SmolderDir {
path: PathBuf,
}
impl SmolderDir {
pub const NAME: &str = ".smolder";
pub fn new() -> Self {
Self {
path: PathBuf::from(Self::NAME),
}
}
pub fn at<P: Into<PathBuf>>(path: P) -> Self {
Self { path: path.into() }
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn join<P: AsRef<Path>>(&self, path: P) -> PathBuf {
self.path.join(path)
}
pub fn exists(&self) -> bool {
self.path.is_dir()
}
pub fn create(&self) -> std::io::Result<()> {
if !self.exists() {
std::fs::create_dir_all(&self.path)?;
}
Ok(())
}
}
impl Default for SmolderDir {
fn default() -> Self {
Self::new()
}
}
impl AsRef<Path> for SmolderDir {
fn as_ref(&self) -> &Path {
&self.path
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let dir = SmolderDir::new();
assert_eq!(dir.path(), Path::new(".smolder"));
}
#[test]
fn test_at() {
let dir = SmolderDir::at("/custom/path/.smolder");
assert_eq!(dir.path(), Path::new("/custom/path/.smolder"));
}
#[test]
fn test_join() {
let dir = SmolderDir::new();
assert_eq!(dir.join("smolder.db"), PathBuf::from(".smolder/smolder.db"));
assert_eq!(
dir.join("cache/artifacts"),
PathBuf::from(".smolder/cache/artifacts")
);
}
#[test]
fn test_default() {
let dir = SmolderDir::default();
assert_eq!(dir.path(), Path::new(".smolder"));
}
}