use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use crate::{log::Output, util::PathExt};
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Settings {
pub version: String,
pub home: PathBuf,
pub root: PathBuf,
pub config_file: PathBuf,
pub lock_file: PathBuf,
pub clone_dir: PathBuf,
pub download_dir: PathBuf,
}
#[derive(Debug)]
pub struct Context<'a> {
pub settings: &'a Settings,
pub output: &'a Output,
}
#[derive(Debug)]
pub struct EditContext {
pub settings: Settings,
pub output: Output,
}
#[derive(Debug)]
pub struct LockContext {
pub settings: Settings,
pub output: Output,
pub reinstall: bool,
}
macro_rules! setting_access {
($name:ident) => {
#[inline]
fn $name(&self) -> &Path {
self.settings().$name.as_path()
}
};
}
pub trait SettingsExt {
fn settings(&self) -> &Settings;
setting_access!(home);
setting_access!(root);
setting_access!(config_file);
setting_access!(lock_file);
setting_access!(clone_dir);
setting_access!(download_dir);
#[inline]
fn expand_tilde(&self, path: PathBuf) -> PathBuf {
path.expand_tilde(self.home())
}
#[inline]
fn replace_home<P>(&self, path: P) -> PathBuf
where
P: AsRef<Path>,
{
path.as_ref().replace_home(self.home())
}
}
impl SettingsExt for Settings {
#[inline]
fn settings(&self) -> &Settings {
self
}
}
impl SettingsExt for Context<'_> {
#[inline]
fn settings(&self) -> &Settings {
self.settings
}
}
impl SettingsExt for EditContext {
#[inline]
fn settings(&self) -> &Settings {
&self.settings
}
}
impl SettingsExt for LockContext {
#[inline]
fn settings(&self) -> &Settings {
&self.settings
}
}