refactor!: moved quest::error::QuestError to crate::error::Error

This commit is contained in:
Alexey 2025-12-01 17:15:08 +03:00
commit 1dc7d94785
5 changed files with 49 additions and 22 deletions

View file

@ -1,11 +1,9 @@
//! Text-based quests and user solutions for them
pub mod error;
use std::{fs, io::Write, path::PathBuf};
use serde::{ Serialize, Deserialize };
use error::QuestError;
use crate::error::Error;
use toml::value::Date;
/// Difficulty of the quest
@ -92,12 +90,12 @@ impl Quest {
///
/// # Examples
/// ```rust
/// use squad_quest::quest::{Quest,error::QuestError};
/// use squad_quest::{error::Error,quest::Quest};
/// # fn main() {
/// # let _ = wrapper();
/// # }
///
/// # fn wrapper() -> Result<(), QuestError> {
/// # fn wrapper() -> Result<(), Error> {
/// let path = "quests/0.toml".into();
///
/// let quest = Quest::load(path)?;
@ -105,15 +103,15 @@ impl Quest {
/// # Ok(())
/// # }
/// ```
pub fn load(path: PathBuf) -> Result<Self, QuestError> {
pub fn load(path: PathBuf) -> Result<Self, Error> {
match std::fs::read_to_string(path) {
Ok(string) => {
match toml::from_str::<Quest>(&string) {
Ok(quest) => Ok(quest),
Err(error) => Err(QuestError::TomlDeserializeError(error))
Err(error) => Err(Error::TomlDeserializeError(error))
}
},
Err(error) => Err(QuestError::IoError(error))
Err(error) => Err(Error::IoError(error))
}
}
@ -122,7 +120,7 @@ impl Quest {
///
/// # Examples
/// ```rust
/// use squad_quest::quest::{Quest,error::QuestError};
/// use squad_quest::{error::Error,quest::Quest};
///
/// let path = "quests/0.toml".into();
///
@ -130,11 +128,11 @@ impl Quest {
/// // handle the error
/// }
/// ```
pub fn delete(path: PathBuf) -> Result<(), QuestError> {
pub fn delete(path: PathBuf) -> Result<(), Error> {
match Quest::load(path.clone()) {
Ok(_) => {
if let Err(error) = fs::remove_file(path) {
return Err(QuestError::IoError(error));
return Err(Error::IoError(error));
}
Ok(())
@ -150,7 +148,7 @@ impl Quest {
/// # Examples
/// ```rust
/// # fn main() {
/// use squad_quest::quest::{Quest,error::QuestError};
/// use squad_quest::{error::Error,quest::Quest};
/// use std::path::PathBuf;
///
/// let quest = Quest::default();
@ -165,7 +163,7 @@ impl Quest {
/// # let _ = Quest::delete(path2.with_file_name(filename));
/// # }
/// ```
pub fn save(&self, path: PathBuf) -> Result<(), QuestError> {
pub fn save(&self, path: PathBuf) -> Result<(), Error> {
let filename = format!("{}.toml", self.id);
let mut full_path = path;
full_path.push(filename);
@ -173,19 +171,19 @@ impl Quest {
let str = match toml::to_string_pretty(&self) {
Ok(string) => string,
Err(error) => {
return Err(QuestError::TomlSerializeError(error));
return Err(Error::TomlSerializeError(error));
}
};
let mut file = match fs::File::create(full_path) {
Ok(f) => f,
Err(error) => {
return Err(QuestError::IoError(error));
return Err(Error::IoError(error));
}
};
if let Err(error) = file.write_all(str.as_bytes()) {
return Err(QuestError::IoError(error));
return Err(Error::IoError(error));
}
Ok(())