2023-08-18 02:54:39 +00:00
|
|
|
/*
|
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
fs::File,
|
|
|
|
hash::Hash,
|
|
|
|
io::{ErrorKind, Read},
|
|
|
|
path::PathBuf,
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2023-08-16 04:35:11 +00:00
|
|
|
pub use config_derive::ConfigOption;
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
#[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),
|
|
|
|
}
|
|
|
|
|
2023-08-18 02:54:39 +00:00
|
|
|
#[macro_export]
|
2023-08-16 04:35:11 +00:00
|
|
|
macro_rules! define_config {
|
|
|
|
($($name:ident($struct:ident),)+) => (
|
2023-08-18 02:54:39 +00:00
|
|
|
#[derive(Clone, Debug, Hash, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
2023-08-16 04:35:11 +00:00
|
|
|
pub enum ConfigName {
|
|
|
|
$($name),+
|
|
|
|
}
|
|
|
|
|
2023-08-18 02:54:39 +00:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
2023-08-20 00:46:43 +00:00
|
|
|
#[serde(untagged)]
|
2023-08-16 04:35:11 +00:00
|
|
|
pub enum ConfigOption {
|
|
|
|
$($name($struct)),+
|
|
|
|
}
|
|
|
|
|
2024-03-21 23:20:09 +00:00
|
|
|
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
2023-08-16 04:35:11 +00:00
|
|
|
pub struct Config {
|
2023-08-18 02:54:39 +00:00
|
|
|
values: std::collections::HashMap<ConfigName, ConfigOption>,
|
2023-08-16 04:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2023-08-18 02:54:39 +00:00
|
|
|
values: std::collections::HashMap::new(),
|
2023-08-16 04:35:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 02:54:39 +00:00
|
|
|
pub fn from_path(config_path: std::path::PathBuf) -> Result<Self, $crate::ConfigReadError> {
|
2023-08-16 04:35:11 +00:00
|
|
|
let mut settings = config_path.clone();
|
|
|
|
settings.push("config");
|
|
|
|
|
2023-08-18 02:54:39 +00:00
|
|
|
match std::fs::File::open(settings) {
|
2023-08-16 04:35:11 +00:00
|
|
|
Ok(mut file) => {
|
|
|
|
let mut buf = String::new();
|
2023-08-18 02:54:39 +00:00
|
|
|
std::io::Read::read_to_string(&mut file, &mut buf)
|
|
|
|
.map_err(|err| $crate::ConfigReadError::CannotRead(err))?;
|
2023-08-16 04:35:11 +00:00
|
|
|
let values = serde_json::from_str(buf.as_ref())
|
2023-08-18 02:54:39 +00:00
|
|
|
.map_err(|err| $crate::ConfigReadError::InvalidJSON(err))?;
|
2023-08-16 04:35:11 +00:00
|
|
|
Ok(Self {
|
|
|
|
values,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Err(io_err) => {
|
|
|
|
match io_err.kind() {
|
2023-08-18 02:54:39 +00:00
|
|
|
std::io::ErrorKind::NotFound => {
|
2023-08-16 04:35:11 +00:00
|
|
|
/* create the path and an empty file */
|
|
|
|
Ok(Self {
|
2023-08-18 02:54:39 +00:00
|
|
|
values: std::collections::HashMap::new(),
|
2023-08-16 04:35:11 +00:00
|
|
|
})
|
|
|
|
}
|
2023-08-18 02:54:39 +00:00
|
|
|
_ => Err($crate::ConfigReadError::CannotOpen(io_err)),
|
2023-08-16 04:35:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set(&mut self, val: ConfigOption) {
|
|
|
|
let _ = match val {
|
|
|
|
$(ConfigOption::$struct(_) => self.values.insert(ConfigName::$name, val)),+
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get<'a, T>(&'a self) -> Option<T>
|
|
|
|
where
|
|
|
|
Option<T>: From<&'a Self>,
|
|
|
|
{
|
|
|
|
self.into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use cool_asserts::assert_matches;
|
2023-08-18 03:12:05 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::path::PathBuf;
|
2023-08-16 04:35:11 +00:00
|
|
|
|
|
|
|
define_config! {
|
|
|
|
DatabasePath(DatabasePath),
|
|
|
|
Me(Me),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, ConfigOption)]
|
|
|
|
pub struct DatabasePath(PathBuf);
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
enum Rank {
|
|
|
|
Kyu(i8),
|
|
|
|
Dan(i8),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, ConfigOption)]
|
|
|
|
pub struct Me {
|
|
|
|
name: String,
|
|
|
|
rank: Option<Rank>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn it_can_set_and_get_options() {
|
|
|
|
let mut config: Config = Config::new();
|
|
|
|
config.set(ConfigOption::DatabasePath(DatabasePath(PathBuf::from(
|
|
|
|
"./fixtures/five_games",
|
|
|
|
))));
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Some(DatabasePath(PathBuf::from("./fixtures/five_games"))),
|
|
|
|
config.get()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn it_can_serialize_and_deserialize() {
|
|
|
|
let mut config = Config::new();
|
|
|
|
config.set(ConfigOption::DatabasePath(DatabasePath(PathBuf::from(
|
|
|
|
"fixtures/five_games",
|
|
|
|
))));
|
|
|
|
config.set(ConfigOption::Me(Me {
|
|
|
|
name: "Savanni".to_owned(),
|
|
|
|
rank: Some(Rank::Kyu(10)),
|
|
|
|
}));
|
|
|
|
let s = serde_json::to_string(&config.values).unwrap();
|
|
|
|
println!("{}", s);
|
|
|
|
let values: HashMap<ConfigName, ConfigOption> = serde_json::from_str(s.as_ref()).unwrap();
|
|
|
|
println!("options: {:?}", values);
|
|
|
|
|
|
|
|
assert_matches!(values.get(&ConfigName::DatabasePath),
|
|
|
|
Some(ConfigOption::DatabasePath(ref db_path)) =>
|
|
|
|
assert_eq!(Some(db_path.clone()), config.get())
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_matches!(values.get(&ConfigName::Me), Some(ConfigOption::Me(val)) =>
|
|
|
|
assert_eq!(Some(val.clone()), config.get())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|