2024-01-15 08:03:55 +00:00
|
|
|
use clap::Parser;
|
2024-01-12 08:36:30 +00:00
|
|
|
|
2024-01-15 09:43:35 +00:00
|
|
|
use self::{
|
|
|
|
cli::Args,
|
|
|
|
config_file::{find_config_file, Configs},
|
|
|
|
};
|
2024-01-12 08:36:30 +00:00
|
|
|
|
2024-01-15 08:03:55 +00:00
|
|
|
mod cli;
|
|
|
|
mod config_file;
|
2024-01-12 08:36:30 +00:00
|
|
|
|
2024-01-15 08:03:55 +00:00
|
|
|
/// this struct may hold all configuration
|
|
|
|
pub struct Config {
|
|
|
|
pub startup_msg: bool,
|
2024-01-12 08:36:30 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 08:03:55 +00:00
|
|
|
impl Config {
|
2024-01-19 07:54:36 +00:00
|
|
|
pub fn read() -> Self {
|
2024-01-15 08:03:55 +00:00
|
|
|
let args = Args::parse();
|
2024-01-15 09:43:35 +00:00
|
|
|
let config_path = if let Some(config_path) = args.config_path {
|
2024-01-19 07:54:36 +00:00
|
|
|
Ok(config_path)
|
2024-01-15 09:43:35 +00:00
|
|
|
} else {
|
2024-01-19 07:54:36 +00:00
|
|
|
find_config_file()
|
2024-01-15 09:43:35 +00:00
|
|
|
};
|
2024-01-12 08:36:30 +00:00
|
|
|
|
2024-01-19 07:54:36 +00:00
|
|
|
let file_config = if let Ok(config_path) = config_path {
|
|
|
|
let file_config = Configs::read(config_path);
|
|
|
|
|
|
|
|
match file_config {
|
|
|
|
Ok(c) => Some(c),
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("Config error: {e:?}");
|
|
|
|
eprintln!("Proceeding with defaults or cli args...");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(file_config) = file_config {
|
|
|
|
Self {
|
|
|
|
// this is negated because to an outward api, the negative is more intuitive,
|
|
|
|
// while in the source the other way around is more intuitive
|
|
|
|
startup_msg: !(args.no_startup_message || file_config.no_startup_message),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Self {
|
|
|
|
startup_msg: !args.no_startup_message,
|
|
|
|
}
|
|
|
|
}
|
2024-01-15 09:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod error {
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ConfigError {
|
|
|
|
NoConfigDir,
|
|
|
|
NoConfigFileFound,
|
|
|
|
IoError(std::io::Error),
|
|
|
|
UnknownExtension(Option<String>),
|
|
|
|
SerdeJsonError(serde_json::Error),
|
|
|
|
SerdeRonError(ron::error::SpannedError),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::io::Error> for ConfigError {
|
|
|
|
fn from(value: std::io::Error) -> Self {
|
|
|
|
Self::IoError(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<serde_json::Error> for ConfigError {
|
|
|
|
fn from(value: serde_json::Error) -> Self {
|
|
|
|
Self::SerdeJsonError(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ron::error::SpannedError> for ConfigError {
|
|
|
|
fn from(value: ron::error::SpannedError) -> Self {
|
|
|
|
Self::SerdeRonError(value)
|
2024-01-12 08:36:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|