good_ormning_runtime/
lib.rs

1use std::fmt::Display;
2
3#[cfg(feature = "pg")]
4pub mod pg;
5#[cfg(feature = "sqlite")]
6pub mod sqlite;
7
8#[derive(Debug)]
9pub struct GoodError(pub String);
10
11impl std::fmt::Display for GoodError {
12    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13        self.0.fmt(f)
14    }
15}
16
17impl std::error::Error for GoodError { }
18
19pub trait ToGoodError<T> {
20    fn to_good_error<F: FnOnce() -> String>(self, context: F) -> Result<T, GoodError>;
21    fn to_good_error_query(self, query: &str) -> Result<T, GoodError>;
22}
23
24impl<T, E: Display> ToGoodError<T> for Result<T, E> {
25    fn to_good_error<F: FnOnce() -> String>(self, context: F) -> Result<T, GoodError> {
26        match self {
27            Ok(x) => Ok(x),
28            Err(e) => Err(GoodError(format!("{}: {}", context(), e))),
29        }
30    }
31
32    fn to_good_error_query(self, query: &str) -> Result<T, GoodError> {
33        return self.to_good_error(|| format!("In query [{}]", query));
34    }
35}