1//! Consolidates rusqlite and validation errors under a common error type
23extern crate rusqlite;
45use std::fmt;
67#[derive(Debug)]
8pub enum PersistenceError {
9// An invalid asset was returned from the database
10AssetError(String),
11// The player has already reached the max character limit
12CharacterLimitReached,
13// An error occurred while establish a db connection
14DatabaseConnectionError(rusqlite::Error),
15// An error occurred when performing a database action
16DatabaseError(rusqlite::Error),
17// Unable to load body or stats for a character
18CharacterDataError,
19 SerializationError(serde_json::Error),
20 ConversionError(String),
21 OtherError(String),
22}
2324impl fmt::Display for PersistenceError {
25fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26write!(f, "{}", match self {
27Self::AssetError(error) => error.to_string(),
28Self::CharacterLimitReached => String::from("Character limit exceeded"),
29Self::DatabaseError(error) => error.to_string(),
30Self::DatabaseConnectionError(error) => error.to_string(),
31Self::CharacterDataError => String::from("Error while loading character data"),
32Self::SerializationError(error) => error.to_string(),
33Self::ConversionError(error) => error.to_string(),
34Self::OtherError(error) => error.to_string(),
35 })
36 }
37}
3839impl From<rusqlite::Error> for PersistenceError {
40fn from(error: rusqlite::Error) -> PersistenceError { PersistenceError::DatabaseError(error) }
41}
4243impl From<serde_json::Error> for PersistenceError {
44fn from(error: serde_json::Error) -> PersistenceError {
45 PersistenceError::SerializationError(error)
46 }
47}