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

@ -3,7 +3,7 @@
use std::{fs::{self, DirEntry},path::{Path, PathBuf}};
use serde::Deserialize;
use crate::quest::{Quest,error::QuestError};
use crate::{error::Error,quest::Quest};
/// Struct for containing paths to other (de-)serializable things
#[derive(Deserialize)]
@ -34,17 +34,17 @@ impl Default for Config {
}
}
fn handle_quest_entry(quest_entry: DirEntry) -> Result<Quest, QuestError>{
fn handle_quest_entry(quest_entry: DirEntry) -> Result<Quest, Error>{
let filetype = quest_entry.file_type();
if let Err(error) = filetype {
return Err(QuestError::IoError(error));
return Err(Error::IoError(error));
}
let path = quest_entry.path();
let filetype = filetype.unwrap();
if !filetype.is_file() {
return Err(QuestError::IsNotAFile(path));
return Err(Error::IsNotAFile(path));
}
Quest::load(path)

28
src/error.rs Normal file
View file

@ -0,0 +1,28 @@
//! Module for handling crate errors
use std::{fmt, path::PathBuf};
/// Error struct
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// Given path is not a file
IsNotAFile(PathBuf),
/// std::io::Error happenned when loading
IoError(std::io::Error),
/// toml::ser::Error happened when loading
TomlSerializeError(toml::ser::Error),
/// toml::de::Error happened when loading
TomlDeserializeError(toml::de::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::IsNotAFile(path) => write!(f, "{:?} is not a file", path),
Error::IoError(error) => write!(f, "io error: {error}"),
Error::TomlSerializeError(error) => write!(f, "serialize error: {error}"),
Error::TomlDeserializeError(error) => write!(f, "parse error: {error}")
}
}
}

View file

@ -4,5 +4,6 @@
pub mod account;
pub mod config;
pub mod error;
pub mod map;
pub mod quest;

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(())

View file

@ -1,4 +1,4 @@
use squad_quest::{config::Config,quest::{error::{QuestError}, Quest}};
use squad_quest::{config::Config,error::Error,quest::Quest};
use std::path::PathBuf;
const CONFIG_PATH: &str = "tests/io/config.toml";
@ -7,7 +7,7 @@ const CONFIG_PATH: &str = "tests/io/config.toml";
// and Quest::save can override files,
// so this test covers full quest CRUD
#[test]
fn quest_crud() -> Result<(), QuestError> {
fn quest_crud() -> Result<(), Error> {
let config = Config::load(CONFIG_PATH.into());
let mut quests_path = PathBuf::from(CONFIG_PATH).parent().unwrap().to_owned();