Start setting up app configuration

This commit is contained in:
Savanni D'Gerinel 2023-07-05 21:29:36 -04:00
parent b15658dd0c
commit 7e85b9322f
4 changed files with 105 additions and 2 deletions

View File

@ -1,6 +1,7 @@
use crate::{ use crate::{
types::{AppState, GameState, Player, Rank}, types::{AppState, GameState, Player, Rank},
ui::{new_game, playing_field, NewGameView, PlayingFieldView}, ui::{new_game, playing_field, NewGameView, PlayingFieldView},
Config,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
@ -64,14 +65,22 @@ pub enum CoreResponse {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct CoreApp { pub struct CoreApp {
config: Config,
state: Arc<RwLock<AppState>>, state: Arc<RwLock<AppState>>,
} }
impl CoreApp { impl CoreApp {
pub fn new() -> Self { pub fn new() -> Self {
/* Candidate for dependency injection from the UI */
let user_home = std::env::var("HOME").expect("the user's home directory isn't set");
let mut config_path = std::path::PathBuf::from(user_home);
config_path.push(".config");
config_path.push("kifu");
let config = Config::new(config_path);
let state = Arc::new(RwLock::new(AppState::new())); let state = Arc::new(RwLock::new(AppState::new()));
Self { state } Self { config, state }
} }
pub async fn dispatch(&self, request: CoreRequest) -> CoreResponse { pub async fn dispatch(&self, request: CoreRequest) -> CoreResponse {

91
kifu/core/src/config.rs Normal file
View File

@ -0,0 +1,91 @@
use crate::types::Player;
use std::{collections::HashMap, path::PathBuf};
/*
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)]
enum OptionNames {
DatabasePath,
Me,
}
#[derive(Clone, Debug)]
pub enum ConfigOption {
DatabasePath(DatabasePath),
Me(Me),
}
#[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(),
}
}
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),
};
}
fn get<'a, T>(&'a self) -> T
where
T: From<&'a Self>,
{
self.into()
}
}
#[derive(Clone, Debug)]
pub struct DatabasePath(PathBuf);
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)]
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,
})
}
}

View File

@ -9,3 +9,6 @@ pub mod ui;
mod board; mod board;
pub use board::*; pub use board::*;
mod config;
pub use config::*;

View File

@ -91,7 +91,7 @@ impl From<Rank> for String {
} }
} }
#[derive(Debug)] #[derive(Clone, Debug)]
pub struct Player { pub struct Player {
pub name: String, pub name: String,
pub rank: Option<Rank>, pub rank: Option<Rank>,