refactor!: moved quest::error::QuestError to crate::error::Error
This commit is contained in:
parent
a0bec4003c
commit
1dc7d94785
5 changed files with 49 additions and 22 deletions
|
|
@ -3,7 +3,7 @@
|
||||||
use std::{fs::{self, DirEntry},path::{Path, PathBuf}};
|
use std::{fs::{self, DirEntry},path::{Path, PathBuf}};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
use crate::quest::{Quest,error::QuestError};
|
use crate::{error::Error,quest::Quest};
|
||||||
|
|
||||||
/// Struct for containing paths to other (de-)serializable things
|
/// Struct for containing paths to other (de-)serializable things
|
||||||
#[derive(Deserialize)]
|
#[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();
|
let filetype = quest_entry.file_type();
|
||||||
if let Err(error) = filetype {
|
if let Err(error) = filetype {
|
||||||
return Err(QuestError::IoError(error));
|
return Err(Error::IoError(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
let path = quest_entry.path();
|
let path = quest_entry.path();
|
||||||
|
|
||||||
let filetype = filetype.unwrap();
|
let filetype = filetype.unwrap();
|
||||||
if !filetype.is_file() {
|
if !filetype.is_file() {
|
||||||
return Err(QuestError::IsNotAFile(path));
|
return Err(Error::IsNotAFile(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
Quest::load(path)
|
Quest::load(path)
|
||||||
|
|
|
||||||
28
src/error.rs
Normal file
28
src/error.rs
Normal 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}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,5 +4,6 @@
|
||||||
|
|
||||||
pub mod account;
|
pub mod account;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
|
pub mod error;
|
||||||
pub mod map;
|
pub mod map;
|
||||||
pub mod quest;
|
pub mod quest;
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,9 @@
|
||||||
//! Text-based quests and user solutions for them
|
//! Text-based quests and user solutions for them
|
||||||
|
|
||||||
pub mod error;
|
|
||||||
|
|
||||||
use std::{fs, io::Write, path::PathBuf};
|
use std::{fs, io::Write, path::PathBuf};
|
||||||
|
|
||||||
use serde::{ Serialize, Deserialize };
|
use serde::{ Serialize, Deserialize };
|
||||||
use error::QuestError;
|
use crate::error::Error;
|
||||||
use toml::value::Date;
|
use toml::value::Date;
|
||||||
|
|
||||||
/// Difficulty of the quest
|
/// Difficulty of the quest
|
||||||
|
|
@ -92,12 +90,12 @@ impl Quest {
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use squad_quest::quest::{Quest,error::QuestError};
|
/// use squad_quest::{error::Error,quest::Quest};
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// # let _ = wrapper();
|
/// # let _ = wrapper();
|
||||||
/// # }
|
/// # }
|
||||||
///
|
///
|
||||||
/// # fn wrapper() -> Result<(), QuestError> {
|
/// # fn wrapper() -> Result<(), Error> {
|
||||||
/// let path = "quests/0.toml".into();
|
/// let path = "quests/0.toml".into();
|
||||||
///
|
///
|
||||||
/// let quest = Quest::load(path)?;
|
/// let quest = Quest::load(path)?;
|
||||||
|
|
@ -105,15 +103,15 @@ impl Quest {
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn load(path: PathBuf) -> Result<Self, QuestError> {
|
pub fn load(path: PathBuf) -> Result<Self, Error> {
|
||||||
match std::fs::read_to_string(path) {
|
match std::fs::read_to_string(path) {
|
||||||
Ok(string) => {
|
Ok(string) => {
|
||||||
match toml::from_str::<Quest>(&string) {
|
match toml::from_str::<Quest>(&string) {
|
||||||
Ok(quest) => Ok(quest),
|
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
|
/// # Examples
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use squad_quest::quest::{Quest,error::QuestError};
|
/// use squad_quest::{error::Error,quest::Quest};
|
||||||
///
|
///
|
||||||
/// let path = "quests/0.toml".into();
|
/// let path = "quests/0.toml".into();
|
||||||
///
|
///
|
||||||
|
|
@ -130,11 +128,11 @@ impl Quest {
|
||||||
/// // handle the error
|
/// // handle the error
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn delete(path: PathBuf) -> Result<(), QuestError> {
|
pub fn delete(path: PathBuf) -> Result<(), Error> {
|
||||||
match Quest::load(path.clone()) {
|
match Quest::load(path.clone()) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
if let Err(error) = fs::remove_file(path) {
|
if let Err(error) = fs::remove_file(path) {
|
||||||
return Err(QuestError::IoError(error));
|
return Err(Error::IoError(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -150,7 +148,7 @@ impl Quest {
|
||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// use squad_quest::quest::{Quest,error::QuestError};
|
/// use squad_quest::{error::Error,quest::Quest};
|
||||||
/// use std::path::PathBuf;
|
/// use std::path::PathBuf;
|
||||||
///
|
///
|
||||||
/// let quest = Quest::default();
|
/// let quest = Quest::default();
|
||||||
|
|
@ -165,7 +163,7 @@ impl Quest {
|
||||||
/// # let _ = Quest::delete(path2.with_file_name(filename));
|
/// # 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 filename = format!("{}.toml", self.id);
|
||||||
let mut full_path = path;
|
let mut full_path = path;
|
||||||
full_path.push(filename);
|
full_path.push(filename);
|
||||||
|
|
@ -173,19 +171,19 @@ impl Quest {
|
||||||
let str = match toml::to_string_pretty(&self) {
|
let str = match toml::to_string_pretty(&self) {
|
||||||
Ok(string) => string,
|
Ok(string) => string,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
return Err(QuestError::TomlSerializeError(error));
|
return Err(Error::TomlSerializeError(error));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut file = match fs::File::create(full_path) {
|
let mut file = match fs::File::create(full_path) {
|
||||||
Ok(f) => f,
|
Ok(f) => f,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
return Err(QuestError::IoError(error));
|
return Err(Error::IoError(error));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(error) = file.write_all(str.as_bytes()) {
|
if let Err(error) = file.write_all(str.as_bytes()) {
|
||||||
return Err(QuestError::IoError(error));
|
return Err(Error::IoError(error));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
||||||
|
|
@ -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;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
const CONFIG_PATH: &str = "tests/io/config.toml";
|
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,
|
// and Quest::save can override files,
|
||||||
// so this test covers full quest CRUD
|
// so this test covers full quest CRUD
|
||||||
#[test]
|
#[test]
|
||||||
fn quest_crud() -> Result<(), QuestError> {
|
fn quest_crud() -> Result<(), Error> {
|
||||||
let config = Config::load(CONFIG_PATH.into());
|
let config = Config::load(CONFIG_PATH.into());
|
||||||
|
|
||||||
let mut quests_path = PathBuf::from(CONFIG_PATH).parent().unwrap().to_owned();
|
let mut quests_path = PathBuf::from(CONFIG_PATH).parent().unwrap().to_owned();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue