Inject the config path from the UI, try to read the file
This commit is contained in:
parent
7e85b9322f
commit
bb74f3cb49
|
@ -163,6 +163,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"grid",
|
"grid",
|
||||||
"serde",
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"typeshare",
|
"typeshare",
|
||||||
]
|
]
|
||||||
|
|
|
@ -8,5 +8,6 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
grid = { version = "0.9" }
|
grid = { version = "0.9" }
|
||||||
serde = { version = "1", features = [ "derive" ] }
|
serde = { version = "1", features = [ "derive" ] }
|
||||||
|
serde_json = { version = "1" }
|
||||||
thiserror = { version = "1" }
|
thiserror = { version = "1" }
|
||||||
typeshare = { version = "1" }
|
typeshare = { version = "1" }
|
||||||
|
|
|
@ -70,14 +70,8 @@ pub struct CoreApp {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CoreApp {
|
impl CoreApp {
|
||||||
pub fn new() -> Self {
|
pub fn new(config_path: std::path::PathBuf) -> Self {
|
||||||
/* Candidate for dependency injection from the UI */
|
let config = Config::from_path(config_path).expect("configuration to open");
|
||||||
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 { config, state }
|
Self { config, state }
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
use crate::types::Player;
|
use crate::types::Player;
|
||||||
use std::{collections::HashMap, path::PathBuf};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
fs::File,
|
||||||
|
io::{ErrorKind, Read},
|
||||||
|
path::PathBuf,
|
||||||
|
};
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
pub trait ConfigOption {
|
pub trait ConfigOption {
|
||||||
|
@ -22,18 +29,28 @@ pub trait Config {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
enum OptionNames {
|
enum OptionNames {
|
||||||
DatabasePath,
|
DatabasePath,
|
||||||
Me,
|
Me,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
pub enum ConfigOption {
|
pub enum ConfigOption {
|
||||||
DatabasePath(DatabasePath),
|
DatabasePath(DatabasePath),
|
||||||
Me(Me),
|
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)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
config_path: PathBuf,
|
config_path: PathBuf,
|
||||||
|
@ -48,6 +65,37 @@ impl Config {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
fn set(&mut self, val: ConfigOption) {
|
||||||
let _ = match val {
|
let _ = match val {
|
||||||
ConfigOption::DatabasePath(_) => self.values.insert(OptionNames::DatabasePath, val),
|
ConfigOption::DatabasePath(_) => self.values.insert(OptionNames::DatabasePath, val),
|
||||||
|
@ -63,7 +111,7 @@ impl Config {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
pub struct DatabasePath(PathBuf);
|
pub struct DatabasePath(PathBuf);
|
||||||
|
|
||||||
impl From<&Config> for DatabasePath {
|
impl From<&Config> for DatabasePath {
|
||||||
|
@ -75,7 +123,7 @@ impl From<&Config> for DatabasePath {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
pub struct Me(Player);
|
pub struct Me(Player);
|
||||||
|
|
||||||
impl From<&Config> for Option<Me> {
|
impl From<&Config> for Option<Me> {
|
||||||
|
|
|
@ -91,7 +91,7 @@ impl From<Rank> for String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub rank: Option<Rank>,
|
pub rank: Option<Rank>,
|
||||||
|
|
|
@ -787,6 +787,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"grid",
|
"grid",
|
||||||
"serde",
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"typeshare",
|
"typeshare",
|
||||||
]
|
]
|
||||||
|
|
|
@ -44,7 +44,12 @@ fn main() {
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let core = CoreApp::new();
|
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_handle = runtime.spawn({
|
let core_handle = runtime.spawn({
|
||||||
let core = core.clone();
|
let core = core.clone();
|
||||||
|
|
Loading…
Reference in New Issue