use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use crate::error::AppError;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AppConfig {
pub style: String,
pub update: UpdateConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateConfig {
pub enabled: bool,
pub owner: String,
pub repo: String,
}
impl Default for AppConfig {
fn default() -> Self {
Self {
style: "friendly".into(),
update: UpdateConfig::default(),
}
}
}
impl Default for UpdateConfig {
fn default() -> Self {
Self {
enabled: true,
owner: "paperfoot".into(),
repo: "aiseo".into(),
}
}
}
pub fn config_path() -> PathBuf {
directories::ProjectDirs::from("", "", env!("CARGO_PKG_NAME"))
.map(|d| d.config_dir().to_path_buf())
.unwrap_or_else(|| PathBuf::from("."))
.join("config.toml")
}
pub fn load() -> Result<AppConfig, AppError> {
use figment::Figment;
use figment::providers::{Env, Format as _, Serialized, Toml};
let prefix = format!("{}_", env!("CARGO_PKG_NAME").to_uppercase());
Figment::from(Serialized::defaults(AppConfig::default()))
.merge(Toml::file(config_path()))
.merge(Env::prefixed(&prefix).split("_"))
.extract()
.map_err(|e| AppError::Config(e.to_string()))
}