forked from katzen-cafe/iowo
app: improve config loading and merging
This commit is contained in:
parent
ea2e5d6075
commit
7c9dca0ae2
4 changed files with 73 additions and 54 deletions
|
@ -1,6 +1,10 @@
|
|||
use clap::Parser;
|
||||
|
||||
use self::{cli::Args, config_file::Configs};
|
||||
use self::{
|
||||
cli::Args,
|
||||
config_file::{find_config_file, Configs},
|
||||
error::ConfigError,
|
||||
};
|
||||
|
||||
mod cli;
|
||||
mod config_file;
|
||||
|
@ -11,14 +15,49 @@ pub struct Config {
|
|||
}
|
||||
|
||||
impl Config {
|
||||
pub fn read() -> Self {
|
||||
pub fn read() -> Result<Self, ConfigError> {
|
||||
let args = Args::parse();
|
||||
let cfg = Configs::read(args.config_file);
|
||||
let config_path = if let Some(config_path) = args.config_path {
|
||||
config_path
|
||||
} else {
|
||||
find_config_file()?
|
||||
};
|
||||
let file_config = Configs::read(config_path)?;
|
||||
|
||||
Self {
|
||||
Ok(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 || cfg.no_startup_message),
|
||||
startup_msg: !(args.no_startup_message || file_config.no_startup_message),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue