use crate::prelude::*;
use caesura_macros::Options;
use serde::{Deserialize, Serialize};
const LEGACY_CONFIG_PATH: &str = "./config.yml";
pub(crate) const CONFIG_FILE_LABEL: &str = "Config File";
#[derive(Options, Clone, Debug, Deserialize, Serialize)]
pub struct ConfigOptions {
#[arg(long)]
#[options(default_doc = "`~/.config/caesura/config.yml` or platform equivalent")]
pub config: Option<PathBuf>,
}
impl ConfigOptions {
#[must_use]
pub fn path(&self) -> Option<PathBuf> {
self.config.as_ref().map(ExpandTilde::expand_tilde)
}
}
impl OptionsContract for ConfigOptions {
type Partial = ConfigOptionsPartial;
fn validate(&self, errors: &mut Vec<OptionRule>) {
if let Some(config) = self.path()
&& !config.is_file()
{
errors.push(DoesNotExist(
CONFIG_FILE_LABEL.to_owned(),
config.to_string_lossy().to_string(),
));
}
if !self
.config
.clone()
.unwrap_or(PathManager::default_config_path())
.is_file()
&& PathBuf::from(LEGACY_CONFIG_PATH).is_file()
{
let default_path = PathManager::default_config_path();
errors.push(Changed(
CONFIG_FILE_LABEL.to_owned(),
default_path.to_string_lossy().to_string(),
format!("In v0.27.0 the default config path changed to {}.\nPass the option: --config {LEGACY_CONFIG_PATH} to use the previous config path.", default_path.display()),
));
}
}
}