late night grind: implementing cmdargs feature, fixing Args and Config, adding jobs field and more...
This commit is contained in:
parent
54f0b38252
commit
32a3c9fb12
@ -4,10 +4,13 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
default = ["cmdargs"]
|
||||
custom_pieces = []
|
||||
cmdargs = ["dep:clap"]
|
||||
|
||||
[dependencies]
|
||||
toml = { version = "0.8.12" }
|
||||
clap = { version = "4.5.4", features = ["derive"] }
|
||||
clap = { version = "4.5.4", features = ["derive"], optional = true }
|
||||
serde = { version = "1.0.197", features = ["derive"] }
|
||||
serde_derive = { version = "1.0.197" }
|
||||
num_cpus = "1.16.0"
|
||||
|
||||
37
config.toml
37
config.toml
@ -1,10 +1,43 @@
|
||||
[piece.turtle]
|
||||
change = [[1, 2], [2, 1]]
|
||||
change = [
|
||||
[
|
||||
3,
|
||||
2,
|
||||
],
|
||||
[
|
||||
3,
|
||||
-2,
|
||||
],
|
||||
[
|
||||
2,
|
||||
-3,
|
||||
],
|
||||
[
|
||||
2,
|
||||
3,
|
||||
],
|
||||
[
|
||||
-2,
|
||||
-3,
|
||||
],
|
||||
[
|
||||
-2,
|
||||
3,
|
||||
],
|
||||
[
|
||||
-3,
|
||||
-2,
|
||||
],
|
||||
[
|
||||
-3,
|
||||
2,
|
||||
],
|
||||
]
|
||||
|
||||
# INFO: when one: number of must-moves
|
||||
# if 0 infinite
|
||||
# INFO: when two: [0] = min, [1] = max
|
||||
iterable = [1, 2]
|
||||
iterable = [0]
|
||||
|
||||
[engine]
|
||||
depth = 23
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
use super::{piece::PieceVariant, square::Square};
|
||||
|
||||
use serde::Serialize;
|
||||
#[cfg(feature = "custom_pieces")]
|
||||
use serde_derive::Deserialize;
|
||||
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
use super::moves::Change;
|
||||
|
||||
use serde::Serialize;
|
||||
#[cfg(feature = "custom_pieces")]
|
||||
use serde_derive::Deserialize;
|
||||
|
||||
@ -38,13 +36,13 @@ impl Default for Iterability {
|
||||
#[cfg(feature = "custom_pieces")]
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct CustomPiece {
|
||||
change: Vec<[u8; 2]>,
|
||||
change: Vec<[i8; 2]>,
|
||||
iterable: Vec<u8>
|
||||
}
|
||||
|
||||
#[cfg(feature = "custom_pieces")]
|
||||
impl CustomPiece {
|
||||
pub fn new(change: Vec<[u8; 2]>, iterable: Vec<u8>) -> Self {
|
||||
pub fn new(change: Vec<[i8; 2]>, iterable: Vec<u8>) -> Self {
|
||||
Self { change, iterable }
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,9 +1,6 @@
|
||||
use crate::chess::piece::Piece;
|
||||
use crate::chess::square::Square;
|
||||
use crate::chess::moves::Move;
|
||||
use crate::chess::moves::MoveVariant;
|
||||
|
||||
use super::moves::Change;
|
||||
|
||||
|
||||
pub struct Castling {
|
||||
@ -71,7 +68,7 @@ impl Position {
|
||||
Position::default()
|
||||
}
|
||||
|
||||
pub fn from_fen(fen: String) -> Self {
|
||||
pub fn from_fen(_fen: String) -> Self {
|
||||
todo!("Implement this function")
|
||||
}
|
||||
|
||||
|
||||
@ -1,7 +1,42 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
|
||||
#[derive(clap::Parser)]
|
||||
struct Args {
|
||||
#[arg(short, long, default_value = "config.toml")]
|
||||
config_path: std::path::PathBuf,
|
||||
depth: Option<u32>
|
||||
pub struct Args {
|
||||
#[arg(short, long="config", default_value = "config.toml")]
|
||||
config_path: PathBuf,
|
||||
|
||||
#[arg(short, long, default_value_t = 20)]
|
||||
depth: u8,
|
||||
|
||||
#[arg(short, long, default_value_t = 100)]
|
||||
strenght: u8,
|
||||
|
||||
#[arg(short, long, default_value_t = {num_cpus::get() as u8})]
|
||||
jobs: u8,
|
||||
}
|
||||
|
||||
impl Default for Args {
|
||||
fn default() -> Self {
|
||||
Self { config_path: "config.toml".into(), depth: 20, strenght: 100, jobs: num_cpus::get() as u8 }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Args {
|
||||
pub fn get_config_path(self) -> PathBuf {
|
||||
self.config_path
|
||||
}
|
||||
|
||||
pub fn get_depth(self) -> u8 {
|
||||
self.depth
|
||||
}
|
||||
|
||||
pub fn get_strenght(self) -> u8 {
|
||||
self.strenght
|
||||
}
|
||||
|
||||
pub fn get_jobs(self) -> u8 {
|
||||
self.jobs
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,43 +1,46 @@
|
||||
use std::{collections::HashMap, fs::File, io::Read, path::Path};
|
||||
use crate::chess::moves::Change;
|
||||
use std::{fs::File, io::Read, path::Path};
|
||||
use serde::Deserialize;
|
||||
|
||||
|
||||
#[cfg(feature = "custom_pieces")]
|
||||
use crate::chess::piece::{CustomPiece, Iterability};
|
||||
use crate::chess::piece::CustomPiece;
|
||||
#[cfg(feature = "custom_pieces")]
|
||||
use std::collections::HashMap;
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Engine {
|
||||
depth: u32
|
||||
depth: Option<u8>,
|
||||
jobs: Option<u8>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct Config {
|
||||
// #[serde(rename = "piece")]
|
||||
#[cfg(feature = "custom_pieces")]
|
||||
piece: HashMap<String, CustomPiece>,
|
||||
#[serde(rename = "piece")]
|
||||
pieces: Option<HashMap<String, CustomPiece>>,
|
||||
|
||||
engine: Engine
|
||||
engine: Option<Engine>
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
#[cfg(feature = "custom_pieces")]
|
||||
piece: HashMap::from(
|
||||
[("".to_string(), CustomPiece::new(vec![[0, 0]], vec![0]))]
|
||||
),
|
||||
pieces: None,
|
||||
|
||||
engine: Some(Engine {
|
||||
depth: Some(20),
|
||||
jobs: Some(num_cpus::get() as u8),
|
||||
}),
|
||||
|
||||
engine: Engine {
|
||||
depth: 20
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn from_file(path: &Path) -> Config {
|
||||
pub fn read(path: &Path) -> Config {
|
||||
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");
|
||||
|
||||
@ -1,2 +1,4 @@
|
||||
pub mod config;
|
||||
|
||||
#[cfg(feature = "cmdargs")]
|
||||
pub mod args;
|
||||
|
||||
30
src/main.rs
30
src/main.rs
@ -1,15 +1,37 @@
|
||||
#![allow(dead_code)]
|
||||
#![allow(
|
||||
dead_code,
|
||||
clippy::module_inception
|
||||
)]
|
||||
|
||||
|
||||
use chess::position::Position;
|
||||
use std::path::Path;
|
||||
use config::config::Config;
|
||||
|
||||
#[cfg(feature = "cmdargs")]
|
||||
use config::args::Args;
|
||||
#[cfg(feature = "cmdargs")]
|
||||
use clap::Parser;
|
||||
|
||||
#[cfg(not(feature = "cmdargs"))]
|
||||
use std::path::Path;
|
||||
|
||||
mod chess;
|
||||
mod config;
|
||||
|
||||
|
||||
fn main() {
|
||||
let config = Config::from_file(Path::new("./config.toml"));
|
||||
let config;
|
||||
|
||||
#[cfg(feature = "cmdargs")]
|
||||
{
|
||||
// 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());
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "cmdargs"))]
|
||||
{
|
||||
config = Config::read(Path::new("./config.toml"));
|
||||
}
|
||||
|
||||
println!("{config:#?}");
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user