object_cache/
cache_error.rs

1use std::fmt::{Display, Formatter};
2
3use serde::Serialize;
4
5#[derive(Debug, Serialize)]
6pub struct CacheError {
7    message: String,
8}
9
10impl CacheError {
11    pub fn new(message: &str) -> Self {
12        Self {
13            message: message.to_owned()
14        }
15    }
16}
17
18impl Display for CacheError {
19    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
20        write!(f, "** Bang at\n     {}", &self.message)
21    }
22}
23
24pub trait MapError<T> {
25    fn map_to_cache_error(self, message: &str) -> Result<T, CacheError>;
26}
27
28impl<T, E: Display> MapError<T> for Result<T, E> {
29    fn map_to_cache_error(self, message: &str) -> Result<T, CacheError> {
30        match self {
31            Ok(data) => {
32                Ok(data)
33            }
34            Err(e) => {
35                Err(CacheError::new(&format!("{}\n      {}", message, e)))
36            }
37        }
38    }
39}