Convert the kifu config to the config crate
This commit is contained in:
parent
0765d94a5e
commit
efec8dfe5a
|
@ -928,6 +928,15 @@ dependencies = [
|
||||||
"system-deps",
|
"system-deps",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "grid"
|
||||||
|
version = "0.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0634107a3a005070dd73e27e74ecb691a94e9e5ba7829f434db7fbf73a6b5c47"
|
||||||
|
dependencies = [
|
||||||
|
"no-std-compat",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "gsk4"
|
name = "gsk4"
|
||||||
version = "0.6.3"
|
version = "0.6.3"
|
||||||
|
@ -1288,6 +1297,22 @@ dependencies = [
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "kifu-core"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"chrono",
|
||||||
|
"config",
|
||||||
|
"config-derive",
|
||||||
|
"cool_asserts",
|
||||||
|
"grid",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"sgf",
|
||||||
|
"thiserror",
|
||||||
|
"typeshare",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.4.0"
|
version = "1.4.0"
|
||||||
|
@ -1453,6 +1478,12 @@ dependencies = [
|
||||||
"tempfile",
|
"tempfile",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "no-std-compat"
|
||||||
|
version = "0.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b93853da6d84c2e3c7d730d6473e8817692dd89be387eb01b94d7f108ecb5b8c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nom"
|
name = "nom"
|
||||||
version = "7.1.3"
|
version = "7.1.3"
|
||||||
|
|
|
@ -9,6 +9,7 @@ members = [
|
||||||
"emseries",
|
"emseries",
|
||||||
"flow",
|
"flow",
|
||||||
"fluent-ergonomics",
|
"fluent-ergonomics",
|
||||||
|
"kifu/core",
|
||||||
"geo-types",
|
"geo-types",
|
||||||
"hex-grid",
|
"hex-grid",
|
||||||
"ifc",
|
"ifc",
|
||||||
|
|
|
@ -1,3 +1,13 @@
|
||||||
|
/*
|
||||||
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
fs::File,
|
||||||
|
hash::Hash,
|
||||||
|
io::{ErrorKind, Read},
|
||||||
|
path::PathBuf,
|
||||||
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
pub use config_derive::ConfigOption;
|
pub use config_derive::ConfigOption;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
|
@ -11,63 +21,55 @@ pub enum ConfigReadError {
|
||||||
InvalidJSON(serde_json::Error),
|
InvalidJSON(serde_json::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
macro_rules! define_config {
|
macro_rules! define_config {
|
||||||
($($name:ident($struct:ident),)+) => (
|
($($name:ident($struct:ident),)+) => (
|
||||||
use serde::{Deserialize, Serialize};
|
#[derive(Clone, Debug, Hash, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||||
use std::{
|
|
||||||
collections::HashMap,
|
|
||||||
fs::File,
|
|
||||||
hash::Hash,
|
|
||||||
io::{ErrorKind, Read},
|
|
||||||
path::PathBuf,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
|
||||||
pub enum ConfigName {
|
pub enum ConfigName {
|
||||||
$($name),+
|
$($name),+
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||||
pub enum ConfigOption {
|
pub enum ConfigOption {
|
||||||
$($name($struct)),+
|
$($name($struct)),+
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
values: HashMap<ConfigName, ConfigOption>,
|
values: std::collections::HashMap<ConfigName, ConfigOption>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
values: HashMap::new(),
|
values: std::collections::HashMap::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_path(config_path: PathBuf) -> Result<Self, ConfigReadError> {
|
pub fn from_path(config_path: std::path::PathBuf) -> Result<Self, $crate::ConfigReadError> {
|
||||||
let mut settings = config_path.clone();
|
let mut settings = config_path.clone();
|
||||||
settings.push("config");
|
settings.push("config");
|
||||||
|
|
||||||
match File::open(settings) {
|
match std::fs::File::open(settings) {
|
||||||
Ok(mut file) => {
|
Ok(mut file) => {
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
file.read_to_string(&mut buf)
|
std::io::Read::read_to_string(&mut file, &mut buf)
|
||||||
.map_err(|err| ConfigReadError::CannotRead(err))?;
|
.map_err(|err| $crate::ConfigReadError::CannotRead(err))?;
|
||||||
let values = serde_json::from_str(buf.as_ref())
|
let values = serde_json::from_str(buf.as_ref())
|
||||||
.map_err(|err| ConfigReadError::InvalidJSON(err))?;
|
.map_err(|err| $crate::ConfigReadError::InvalidJSON(err))?;
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
values,
|
values,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Err(io_err) => {
|
Err(io_err) => {
|
||||||
match io_err.kind() {
|
match io_err.kind() {
|
||||||
ErrorKind::NotFound => {
|
std::io::ErrorKind::NotFound => {
|
||||||
/* create the path and an empty file */
|
/* create the path and an empty file */
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
values: HashMap::new(),
|
values: std::collections::HashMap::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
_ => Err(ConfigReadError::CannotOpen(io_err)),
|
_ => Err($crate::ConfigReadError::CannotOpen(io_err)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,8 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = { version = "0.4" }
|
chrono = { version = "0.4" }
|
||||||
|
config = { path = "../../config" }
|
||||||
|
config-derive = { path = "../../config-derive" }
|
||||||
sgf = { path = "../../sgf" }
|
sgf = { path = "../../sgf" }
|
||||||
grid = { version = "0.9" }
|
grid = { version = "0.9" }
|
||||||
serde_json = { version = "1" }
|
serde_json = { version = "1" }
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
types::{AppState, GameState, Player, Rank},
|
types::{AppState, Config, DatabasePath, GameState, Player, Rank},
|
||||||
ui::{home, playing_field, HomeView, PlayingFieldView},
|
ui::{home, playing_field, HomeView, PlayingFieldView},
|
||||||
Config, DatabasePath,
|
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
@ -73,7 +72,7 @@ impl CoreApp {
|
||||||
println!("config_path: {:?}", config_path);
|
println!("config_path: {:?}", config_path);
|
||||||
let config = Config::from_path(config_path).expect("configuration to open");
|
let config = Config::from_path(config_path).expect("configuration to open");
|
||||||
|
|
||||||
let db_path: DatabasePath = config.get();
|
let db_path: DatabasePath = config.get().unwrap();
|
||||||
let state = Arc::new(RwLock::new(AppState::new(db_path)));
|
let state = Arc::new(RwLock::new(AppState::new(db_path)));
|
||||||
|
|
||||||
println!("config: {:?}", config);
|
println!("config: {:?}", config);
|
||||||
|
|
|
@ -1,198 +0,0 @@
|
||||||
use crate::types::Player;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use std::{
|
|
||||||
collections::HashMap,
|
|
||||||
fs::File,
|
|
||||||
io::{ErrorKind, Read},
|
|
||||||
path::PathBuf,
|
|
||||||
};
|
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
/*
|
|
||||||
pub trait ConfigOption {
|
|
||||||
type Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct DatabasePath(PathBuf);
|
|
||||||
|
|
||||||
impl ConfigOption for DatabasePath {
|
|
||||||
type Value = PathBuf;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ConfigOption for Player {
|
|
||||||
type Value = Player;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Config {
|
|
||||||
// fn set_option(option: ConfigOption);
|
|
||||||
fn get_option<N, C: ConfigOption>(name: Name) -> C<Name = Name>
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
|
||||||
enum OptionNames {
|
|
||||||
DatabasePath,
|
|
||||||
Me,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
||||||
#[serde(untagged)]
|
|
||||||
pub enum ConfigOption {
|
|
||||||
DatabasePath(DatabasePath),
|
|
||||||
Me(Me),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
|
||||||
pub enum ConfigReadError {
|
|
||||||
#[error("Cannot read the configuration file: {0}")]
|
|
||||||
CannotRead(std::io::Error),
|
|
||||||
#[error("Cannot open the configuration file for reading: {0}")]
|
|
||||||
CannotOpen(std::io::Error),
|
|
||||||
#[error("Invalid json data found in the configurationfile: {0}")]
|
|
||||||
InvalidJSON(serde_json::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct Config {
|
|
||||||
config_path: PathBuf,
|
|
||||||
values: HashMap<OptionNames, ConfigOption>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Config {
|
|
||||||
pub fn new(config_path: PathBuf) -> Self {
|
|
||||||
Self {
|
|
||||||
config_path,
|
|
||||||
values: HashMap::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_path(config_path: PathBuf) -> Result<Self, ConfigReadError> {
|
|
||||||
let mut settings = config_path.clone();
|
|
||||||
settings.push("config");
|
|
||||||
|
|
||||||
match File::open(settings) {
|
|
||||||
Ok(mut file) => {
|
|
||||||
let mut buf = String::new();
|
|
||||||
file.read_to_string(&mut buf)
|
|
||||||
.map_err(|err| ConfigReadError::CannotRead(err))?;
|
|
||||||
let values = serde_json::from_str(buf.as_ref())
|
|
||||||
.map_err(|err| ConfigReadError::InvalidJSON(err))?;
|
|
||||||
Ok(Self {
|
|
||||||
config_path,
|
|
||||||
values,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Err(io_err) => {
|
|
||||||
match io_err.kind() {
|
|
||||||
ErrorKind::NotFound => {
|
|
||||||
/* create the path and an empty file */
|
|
||||||
Ok(Self {
|
|
||||||
config_path,
|
|
||||||
values: HashMap::new(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
_ => Err(ConfigReadError::CannotOpen(io_err)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set(&mut self, val: ConfigOption) {
|
|
||||||
let _ = match val {
|
|
||||||
ConfigOption::DatabasePath(_) => self.values.insert(OptionNames::DatabasePath, val),
|
|
||||||
ConfigOption::Me(_) => self.values.insert(OptionNames::Me, val),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get<'a, T>(&'a self) -> T
|
|
||||||
where
|
|
||||||
T: From<&'a Self>,
|
|
||||||
{
|
|
||||||
self.into()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
||||||
pub struct DatabasePath(PathBuf);
|
|
||||||
|
|
||||||
impl std::ops::Deref for DatabasePath {
|
|
||||||
type Target = PathBuf;
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<&Config> for DatabasePath {
|
|
||||||
fn from(config: &Config) -> Self {
|
|
||||||
match config.values.get(&OptionNames::DatabasePath) {
|
|
||||||
Some(ConfigOption::DatabasePath(path)) => path.clone(),
|
|
||||||
_ => DatabasePath(config.config_path.clone()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
||||||
pub struct Me(Player);
|
|
||||||
|
|
||||||
impl From<&Config> for Option<Me> {
|
|
||||||
fn from(config: &Config) -> Self {
|
|
||||||
config
|
|
||||||
.values
|
|
||||||
.get(&OptionNames::Me)
|
|
||||||
.and_then(|val| match val {
|
|
||||||
ConfigOption::Me(me) => Some(me.clone()),
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::ops::Deref for Me {
|
|
||||||
type Target = Player;
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::*;
|
|
||||||
use crate::types::Rank;
|
|
||||||
use cool_asserts::assert_matches;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_can_set_and_get_options() {
|
|
||||||
let mut config = Config::new(PathBuf::from("."));
|
|
||||||
config.set(ConfigOption::DatabasePath(DatabasePath(PathBuf::from(
|
|
||||||
"fixtures/five_games",
|
|
||||||
))));
|
|
||||||
config.set(ConfigOption::Me(Me(Player {
|
|
||||||
name: "Savanni".to_owned(),
|
|
||||||
rank: Some(Rank::Kyu(10)),
|
|
||||||
})));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_can_serialize_and_deserialize() {
|
|
||||||
let mut config = Config::new(PathBuf::from("."));
|
|
||||||
config.set(ConfigOption::DatabasePath(DatabasePath(PathBuf::from(
|
|
||||||
"fixtures/five_games",
|
|
||||||
))));
|
|
||||||
config.set(ConfigOption::Me(Me(Player {
|
|
||||||
name: "Savanni".to_owned(),
|
|
||||||
rank: Some(Rank::Kyu(10)),
|
|
||||||
})));
|
|
||||||
let s = serde_json::to_string(&config.values).unwrap();
|
|
||||||
println!("{}", s);
|
|
||||||
let values: HashMap<OptionNames, ConfigOption> = serde_json::from_str(s.as_ref()).unwrap();
|
|
||||||
println!("options: {:?}", values);
|
|
||||||
|
|
||||||
assert_matches!(values.get(&OptionNames::DatabasePath),
|
|
||||||
Some(ConfigOption::DatabasePath(db_path)) =>
|
|
||||||
assert_eq!(*db_path, config.get())
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_matches!(values.get(&OptionNames::Me), Some(ConfigOption::Me(val)) =>
|
|
||||||
assert_eq!(Some(val.clone()), config.get())
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
#[macro_use]
|
||||||
|
extern crate config_derive;
|
||||||
|
|
||||||
mod api;
|
mod api;
|
||||||
pub use api::{
|
pub use api::{
|
||||||
CoreApp, CoreRequest, CoreResponse, CreateGameRequest, HotseatPlayerRequest, PlayerInfoRequest,
|
CoreApp, CoreRequest, CoreResponse, CreateGameRequest, HotseatPlayerRequest, PlayerInfoRequest,
|
||||||
|
@ -6,8 +9,10 @@ pub use api::{
|
||||||
mod board;
|
mod board;
|
||||||
pub use board::*;
|
pub use board::*;
|
||||||
|
|
||||||
|
/*
|
||||||
mod config;
|
mod config;
|
||||||
pub use config::*;
|
pub use config::*;
|
||||||
|
*/
|
||||||
|
|
||||||
mod database;
|
mod database;
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,40 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
api::PlayStoneRequest,
|
api::PlayStoneRequest,
|
||||||
board::{Board, Coordinate},
|
board::{Board, Coordinate},
|
||||||
config::DatabasePath,
|
|
||||||
database::Database,
|
database::Database,
|
||||||
};
|
};
|
||||||
|
use config::define_config;
|
||||||
|
use config_derive::ConfigOption;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{path::PathBuf, time::Duration};
|
use std::{path::PathBuf, time::Duration};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use typeshare::typeshare;
|
use typeshare::typeshare;
|
||||||
|
|
||||||
|
define_config! {
|
||||||
|
DatabasePath(DatabasePath),
|
||||||
|
Me(Me),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, ConfigOption)]
|
||||||
|
pub struct DatabasePath(PathBuf);
|
||||||
|
|
||||||
|
impl std::ops::Deref for DatabasePath {
|
||||||
|
type Target = PathBuf;
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, ConfigOption)]
|
||||||
|
pub struct Me(Player);
|
||||||
|
|
||||||
|
impl std::ops::Deref for Me {
|
||||||
|
type Target = Player;
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Error)]
|
#[derive(Debug, PartialEq, Error)]
|
||||||
pub enum BoardError {
|
pub enum BoardError {
|
||||||
#[error("Position is invalid")]
|
#[error("Position is invalid")]
|
||||||
|
|
Loading…
Reference in New Issue