use color_eyre::eyre::Context as _;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PunktfSource {
pub root: PathBuf,
pub profiles: PathBuf,
pub dotfiles: PathBuf,
}
impl PunktfSource {
pub fn from_root(root: PathBuf) -> color_eyre::Result<Self> {
macro_rules! try_exists {
( $var:ident ) => {
if $var.exists() {
} else {
let _ = std::fs::create_dir(&$var).wrap_err_with(|| {
format!(
"{} directory does not exist and could not be created (path: {})",
stringify!($var),
$var.display()
)
})?;
}
};
}
macro_rules! try_canonicalize {
($var:ident) => {
$var.canonicalize().wrap_err_with(|| {
format!(
"Failed to resolve punktf's {} directory (path: {})",
stringify!($var),
$var.display()
)
})?
};
}
let source = root;
try_exists!(source);
let source = try_canonicalize!(source);
let profiles = source.join("profiles");
try_exists!(profiles);
let profiles = try_canonicalize!(profiles);
let dotfiles = source.join("dotfiles");
try_exists!(dotfiles);
let dotfiles = try_canonicalize!(dotfiles);
Ok(Self {
root: source,
profiles,
dotfiles,
})
}
pub fn root(&self) -> &Path {
&self.root
}
pub fn profiles(&self) -> &Path {
&self.profiles
}
pub fn dotfiles(&self) -> &Path {
&self.dotfiles
}
}