forked from katzen-cafe/iowo
chore: rename executor -> evaluator and integrate into app
This commit is contained in:
parent
de9ca81b65
commit
3c529c3a1a
16 changed files with 229 additions and 115 deletions
|
@ -1,3 +1,5 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
use self::{
|
||||
|
@ -10,6 +12,9 @@ mod config_file;
|
|||
|
||||
/// this struct may hold all configuration
|
||||
pub struct Config {
|
||||
pub source: PathBuf,
|
||||
pub evaluator: eval::Available,
|
||||
|
||||
pub startup_msg: bool,
|
||||
}
|
||||
|
||||
|
@ -17,37 +22,37 @@ impl Config {
|
|||
/// Get the configs from all possible places (args, file, env...)
|
||||
pub fn read() -> Self {
|
||||
let args = Args::parse();
|
||||
let config_path = if let Some(config_path) = args.config_path {
|
||||
Ok(config_path)
|
||||
let config = if let Some(config) = args.config_path {
|
||||
Ok(config)
|
||||
} else {
|
||||
find_config_file()
|
||||
};
|
||||
|
||||
// try to read a maybe existing config file
|
||||
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) => {
|
||||
let config = config.ok().and_then(|path| {
|
||||
Configs::read(path).map_or_else(
|
||||
|e| {
|
||||
eprintln!("Config error: {e:?}");
|
||||
eprintln!("Proceeding with defaults or cli args...");
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
},
|
||||
Some,
|
||||
)
|
||||
});
|
||||
|
||||
if let Some(file_config) = file_config {
|
||||
if let Some(file) = config {
|
||||
Self {
|
||||
source: args.source,
|
||||
evaluator: args.evaluator.and(file.evaluator).unwrap_or_default(),
|
||||
// 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),
|
||||
startup_msg: !(args.no_startup_message || file.no_startup_message),
|
||||
}
|
||||
} else {
|
||||
Self {
|
||||
source: args.source,
|
||||
startup_msg: !args.no_startup_message,
|
||||
evaluator: args.evaluator.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -56,7 +61,7 @@ impl Config {
|
|||
pub mod error {
|
||||
/// Errors that can occur when reading configs
|
||||
#[derive(Debug)]
|
||||
pub enum ConfigError {
|
||||
pub enum Config {
|
||||
/// The config dir doesn't exist
|
||||
NoConfigDir,
|
||||
/// We didn't find a config file in the config dir
|
||||
|
@ -73,19 +78,19 @@ pub mod error {
|
|||
SerdeRonError(ron::error::SpannedError),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for ConfigError {
|
||||
impl From<std::io::Error> for Config {
|
||||
fn from(value: std::io::Error) -> Self {
|
||||
Self::IoError(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::Error> for ConfigError {
|
||||
impl From<serde_json::Error> for Config {
|
||||
fn from(value: serde_json::Error) -> Self {
|
||||
Self::SerdeJsonError(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ron::error::SpannedError> for ConfigError {
|
||||
impl From<ron::error::SpannedError> for Config {
|
||||
fn from(value: ron::error::SpannedError) -> Self {
|
||||
Self::SerdeRonError(value)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue