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
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// FIXME: Make it possible to convert to BError from
// any other StdError type, instead of using the e()
// extension method.
//
// This can only be done by _not_ implementing StdError,
// but using a new trait like Fail.

use std::error::Error as StdError;
use std::result::Result as StdResult;
use std::fmt::{self, Display, Debug, Formatter};
use std::process;

pub type BResult<T> = StdResult<T, BError>;

pub struct BError(Box<BErrorInner>);

// FIXME: Figure out how to use unsized types for unboxed kind
// FIXME: Limit kind to all types that implement Display
// _except_ for str / String
// FIXME: Would like to make it possible to require
// context be added to errors before returning.
struct BErrorInner {
    kind: Box<dyn Display + Send + Sync + 'static>,
    source: Option<Box<dyn StdError + Send + Sync + 'static>>,
}

impl BError {
    pub fn new<K>(kind: K) -> BError
    where K: Display + Send + Sync + 'static
    {
        BError(Box::new(BErrorInner {
            kind: Box::new(kind),
            source: None,
        }))
    }

    pub fn from_source<E, K>(source: E, kind: K) -> BError
    where E: StdError + Send + Sync + 'static,
          K: Display + Send + Sync + 'static
    {
        BError(Box::new(BErrorInner {
            kind: Box::new(kind),
            source: Some(Box::new(source)),
        }))
    }
}

impl StdError for BError {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self.0.source {
            Some(ref e) => Some(e.as_ref()),
            None => None,
        }
    }
}

impl Display for BError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        self.0.kind.fmt(f)
    }
}

impl Debug for BError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.debug_struct("BError")
            .field("kind", &format!("{}", &self.0.kind))
            .field("source", &self.0.source)
            .finish()
    }
}

pub trait StdResultExt<T>: Sized {
    fn ec<K>(self, kind: K) -> StdResult<T, BError>
    where K: Display + Send + Sync + 'static;

    fn e(self) -> StdResult<T, BError> {
        self.ec("error")
    }
}

impl<T, E> StdResultExt<T> for StdResult<T, E>
where E: StdError + Send + Sync + 'static
{
    fn ec<K>(self, kind: K) -> StdResult<T, BError>
    where K: Display + Send + Sync + 'static
    {
        match self {
            Ok(v) => Ok(v),
            Err(e) => Err(BError::from_source(e, kind))
        }
    }
}

pub fn main(run: impl FnOnce() -> BResult<()>) {
    if let Err(e) = run() {
        println!("error: {}", e);
        let mut e = e.source();
        while let Some(cause) = e {
            println!("  caused by: {}", cause);
            e = cause.source();
        }
        process::exit(1);
    }
}

#[test]
fn test() {
    #[derive(Debug)]
    pub struct TestError;

    impl StdError for TestError { }

    impl Display for TestError {
        fn fmt(&self, _f: &mut Formatter) -> fmt::Result {
            panic!()
        }
    }

    fn ret_s_t() -> StdResult<(), TestError> { panic!() }
    fn ret_s_b() -> StdResult<(), BError> { panic!() }

    fn try_s_t() -> StdResult<(), TestError> {
        //ret_s_t().c("test")?;
        //ret_s_t().c("test")?;

        Ok(())
    }

    fn try_s_b() -> StdResult<(), BError> {
        ret_s_t().c("test")?;
        ret_s_b().c("test")?;

        Ok(())
    }

}