From 54f0b38252e84276413b35ff55d82087efac1ceb Mon Sep 17 00:00:00 2001 From: fabolous005 Date: Thu, 4 Apr 2024 01:52:01 +0200 Subject: [PATCH] add config and args parsing --- Cargo.toml | 11 +++++------ config.toml | 4 +++- src/chess/mod.rs | 4 ++-- src/chess/moves.rs | 8 ++++++++ src/chess/piece.rs | 24 +++++++++++++++++++---- src/config/args.rs | 7 +++++++ src/config/config.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++ src/config/mod.rs | 2 ++ src/main.rs | 6 +++++- 9 files changed, 98 insertions(+), 14 deletions(-) create mode 100644 src/config/args.rs create mode 100644 src/config/config.rs create mode 100644 src/config/mod.rs diff --git a/Cargo.toml b/Cargo.toml index ffccbf8..a1513bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,12 +3,11 @@ name = "saltfish" version = "0.1.0" edition = "2021" - [features] -custom_pieces = ["dep:toml", "dep:serde", "dep:serde_derive"] - +custom_pieces = [] [dependencies] -toml = { version = "0.8.12", optional = true } -serde = { version = "1.0.197", optional = true } -serde_derive = { version = "1.0.197", optional = true } +toml = { version = "0.8.12" } +clap = { version = "4.5.4", features = ["derive"] } +serde = { version = "1.0.197", features = ["derive"] } +serde_derive = { version = "1.0.197" } diff --git a/config.toml b/config.toml index bcdb338..edabb68 100644 --- a/config.toml +++ b/config.toml @@ -1,4 +1,3 @@ -[[piece]] [piece.turtle] change = [[1, 2], [2, 1]] @@ -6,3 +5,6 @@ change = [[1, 2], [2, 1]] # if 0 infinite # INFO: when two: [0] = min, [1] = max iterable = [1, 2] + +[engine] +depth = 23 diff --git a/src/chess/mod.rs b/src/chess/mod.rs index cccf5ce..3b6412c 100644 --- a/src/chess/mod.rs +++ b/src/chess/mod.rs @@ -1,5 +1,5 @@ // TODO: remove this pub later pub mod position; -mod piece; -mod moves; +pub mod piece; +pub mod moves; mod square; diff --git a/src/chess/moves.rs b/src/chess/moves.rs index 721afe0..9fa159f 100644 --- a/src/chess/moves.rs +++ b/src/chess/moves.rs @@ -1,5 +1,6 @@ use super::{piece::PieceVariant, square::Square}; +use serde::Serialize; #[cfg(feature = "custom_pieces")] use serde_derive::Deserialize; @@ -36,6 +37,13 @@ pub struct Change { y: i8 } +impl Change { + pub fn new(x: i8, y: i8) -> Self { + Self { x, y } + } +} + + pub enum MoveVariant { King, KingCastle, diff --git a/src/chess/piece.rs b/src/chess/piece.rs index 13c61a6..d44a898 100644 --- a/src/chess/piece.rs +++ b/src/chess/piece.rs @@ -1,5 +1,6 @@ use super::moves::Change; +use serde::Serialize; #[cfg(feature = "custom_pieces")] use serde_derive::Deserialize; @@ -19,16 +20,31 @@ pub struct Piece { #[cfg(feature = "custom_pieces")] -enum Iterablity { +#[derive(Debug, Deserialize)] +pub enum Iterability { Infinite(u8), ForcedNum(u8), Range([u8; 2]) } +#[cfg(feature = "custom_pieces")] +impl Default for Iterability { + fn default() -> Self { + Self::Infinite(0) + } +} + #[cfg(feature = "custom_pieces")] #[derive(Debug, Deserialize)] -struct CustomPiece { - change: Vec, - iterable: Vec> +pub struct CustomPiece { + change: Vec<[u8; 2]>, + iterable: Vec +} + +#[cfg(feature = "custom_pieces")] +impl CustomPiece { + pub fn new(change: Vec<[u8; 2]>, iterable: Vec) -> Self { + Self { change, iterable } + } } diff --git a/src/config/args.rs b/src/config/args.rs new file mode 100644 index 0000000..92958cc --- /dev/null +++ b/src/config/args.rs @@ -0,0 +1,7 @@ + +#[derive(clap::Parser)] +struct Args { + #[arg(short, long, default_value = "config.toml")] + config_path: std::path::PathBuf, + depth: Option +} diff --git a/src/config/config.rs b/src/config/config.rs new file mode 100644 index 0000000..b8489ea --- /dev/null +++ b/src/config/config.rs @@ -0,0 +1,46 @@ +use std::{collections::HashMap, fs::File, io::Read, path::Path}; +use crate::chess::moves::Change; +use serde::Deserialize; + +#[cfg(feature = "custom_pieces")] +use crate::chess::piece::{CustomPiece, Iterability}; + + +#[derive(Debug, Deserialize)] +struct Engine { + depth: u32 +} + +#[derive(Debug, Deserialize)] +#[serde(default)] +pub struct Config { + // #[serde(rename = "piece")] + #[cfg(feature = "custom_pieces")] + piece: HashMap, + + engine: 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]))] + ), + + engine: Engine { + depth: 20 + } + } + } +} + +impl Config { + pub fn from_file(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"); + toml::from_str(&config_string).expect("Failed to parse toml") + } +} diff --git a/src/config/mod.rs b/src/config/mod.rs new file mode 100644 index 0000000..1c94de5 --- /dev/null +++ b/src/config/mod.rs @@ -0,0 +1,2 @@ +pub mod config; +pub mod args; diff --git a/src/main.rs b/src/main.rs index 6dd5364..310453f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,14 @@ use chess::position::Position; +use std::path::Path; +use config::config::Config; mod chess; +mod config; fn main() { - let mut position = Position::new(); + let config = Config::from_file(Path::new("./config.toml")); + println!("{config:#?}"); }