Skip to content

Commit

Permalink
adding setting tarit
Browse files Browse the repository at this point in the history
  • Loading branch information
HadziqM committed Nov 9, 2024
1 parent db17ef4 commit ddf499e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
36 changes: 36 additions & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::fmt::Debug;

use lazy_static::lazy_static;
use log::{error, warn};
use sysdir::Sysdir;
pub mod database;
pub mod item_code;
Expand All @@ -9,6 +12,39 @@ lazy_static! {
pub static ref SYSDIR: Sysdir = Sysdir::custom_name("RustDiscordBot");
}

pub trait MyResult<T, E> {
fn log(self) -> Self;
fn log_warn(self) -> Self;
fn unwrap_log(self) -> T;
}

impl<T, E> MyResult<T, E> for Result<T, E>
where
E: Debug,
{
fn log(self) -> Self {
if let Err(e) = &self {
error!("Getting error: {:#?}", e);
}
self
}
fn log_warn(self) -> Self {
if let Err(e) = &self {
warn!("Getting error: {:#?}", e);
}
self
}
fn unwrap_log(self) -> T {
match self {
Ok(v) => v,
Err(e) => {
error!("Getting error: {:#?}", e);
panic!();
}
}
}
}

pub fn add(left: u64, right: u64) -> u64 {
left + right
}
Expand Down
18 changes: 15 additions & 3 deletions common/src/setting.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#![allow(dead_code)]

use crate::MyResult;
use crate::{item_code::ItemCode, SYSDIR};
use log::debug;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use sysdir::Sysdir;

trait JsonSetting: Serialize + DeserializeOwned {
pub trait JsonSetting: Serialize + DeserializeOwned {
fn open(ty: SettingList) -> Result<Self, Box<dyn std::error::Error>> {
debug!("loading setting file {}", ty.path());
Ok(serde_json::from_slice(&std::fs::read(ty.path())?)?)
}
}
Expand All @@ -26,8 +29,14 @@ pub struct SettingAll {
}

impl SettingAll {
fn load() -> Self {
todo!()
pub fn load_all() -> Self {
Self {
main: SettingList::Main.load().unwrap_log(),
market: SettingList::Market.load().unwrap_log(),
gacha: SettingList::Gacha.load().unwrap_log(),
savefile: SettingList::SaveFile.load().unwrap_log(),
discord: SettingList::Discord.load().unwrap_log(),
}
}
}

Expand All @@ -50,6 +59,9 @@ impl SettingList {
Self::Market => SYSDIR.config_dir("market.json"),
}
}
pub fn load<T: JsonSetting>(self) -> Result<T, Box<dyn std::error::Error>> {
T::open(self)
}
}

#[derive(Serialize, Deserialize, Clone)]
Expand Down

0 comments on commit ddf499e

Please sign in to comment.