http_tiny/
error.rs

1use ebacktrace::define_error;
2use std::{
3    error, 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::new($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}
38impl error::Error for ErrorKind {
39    /* No members to implement */
40}
41
42
43// Define our custom error type
44define_error!(ErrorImpl);
45impl From<io::Error> for ErrorImpl<ErrorKind> {
46    fn from(underlying: io::Error) -> Self {
47        ErrorImpl::new(ErrorKind::InOutError, underlying.to_string())
48    }
49}
50
51
52/// A nice typealias for our custom error
53pub type Error = ErrorImpl<ErrorKind>;
54/// A nice typealias for a `Result` with our custom error
55pub type Result<T = ()> = result::Result<T, ErrorImpl<ErrorKind>>;