use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::fs::{self, read_to_string};
use std::path::PathBuf;
pub const MAIL_DIR: &str = "~/.local/share/mail";
#[derive(Clone, Deserialize, Serialize)]
#[allow(clippy::module_name_repetitions)]
pub struct TermailConfig {
pub mail_dir: String,
#[serde(skip_serializing)]
pub mail_dir_from_cli: Option<String>,
}
impl Default for TermailConfig {
fn default() -> Self {
Self {
mail_dir: MAIL_DIR.to_string(),
mail_dir_from_cli: None,
}
}
}
impl TermailConfig {
pub fn save(&self) -> Result<()> {
let mut path = get_app_config_path()?;
path.push("config.toml");
let string = toml::to_string(self)?;
fs::write(path.to_string_lossy().as_ref(), string)?;
Ok(())
}
pub fn load(&mut self) -> Result<()> {
let mut path = get_app_config_path()?;
path.push("config.toml");
if !path.exists() {
let config = Self::default();
config.save()?;
}
let string = read_to_string(path.to_string_lossy().as_ref())?;
let config: Self = toml::from_str(&string)?;
*self = config;
Ok(())
}
}
pub fn get_app_config_path() -> Result<PathBuf> {
let mut path =
dirs_next::config_dir().ok_or_else(|| anyhow!("failed to find os config dir."))?;
path.push("termail");
if !path.exists() {
fs::create_dir_all(&path)?;
}
Ok(path)
}