1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use std::fmt::Display;

#[cfg(feature = "pg")]
pub mod pg;
#[cfg(feature = "sqlite")]
pub mod sqlite;

#[derive(Debug)]
pub struct GoodError(pub String);

impl std::fmt::Display for GoodError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl std::error::Error for GoodError { }

pub trait ToGoodError<T> {
    fn to_good_error<F: FnOnce() -> String>(self, context: F) -> Result<T, GoodError>;
    fn to_good_error_query(self, query: &str) -> Result<T, GoodError>;
}

impl<T, E: Display> ToGoodError<T> for Result<T, E> {
    fn to_good_error<F: FnOnce() -> String>(self, context: F) -> Result<T, GoodError> {
        match self {
            Ok(x) => Ok(x),
            Err(e) => Err(GoodError(format!("{}: {}", context(), e))),
        }
    }

    fn to_good_error_query(self, query: &str) -> Result<T, GoodError> {
        return self.to_good_error(|| format!("In query [{}]", query));
    }
}