From ea2e5d6075e6d48d15b204c7a666126dcbc069de Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Mon, 15 Jan 2024 09:03:55 +0100 Subject: [PATCH] app: unify config handling stuff a bit --- crates/app/src/config.rs | 80 ++++++---------------------- crates/app/src/{ => config}/cli.rs | 0 crates/app/src/config/config_file.rs | 75 ++++++++++++++++++++++++++ crates/app/src/main.rs | 11 ++-- crates/rpl/Cargo.toml | 2 +- 5 files changed, 94 insertions(+), 74 deletions(-) rename crates/app/src/{ => config}/cli.rs (100%) create mode 100644 crates/app/src/config/config_file.rs diff --git a/crates/app/src/config.rs b/crates/app/src/config.rs index 86f6763..c19b94a 100644 --- a/crates/app/src/config.rs +++ b/crates/app/src/config.rs @@ -1,74 +1,24 @@ -use std::{ - fs, - path::{Path, PathBuf}, - process, -}; +use clap::Parser; -use serde::{Deserialize, Serialize}; +use self::{cli::Args, config_file::Configs}; -use crate::error_reporting::{report_serde_json_err, report_serde_ron_err}; +mod cli; +mod config_file; -#[derive(Debug, Serialize, Deserialize)] -pub struct Configs { - #[serde(default = "default_example_value")] - pub example_value: i32, - #[serde(default = "default_no_startup_msg")] - pub no_startup_message: bool, +/// this struct may hold all configuration +pub struct Config { + pub startup_msg: bool, } -/// what the fuck serde why do i need this -fn default_example_value() -> i32 { - 43 -} -fn default_no_startup_msg() -> bool { - false -} +impl Config { + pub fn read() -> Self { + let args = Args::parse(); + let cfg = Configs::read(args.config_file); -impl Configs { - pub fn read(custom_path: Option) -> Self { - use owo_colors::OwoColorize; - let p = match custom_path { - Some(p) => p, - None => { - let config_path = dirs::config_dir().expect("config dir should exist"); - - let ron_path = config_path.with_file_name("config.ron"); - let json_path = config_path.with_file_name("config.json"); - - if Path::new(&ron_path).exists() { - ron_path - } else if Path::new(&json_path).exists() { - json_path - } else { - eprintln!("{}: couldn't find config file", "Fatal error".red()); - process::exit(1) - } - } - }; - - match p.extension().map(|v| v.to_str().unwrap()) { - Some("ron") => Self::read_json(&fs::read_to_string(p).unwrap()), - Some("json") => Self::read_ron(&fs::read_to_string(p).unwrap()), - None | Some(_) => { - eprintln!( - "{}: couldn't determine config file type", - "Fatal error".red() - ); - process::exit(1) - } - } - } - pub fn read_json(config_text: &str) -> Configs { - match serde_json::from_str(config_text) { - Ok(c) => c, - Err(e) => report_serde_json_err(config_text, e), - } - } - - pub fn read_ron(config_text: &str) -> Configs { - match ron::from_str(config_text) { - Ok(c) => c, - Err(e) => report_serde_ron_err(config_text, e), + 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), } } } diff --git a/crates/app/src/cli.rs b/crates/app/src/config/cli.rs similarity index 100% rename from crates/app/src/cli.rs rename to crates/app/src/config/cli.rs diff --git a/crates/app/src/config/config_file.rs b/crates/app/src/config/config_file.rs new file mode 100644 index 0000000..fc96675 --- /dev/null +++ b/crates/app/src/config/config_file.rs @@ -0,0 +1,75 @@ +use std::{ + fs, + path::{Path, PathBuf}, + process, +}; + +use serde::{Deserialize, Serialize}; + +use crate::error_reporting::{report_serde_json_err, report_serde_ron_err}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Configs { + #[serde(default = "default_example_value")] + pub example_value: i32, + #[serde(default = "default_no_startup_msg")] + pub no_startup_message: bool, +} + +/// what the fuck serde why do i need this +pub(crate) fn default_example_value() -> i32 { + 43 +} + +pub(crate) fn default_no_startup_msg() -> bool { + false +} + +impl Configs { + pub fn read(custom_path: Option) -> Self { + use owo_colors::OwoColorize; + let p = match custom_path { + Some(p) => p, + None => { + let config_path = dirs::config_dir().expect("config dir should exist"); + + let ron_path = config_path.with_file_name("config.ron"); + let json_path = config_path.with_file_name("config.json"); + + if Path::new(&ron_path).exists() { + ron_path + } else if Path::new(&json_path).exists() { + json_path + } else { + eprintln!("{}: couldn't find config file", "Fatal error".red()); + process::exit(1) + } + } + }; + + match p.extension().map(|v| v.to_str().unwrap()) { + Some("ron") => Self::read_json(&fs::read_to_string(p).unwrap()), + Some("json") => Self::read_ron(&fs::read_to_string(p).unwrap()), + None | Some(_) => { + eprintln!( + "{}: couldn't determine config file type", + "Fatal error".red() + ); + process::exit(1) + } + } + } + pub fn read_json(config_text: &str) -> Configs { + match serde_json::from_str(config_text) { + Ok(c) => c, + Err(e) => report_serde_json_err(config_text, e), + } + } + + pub fn read_ron(config_text: &str) -> Configs { + match ron::from_str(config_text) { + Ok(c) => c, + Err(e) => report_serde_ron_err(config_text, e), + } + } +} diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 491e8b4..deab5b4 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -1,19 +1,14 @@ -use clap::Parser; -use cli::Args; +use config::Config; use welcome_msg::print_startup_msg; -use crate::config::Configs; - -mod cli; mod config; mod error_reporting; mod welcome_msg; fn main() { - let args = Args::parse(); - let cfg = Configs::read(args.config_file); + let cfg = Config::read(); - if !(args.no_startup_message || cfg.no_startup_message) { + if cfg.startup_msg { print_startup_msg(); } } diff --git a/crates/rpl/Cargo.toml b/crates/rpl/Cargo.toml index d125c78..b40a46e 100644 --- a/crates/rpl/Cargo.toml +++ b/crates/rpl/Cargo.toml @@ -7,4 +7,4 @@ edition = "2021" [dependencies] serde = { workspace = true, features = [ "derive" ] } -ron = { workspace = true } +ron = "0.8"