diff --git a/src/config/mod.rs b/src/config/mod.rs index 407f8dc..5057e16 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -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{ +fn handle_quest_entry(quest_entry: DirEntry) -> Result{ 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) diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..501d6c4 --- /dev/null +++ b/src/error.rs @@ -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}") + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 594f090..90670d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,5 +4,6 @@ pub mod account; pub mod config; +pub mod error; pub mod map; pub mod quest; diff --git a/src/quest/mod.rs b/src/quest/mod.rs index ccbdbbb..6cbaf7d 100644 --- a/src/quest/mod.rs +++ b/src/quest/mod.rs @@ -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 { + pub fn load(path: PathBuf) -> Result { match std::fs::read_to_string(path) { Ok(string) => { match toml::from_str::(&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(()) diff --git a/tests/io.rs b/tests/io.rs index fc3def8..90068f5 100644 --- a/tests/io.rs +++ b/tests/io.rs @@ -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();