2021-04-13 22:05:47 +00:00
|
|
|
//! Consolidates rusqlite and validation errors under a common error type
|
2020-06-02 08:16:23 +00:00
|
|
|
|
2021-04-13 22:05:47 +00:00
|
|
|
extern crate rusqlite;
|
2020-04-20 08:44:29 +00:00
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
2020-09-17 23:02:14 +00:00
|
|
|
#[derive(Debug)]
|
2021-04-13 22:05:47 +00:00
|
|
|
pub enum PersistenceError {
|
2020-09-17 23:02:14 +00:00
|
|
|
// An invalid asset was returned from the database
|
|
|
|
AssetError(String),
|
2020-05-10 16:28:11 +00:00
|
|
|
// The player has already reached the max character limit
|
2020-04-20 08:44:29 +00:00
|
|
|
CharacterLimitReached,
|
2020-08-25 12:21:25 +00:00
|
|
|
// An error occurred while establish a db connection
|
2021-04-13 22:05:47 +00:00
|
|
|
DatabaseConnectionError(rusqlite::Error),
|
2020-08-25 12:21:25 +00:00
|
|
|
// An error occurred when performing a database action
|
2021-04-13 22:05:47 +00:00
|
|
|
DatabaseError(rusqlite::Error),
|
2020-05-11 10:06:53 +00:00
|
|
|
// Unable to load body or stats for a character
|
|
|
|
CharacterDataError,
|
2020-09-17 23:02:14 +00:00
|
|
|
SerializationError(serde_json::Error),
|
|
|
|
ConversionError(String),
|
|
|
|
OtherError(String),
|
2020-04-20 08:44:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 22:05:47 +00:00
|
|
|
impl fmt::Display for PersistenceError {
|
2020-04-20 08:44:29 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", match self {
|
2020-09-17 23:02:14 +00:00
|
|
|
Self::AssetError(error) => error.to_string(),
|
2020-04-20 08:44:29 +00:00
|
|
|
Self::CharacterLimitReached => String::from("Character limit exceeded"),
|
2020-06-16 01:00:32 +00:00
|
|
|
Self::DatabaseError(error) => error.to_string(),
|
|
|
|
Self::DatabaseConnectionError(error) => error.to_string(),
|
2020-05-11 10:06:53 +00:00
|
|
|
Self::CharacterDataError => String::from("Error while loading character data"),
|
2020-09-17 23:02:14 +00:00
|
|
|
Self::SerializationError(error) => error.to_string(),
|
|
|
|
Self::ConversionError(error) => error.to_string(),
|
|
|
|
Self::OtherError(error) => error.to_string(),
|
2020-04-20 08:44:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-13 22:05:47 +00:00
|
|
|
impl From<rusqlite::Error> for PersistenceError {
|
|
|
|
fn from(error: rusqlite::Error) -> PersistenceError { PersistenceError::DatabaseError(error) }
|
2020-04-20 08:44:29 +00:00
|
|
|
}
|
2020-06-16 01:00:32 +00:00
|
|
|
|
2021-04-13 22:05:47 +00:00
|
|
|
impl From<serde_json::Error> for PersistenceError {
|
|
|
|
fn from(error: serde_json::Error) -> PersistenceError {
|
|
|
|
PersistenceError::SerializationError(error)
|
2020-06-16 01:00:32 +00:00
|
|
|
}
|
|
|
|
}
|