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
//! Error struct representing possible failures.

use std::error::Error;
use std::fmt::{Display, Formatter, Result as FmtResult};

#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
/// The error enum representing all possible errors that can originate from this crate.
pub enum HError {
    /// Error originating from the `ureq` crate.
    UReq(ureq::Error),
    /// Error originating from the `miniserde` crate.
    Miniserde(miniserde::Error),
    /// Error originating from `std::io::Error`.
    Io(std::io::Error),
    /// Conversion between returned data and our representation failed.
    ConversionFailed,
}

impl Error for HError {}

impl Display for HError {
    fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult {
        use HError::*;
        match self {
            UReq(e) => write!(fmt, "UReq Error: {}", e),
            Miniserde(e) => write!(fmt, "Miniserde Error: {}", e),
            Io(e) => write!(fmt, "Io Error: {}", e),
            ConversionFailed => write!(
                fmt,
                "Conversion between returned data and our representation failed."
            ),
        }
    }
}

impl From<ureq::Error> for HError {
    fn from(err: ureq::Error) -> Self {
        HError::UReq(err)
    }
}

impl From<miniserde::Error> for HError {
    fn from(err: miniserde::Error) -> Self {
        HError::Miniserde(err)
    }
}

impl From<std::io::Error> for HError {
    fn from(err: std::io::Error) -> Self {
        HError::Io(err)
    }
}

macro_rules! convert {
    ($e:expr) => {
        $e.ok_or(HError::ConversionFailed)?
    };
}

macro_rules! convert_default {
    ($e:expr) => {
        $e.unwrap_or_default()
    };
}