feat: Quest loading
- Added config::Config::load_quests() - Added quest::error::QuestError error type - Updated documentation
This commit is contained in:
parent
4e3c137f8b
commit
a5eac1b64f
5 changed files with 130 additions and 10 deletions
|
|
@ -1,6 +1,11 @@
|
|||
//! Module for handling text-based quests and user answers
|
||||
//! Text-based quests and user solutions for them
|
||||
|
||||
pub mod error;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use serde::{ Serialize, Deserialize };
|
||||
use error::QuestError;
|
||||
|
||||
/// Difficulty of the quest
|
||||
#[derive(Serialize, Deserialize)]
|
||||
|
|
@ -60,3 +65,18 @@ pub struct Quest {
|
|||
#[serde(default = "default_answer")]
|
||||
pub answer: String,
|
||||
}
|
||||
|
||||
impl Quest {
|
||||
/// Parse quest TOML or return error
|
||||
pub fn load(path: PathBuf) -> Result<Self, QuestError> {
|
||||
match std::fs::read_to_string(path) {
|
||||
Ok(string) => {
|
||||
match toml::from_str::<Quest>(&string) {
|
||||
Ok(quest) => Ok(quest),
|
||||
Err(error) => Err(QuestError::TomlError(error))
|
||||
}
|
||||
},
|
||||
Err(error) => Err(QuestError::IoError(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue