use anyhow::anyhow;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
#[derive(Debug, Serialize, Deserialize)]
pub enum AttritionType {
Each,
Percentage,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct AttritionEntry {
pub value: u32,
pub attype: AttritionType,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct AttritionConfig {
pub entries: Vec<AttritionEntry>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
pub database_name: String,
pub library_name: String,
pub attrition_config: AttritionConfig,
pub part_number_ignore_list: Vec<String>,
}
pub fn save_config(config: &Config, config_path: &Path) -> anyhow::Result<()> {
let config_string = toml::to_string(config).unwrap();
std::fs::write(config_path, config_string)?;
Ok(())
}
pub fn load_config(config_path: &Path) -> anyhow::Result<Config> {
let config = std::fs::read_to_string(&config_path)?;
Ok(toml::from_str(&config)?)
}
pub fn get_config_path(config_path: &Option<String>) -> anyhow::Result<PathBuf> {
match config_path {
Some(c) => Ok(PathBuf::from(c)),
None => {
let mut path = get_default_config_path()?;
std::fs::create_dir_all(&path)?;
path.push("config.toml");
Ok(path)
}
}
}
pub fn get_default_config_path() -> anyhow::Result<PathBuf> {
let mut config_path = match home::home_dir() {
Some(path) => path,
None => {
return Err(anyhow!("Impossible to get your home dir!"));
}
};
config_path.push(".eagle-plm");
Ok(config_path)
}