first structure for Errors and crates for tracing added + added logging feature
This commit is contained in:
parent
cc3da89aee
commit
b7faca1e4b
@ -5,6 +5,7 @@ edition = "2021"
|
|||||||
|
|
||||||
[features]
|
[features]
|
||||||
custom_pieces = []
|
custom_pieces = []
|
||||||
|
logging = ["dep:tracing", "dep:tracing-subscriber"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
toml = { version = "0.8.12" }
|
toml = { version = "0.8.12" }
|
||||||
@ -12,3 +13,5 @@ clap = { version = "4.5.4", features = ["derive"] }
|
|||||||
serde = { version = "1.0.197", features = ["derive"] }
|
serde = { version = "1.0.197", features = ["derive"] }
|
||||||
serde_derive = { version = "1.0.197" }
|
serde_derive = { version = "1.0.197" }
|
||||||
num_cpus = "1.16.0"
|
num_cpus = "1.16.0"
|
||||||
|
tracing = { version = "0.1.40", optional = true }
|
||||||
|
tracing-subscriber = { version = "0.3.18", optional = true }
|
||||||
|
|||||||
@ -4,52 +4,44 @@ use std::path::PathBuf;
|
|||||||
#[derive(clap::Parser)]
|
#[derive(clap::Parser)]
|
||||||
pub struct Args {
|
pub struct Args {
|
||||||
#[arg(short, long="config", default_value = "config.toml")]
|
#[arg(short, long="config", default_value = "config.toml")]
|
||||||
config_path: PathBuf,
|
config_path: Option<PathBuf>,
|
||||||
|
|
||||||
#[arg(short, long, default_value_t = 20)]
|
#[arg(short, long)]
|
||||||
depth: u8,
|
depth: Option<u8>,
|
||||||
|
|
||||||
#[arg(short, long, default_value_t = 100)]
|
#[arg(short, long)]
|
||||||
strenght: u8,
|
strenght: Option<u8>,
|
||||||
|
|
||||||
#[arg(short, long, default_value_t = {num_cpus::get() as u8})]
|
#[arg(short, long)]
|
||||||
jobs: u8,
|
jobs: Option<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Args {
|
impl Default for Args {
|
||||||
fn default() -> Self {
|
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 {
|
impl Args {
|
||||||
pub fn get_config_path(self) -> PathBuf {
|
pub fn get_config_path(&self) -> Option<PathBuf> {
|
||||||
self.config_path
|
self.config_path.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_depth(self) -> u8 {
|
pub fn get_depth(&self) -> Option<u8> {
|
||||||
self.depth
|
self.depth
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_strenght(self) -> u8 {
|
pub fn get_strenght(&self) -> Option<u8> {
|
||||||
self.strenght
|
self.strenght
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_jobs(self) -> u8 {
|
pub fn get_jobs(&self) -> Option<u8> {
|
||||||
self.jobs
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 serde::Deserialize;
|
||||||
|
use crate::config::{args::Args, engine::Engine};
|
||||||
|
|
||||||
|
|
||||||
#[cfg(feature = "custom_pieces")]
|
#[cfg(feature = "custom_pieces")]
|
||||||
@ -7,15 +8,8 @@ use crate::chess::piece::CustomPiece;
|
|||||||
#[cfg(feature = "custom_pieces")]
|
#[cfg(feature = "custom_pieces")]
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::config::args::Args;
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
struct Engine {
|
|
||||||
depth: Option<u8>,
|
|
||||||
jobs: Option<u8>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
@ -32,17 +26,15 @@ impl Default for Config {
|
|||||||
#[cfg(feature = "custom_pieces")]
|
#[cfg(feature = "custom_pieces")]
|
||||||
pieces: None,
|
pieces: None,
|
||||||
|
|
||||||
engine: Some(Engine {
|
engine: Some(Engine::default()),
|
||||||
depth: Some(20),
|
|
||||||
jobs: Some(num_cpus::get() as u8),
|
|
||||||
}),
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl Config {
|
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 file = File::open(path).expect("Failed to read config file");
|
||||||
let mut config_string = String::new();
|
let mut config_string = String::new();
|
||||||
file.read_to_string(&mut config_string).expect("Failed to parse file to string");
|
file.read_to_string(&mut config_string).expect("Failed to parse file to string");
|
||||||
@ -53,10 +45,11 @@ impl Config {
|
|||||||
Self {
|
Self {
|
||||||
#[cfg(feature = "custom_pieces")]
|
#[cfg(feature = "custom_pieces")]
|
||||||
pieces: self.pieces,
|
pieces: self.pieces,
|
||||||
engine: Some(Engine {
|
|
||||||
depth: Some(args.get_ref_depth()),
|
engine: Some(Engine::new(
|
||||||
jobs: Some(args.get_ref_jobs())
|
args.get_depth().or(self.engine.clone().unwrap_or_default().depth()),
|
||||||
}),
|
args.get_jobs().or(self.engine.clone().unwrap_or_default().jobs()),
|
||||||
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
35
src/config/engine.rs
Normal file
35
src/config/engine.rs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
|
pub struct Engine {
|
||||||
|
depth: Option<u8>,
|
||||||
|
jobs: Option<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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<u8>, jobs: Option<u8>) -> Self {
|
||||||
|
Self {
|
||||||
|
depth,
|
||||||
|
jobs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Engine {
|
||||||
|
pub fn depth(self) -> Option<u8> {
|
||||||
|
self.depth
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn jobs(self) -> Option<u8> {
|
||||||
|
self.jobs
|
||||||
|
}
|
||||||
|
}
|
||||||
38
src/config/error.rs
Normal file
38
src/config/error.rs
Normal file
@ -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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,2 +1,18 @@
|
|||||||
pub mod config;
|
mod config;
|
||||||
pub mod args;
|
mod args;
|
||||||
|
mod engine;
|
||||||
|
pub mod error;
|
||||||
|
|
||||||
|
use self::{args::Args, config::Config, error::ConfigError};
|
||||||
|
use clap::Parser;
|
||||||
|
|
||||||
|
pub fn get_config() -> Result<Config, ConfigError> {
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|||||||
23
src/errors.rs
Normal file
23
src/errors.rs
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
20
src/main.rs
20
src/main.rs
@ -3,27 +3,13 @@
|
|||||||
clippy::module_inception
|
clippy::module_inception
|
||||||
)]
|
)]
|
||||||
|
|
||||||
|
use crate::config::get_config;
|
||||||
use config::config::Config;
|
|
||||||
use config::args::Args;
|
|
||||||
use clap::Parser;
|
|
||||||
// TODO: move all of this into seperate file
|
|
||||||
|
|
||||||
|
|
||||||
mod chess;
|
mod chess;
|
||||||
mod config;
|
mod config;
|
||||||
|
mod errors;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let config;
|
let _config = get_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:#?}");
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user