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
//! Defines the error type and its formatting and conversion.

extern crate failure;

use failure::{Context, Fail};
use std::fmt::{self, Display};

/// This error type is extensively used throughout the codebase.
/// Any external errors are converted to this one using `convert()` method
/// from [`failure`](https://docs.rs/failure/0.1.5/failure/) crate.
/// The context is a string for simplicity's sake, since the only thing we
/// care about is printing the error to the user.
#[derive(Debug)]
pub struct Error {
    inner: Context<String>,
}

impl PartialEq for Error {
    fn eq(&self, other: &Self) -> bool {
        format!("{}", self) == format!("{}", other)
    }
}

impl Fail for Error {
    fn cause(&self) -> Option<&dyn Fail> {
        self.inner.cause()
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", &self.inner)?;
        if let Some(cause) = self.cause() {
            for cause in cause.iter_chain() {
                write!(f, ": {}", &cause)?;
            }
        }
        write!(f, "")
    }
}

impl From<&'static str> for Error {
    fn from(msg: &'static str) -> Self {
        Self {
            inner: Context::new(msg.to_string()),
        }
    }
}

impl From<String> for Error {
    fn from(msg: String) -> Self {
        Self {
            inner: Context::new(msg),
        }
    }
}

impl From<Context<String>> for Error {
    fn from(inner: Context<String>) -> Self {
        Self { inner }
    }
}

impl From<Context<&'static str>> for Error {
    fn from(inner: Context<&'static str>) -> Self {
        Self {
            inner: inner.map(String::from),
        }
    }
}

impl From<std::io::Error> for Error {
    fn from(e: std::io::Error) -> Self {
        Self {
            inner: Context::new(e.to_string()),
        }
    }
}

impl From<failure::Error> for Error {
    fn from(e: failure::Error) -> Self {
        Self {
            inner: Context::new(e.to_string()),
        }
    }
}

#[cfg(test)]
mod tests;