feat: Basic structs
- Added Account struct - Added Config struct - Added empty Map struct - Added Quest struct
This commit is contained in:
parent
753d0e18e2
commit
4e3c137f8b
8 changed files with 441 additions and 0 deletions
55
src/config/mod.rs
Normal file
55
src/config/mod.rs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
//! Module for handling configuration
|
||||
|
||||
use std::path::PathBuf;
|
||||
use serde::Deserialize;
|
||||
|
||||
/// Struct for containing paths to other (de-)serializable things
|
||||
#[derive(Deserialize)]
|
||||
pub struct Config {
|
||||
|
||||
/// Path to serialized [quests][`crate::quest::Quest`] folder
|
||||
#[serde(default)]
|
||||
pub quests_path: PathBuf,
|
||||
|
||||
/// Path to serialized [accounts][`crate::account::Account`] folder
|
||||
#[serde(default)]
|
||||
pub accounts_path: PathBuf,
|
||||
|
||||
/// Path to serialized [map][`crate::map::Map`] file
|
||||
#[serde(default)]
|
||||
pub map: PathBuf
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Config {
|
||||
quests_path: "quests".into(),
|
||||
accounts_path: "accounts".into(),
|
||||
map: "map.toml".into()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// Deserialize config from TOML
|
||||
pub fn load(path: PathBuf) -> Self {
|
||||
match std::fs::read_to_string(path) {
|
||||
Ok(string) => {
|
||||
match toml::from_str::<Config>(&string) {
|
||||
Ok(conf) => {
|
||||
println!("Successfully loaded config");
|
||||
conf
|
||||
},
|
||||
Err(error) => {
|
||||
eprintln!("Error on parsing config: {error}");
|
||||
Config::default()
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(error) => {
|
||||
eprintln!("Error on reading config path: {error}");
|
||||
Config::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue