Compare commits
No commits in common. "bb74f3cb499a28ab59b592ebd795b119c70ba259" and "b15658dd0c7dbb116f418fe15feb8e252fb4c732" have entirely different histories.
bb74f3cb49
...
b15658dd0c
|
@ -163,7 +163,6 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"grid",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror",
|
||||
"typeshare",
|
||||
]
|
||||
|
|
|
@ -8,6 +8,5 @@ edition = "2021"
|
|||
[dependencies]
|
||||
grid = { version = "0.9" }
|
||||
serde = { version = "1", features = [ "derive" ] }
|
||||
serde_json = { version = "1" }
|
||||
thiserror = { version = "1" }
|
||||
typeshare = { version = "1" }
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use crate::{
|
||||
types::{AppState, GameState, Player, Rank},
|
||||
ui::{new_game, playing_field, NewGameView, PlayingFieldView},
|
||||
Config,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
@ -65,16 +64,14 @@ pub enum CoreResponse {
|
|||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CoreApp {
|
||||
config: Config,
|
||||
state: Arc<RwLock<AppState>>,
|
||||
}
|
||||
|
||||
impl CoreApp {
|
||||
pub fn new(config_path: std::path::PathBuf) -> Self {
|
||||
let config = Config::from_path(config_path).expect("configuration to open");
|
||||
pub fn new() -> Self {
|
||||
let state = Arc::new(RwLock::new(AppState::new()));
|
||||
|
||||
Self { config, state }
|
||||
Self { state }
|
||||
}
|
||||
|
||||
pub async fn dispatch(&self, request: CoreRequest) -> CoreResponse {
|
||||
|
|
|
@ -1,139 +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)]
|
||||
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)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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, Serialize, Deserialize)]
|
||||
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, 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,
|
||||
})
|
||||
}
|
||||
}
|
|
@ -9,6 +9,3 @@ pub mod ui;
|
|||
|
||||
mod board;
|
||||
pub use board::*;
|
||||
|
||||
mod config;
|
||||
pub use config::*;
|
||||
|
|
|
@ -91,7 +91,7 @@ impl From<Rank> for String {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
pub name: String,
|
||||
pub rank: Option<Rank>,
|
||||
|
|
|
@ -787,7 +787,6 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"grid",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror",
|
||||
"typeshare",
|
||||
]
|
||||
|
|
|
@ -44,12 +44,7 @@ fn main() {
|
|||
.unwrap(),
|
||||
);
|
||||
|
||||
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 core = CoreApp::new(config_path);
|
||||
let core = CoreApp::new();
|
||||
|
||||
let core_handle = runtime.spawn({
|
||||
let core = core.clone();
|
||||
|
|
Loading…
Reference in New Issue