http_header/
error.rs

1use ebacktrace::define_error;
2use std::{
3    io, result,
4    fmt::{ self, Display, Formatter }
5};
6
7
8/// Creates a new variant
9#[macro_export] macro_rules! e {
10    ($kind:expr, $($arg:tt)*) => ({ $crate::error::ErrorImpl::with_string($kind, format!($($arg)*)) })
11}
12/// Creates a new `ErrorImpl::InOutError` kind
13#[macro_export] macro_rules! eio {
14    ($($arg:tt)*) => ({ e!($crate::error::ErrorKind::InOutError, $($arg)*) });
15}
16/// Creates a new `ErrorImpl::InvalidValue` kind
17#[macro_export] macro_rules! einval {
18    ($($arg:tt)*) => ({ e!($crate::error::ErrorKind::InvalidValue, $($arg)*) });
19}
20
21
22/// The error kind
23#[derive(Debug, PartialEq, Eq)]
24pub enum ErrorKind {
25    /// An I/O-related error occurred
26    InOutError,
27    /// A value is invalid
28    InvalidValue
29}
30impl Display for ErrorKind {
31    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
32        match self {
33            Self::InOutError => write!(f, "An I/O-error occurred"),
34            Self::InvalidValue => write!(f, "A value is invalid")
35        }
36    }
37}
38
39
40// Define our custom error type
41define_error!(ErrorImpl);
42impl From<io::Error> for ErrorImpl<ErrorKind> {
43    fn from(underlying: io::Error) -> Self {
44        ErrorImpl::with_string(ErrorKind::InOutError, underlying)
45    }
46}
47
48
49/// A nice typealias for our custom error
50pub type Error = ErrorImpl<ErrorKind>;
51/// A nice typealias for a `Result` with our custom error
52pub type Result<T = ()> = result::Result<T, ErrorImpl<ErrorKind>>;