add config and args parsing
This commit is contained in:
parent
d6b52455bd
commit
54f0b38252
11
Cargo.toml
11
Cargo.toml
@ -3,12 +3,11 @@ name = "saltfish"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
custom_pieces = ["dep:toml", "dep:serde", "dep:serde_derive"]
|
custom_pieces = []
|
||||||
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
toml = { version = "0.8.12", optional = true }
|
toml = { version = "0.8.12" }
|
||||||
serde = { version = "1.0.197", optional = true }
|
clap = { version = "4.5.4", features = ["derive"] }
|
||||||
serde_derive = { version = "1.0.197", optional = true }
|
serde = { version = "1.0.197", features = ["derive"] }
|
||||||
|
serde_derive = { version = "1.0.197" }
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
[[piece]]
|
|
||||||
[piece.turtle]
|
[piece.turtle]
|
||||||
change = [[1, 2], [2, 1]]
|
change = [[1, 2], [2, 1]]
|
||||||
|
|
||||||
@ -6,3 +5,6 @@ change = [[1, 2], [2, 1]]
|
|||||||
# if 0 infinite
|
# if 0 infinite
|
||||||
# INFO: when two: [0] = min, [1] = max
|
# INFO: when two: [0] = min, [1] = max
|
||||||
iterable = [1, 2]
|
iterable = [1, 2]
|
||||||
|
|
||||||
|
[engine]
|
||||||
|
depth = 23
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
// TODO: remove this pub later
|
// TODO: remove this pub later
|
||||||
pub mod position;
|
pub mod position;
|
||||||
mod piece;
|
pub mod piece;
|
||||||
mod moves;
|
pub mod moves;
|
||||||
mod square;
|
mod square;
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
use super::{piece::PieceVariant, square::Square};
|
use super::{piece::PieceVariant, square::Square};
|
||||||
|
|
||||||
|
use serde::Serialize;
|
||||||
#[cfg(feature = "custom_pieces")]
|
#[cfg(feature = "custom_pieces")]
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
|
|
||||||
@ -36,6 +37,13 @@ pub struct Change {
|
|||||||
y: i8
|
y: i8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Change {
|
||||||
|
pub fn new(x: i8, y: i8) -> Self {
|
||||||
|
Self { x, y }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
pub enum MoveVariant {
|
pub enum MoveVariant {
|
||||||
King,
|
King,
|
||||||
KingCastle,
|
KingCastle,
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
use super::moves::Change;
|
use super::moves::Change;
|
||||||
|
|
||||||
|
use serde::Serialize;
|
||||||
#[cfg(feature = "custom_pieces")]
|
#[cfg(feature = "custom_pieces")]
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
|
|
||||||
@ -19,16 +20,31 @@ pub struct Piece {
|
|||||||
|
|
||||||
|
|
||||||
#[cfg(feature = "custom_pieces")]
|
#[cfg(feature = "custom_pieces")]
|
||||||
enum Iterablity {
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub enum Iterability {
|
||||||
Infinite(u8),
|
Infinite(u8),
|
||||||
ForcedNum(u8),
|
ForcedNum(u8),
|
||||||
Range([u8; 2])
|
Range([u8; 2])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "custom_pieces")]
|
||||||
|
impl Default for Iterability {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::Infinite(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(feature = "custom_pieces")]
|
#[cfg(feature = "custom_pieces")]
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct CustomPiece {
|
pub struct CustomPiece {
|
||||||
change: Vec<Change>,
|
change: Vec<[u8; 2]>,
|
||||||
iterable: Vec<Vec<u8>>
|
iterable: Vec<u8>
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "custom_pieces")]
|
||||||
|
impl CustomPiece {
|
||||||
|
pub fn new(change: Vec<[u8; 2]>, iterable: Vec<u8>) -> Self {
|
||||||
|
Self { change, iterable }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
7
src/config/args.rs
Normal file
7
src/config/args.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
#[derive(clap::Parser)]
|
||||||
|
struct Args {
|
||||||
|
#[arg(short, long, default_value = "config.toml")]
|
||||||
|
config_path: std::path::PathBuf,
|
||||||
|
depth: Option<u32>
|
||||||
|
}
|
||||||
46
src/config/config.rs
Normal file
46
src/config/config.rs
Normal file
@ -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<String, CustomPiece>,
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
2
src/config/mod.rs
Normal file
2
src/config/mod.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub mod config;
|
||||||
|
pub mod args;
|
||||||
@ -2,10 +2,14 @@
|
|||||||
|
|
||||||
|
|
||||||
use chess::position::Position;
|
use chess::position::Position;
|
||||||
|
use std::path::Path;
|
||||||
|
use config::config::Config;
|
||||||
|
|
||||||
mod chess;
|
mod chess;
|
||||||
|
mod config;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut position = Position::new();
|
let config = Config::from_file(Path::new("./config.toml"));
|
||||||
|
println!("{config:#?}");
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user