diff --git a/Cargo.toml b/Cargo.toml index a1e9abd..e8a832d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [features] custom_pieces = [] +logging = ["dep:tracing", "dep:tracing-subscriber"] [dependencies] toml = { version = "0.8.12" } @@ -12,3 +13,5 @@ clap = { version = "4.5.4", features = ["derive"] } serde = { version = "1.0.197", features = ["derive"] } serde_derive = { version = "1.0.197" } num_cpus = "1.16.0" +tracing = { version = "0.1.40", optional = true } +tracing-subscriber = { version = "0.3.18", optional = true } diff --git a/src/config/args.rs b/src/config/args.rs index 7dc8116..b6bf36f 100644 --- a/src/config/args.rs +++ b/src/config/args.rs @@ -4,52 +4,44 @@ use std::path::PathBuf; #[derive(clap::Parser)] pub struct Args { #[arg(short, long="config", default_value = "config.toml")] - config_path: PathBuf, + config_path: Option, - #[arg(short, long, default_value_t = 20)] - depth: u8, + #[arg(short, long)] + depth: Option, - #[arg(short, long, default_value_t = 100)] - strenght: u8, + #[arg(short, long)] + strenght: Option, - #[arg(short, long, default_value_t = {num_cpus::get() as u8})] - jobs: u8, + #[arg(short, long)] + jobs: Option, } impl Default for Args { fn default() -> Self { - Self { config_path: "config.toml".into(), depth: 20, strenght: 100, jobs: num_cpus::get() as u8 } + Self { + config_path: Some("config.toml".into()), + depth: None, + strenght: None, + jobs: None, + } } } impl Args { - pub fn get_config_path(self) -> PathBuf { - self.config_path + pub fn get_config_path(&self) -> Option { + self.config_path.clone() } - pub fn get_depth(self) -> u8 { + pub fn get_depth(&self) -> Option { self.depth } - pub fn get_strenght(self) -> u8 { + pub fn get_strenght(&self) -> Option { self.strenght } - pub fn get_jobs(self) -> u8 { + pub fn get_jobs(&self) -> Option { self.jobs } - - pub fn get_ref_depth(&self) -> u8 { - self.depth - } - - pub fn get_ref_strenght(&self) -> u8 { - self.strenght - } - - pub fn get_ref_jobs(&self) -> u8 { - self.jobs - } - } diff --git a/src/config/config.rs b/src/config/config.rs index 2d8e1bf..1dbebe9 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -1,5 +1,6 @@ -use std::{fs::File, io::Read, path::Path}; +use std::{fs::File, io::Read, path::Path, u8}; use serde::Deserialize; +use crate::config::{args::Args, engine::Engine}; #[cfg(feature = "custom_pieces")] @@ -7,15 +8,8 @@ use crate::chess::piece::CustomPiece; #[cfg(feature = "custom_pieces")] use std::collections::HashMap; -use crate::config::args::Args; -#[derive(Debug, Deserialize)] -struct Engine { - depth: Option, - jobs: Option, -} - #[derive(Debug, Deserialize)] #[serde(default)] pub struct Config { @@ -32,17 +26,15 @@ impl Default for Config { #[cfg(feature = "custom_pieces")] pieces: None, - engine: Some(Engine { - depth: Some(20), - jobs: Some(num_cpus::get() as u8), - }), + engine: Some(Engine::default()), } } } + impl Config { - pub fn read(path: &Path) -> Config { + pub fn read(path: &Path) -> Self { let mut file = File::open(path).expect("Failed to read config file"); let mut config_string = String::new(); file.read_to_string(&mut config_string).expect("Failed to parse file to string"); @@ -53,10 +45,11 @@ impl Config { Self { #[cfg(feature = "custom_pieces")] pieces: self.pieces, - engine: Some(Engine { - depth: Some(args.get_ref_depth()), - jobs: Some(args.get_ref_jobs()) - }), + + engine: Some(Engine::new( + args.get_depth().or(self.engine.clone().unwrap_or_default().depth()), + args.get_jobs().or(self.engine.clone().unwrap_or_default().jobs()), + )), } } } diff --git a/src/config/engine.rs b/src/config/engine.rs new file mode 100644 index 0000000..2621511 --- /dev/null +++ b/src/config/engine.rs @@ -0,0 +1,35 @@ +use serde::Deserialize; + +#[derive(Debug, Deserialize, Clone)] +pub struct Engine { + depth: Option, + jobs: Option, +} + +impl Default for Engine { + fn default() -> Self { + Self { + depth: Some(20), + jobs: Some(num_cpus::get() as u8) + } + } +} + +impl Engine { + pub fn new(depth: Option, jobs: Option) -> Self { + Self { + depth, + jobs + } + } +} + +impl Engine { + pub fn depth(self) -> Option { + self.depth + } + + pub fn jobs(self) -> Option { + self.jobs + } +} diff --git a/src/config/error.rs b/src/config/error.rs new file mode 100644 index 0000000..6554b77 --- /dev/null +++ b/src/config/error.rs @@ -0,0 +1,38 @@ +use std::io; + +use crate::errors::ErrorType; + +#[derive(Debug)] +pub enum ConfigError { + Arguments(clap::error::Error), + ConfigFile(io::Error), + Merging(String) +} + +impl ConfigError { + fn throw(self) -> ErrorType { + match self { + Self::Arguments(error) => { + ErrorType::new( + "Parsing Arguments".to_string(), + error.to_string(), + false + ) + }, + Self::ConfigFile(error) => { + ErrorType::new( + "Reading Config File".to_string(), + error.to_string(), + false + ) + }, + Self::Merging(error) => { + ErrorType::new( + "Reading Config File".to_string(), + error, + false + ) + } + } + } +} diff --git a/src/config/mod.rs b/src/config/mod.rs index 1c94de5..4144926 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,2 +1,18 @@ -pub mod config; -pub mod args; +mod config; +mod args; +mod engine; +pub mod error; + +use self::{args::Args, config::Config, error::ConfigError}; +use clap::Parser; + +pub fn get_config() -> Result { + // TODO: implement error handling and logging and handle this case appropriate + let args = Args::try_parse().unwrap_or_else(|err|{ + println!("error parsing cmd args\n{:#?}", err); + Args::default() + }); + let config_path = args.get_config_path().unwrap_or_default(); + let config = Config::read(&config_path).merge_args(&args); + Ok(config) +} diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..9115d2d --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,23 @@ +use crate::config::error::ConfigError; + + +#[derive(Debug)] +pub struct ErrorType { + kind: String, + message: String, + fatal: bool +} + +impl ErrorType { + pub fn new(kind: String, message: String, fatal: bool) -> Self { + ErrorType { kind, message, fatal } + } +} + + +#[derive(Debug)] +pub enum SaltfishError { + ConfigError(ConfigError) +} + + diff --git a/src/main.rs b/src/main.rs index abf48a4..02b61cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,27 +3,13 @@ clippy::module_inception )] - -use config::config::Config; -use config::args::Args; -use clap::Parser; -// TODO: move all of this into seperate file - +use crate::config::get_config; mod chess; mod config; +mod errors; fn main() { - let config; - - // TODO: implement error handling and logging and handle this case appropriate - let args = Args::try_parse().unwrap_or_else(|err|{ - println!("error parsing cmd args\n{:#?}", err); - Args::default() - }); - config = Config::read(&args.get_config_path()); - - - println!("{config:#?}"); + let _config = get_config(); }