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
use std::fmt;

#[derive(Debug)]
pub enum Error {
    Request(reqwest::Error),
    HaApi(String),
    Config(String),
    Refresh(),
    NoAuth(),
    PoisonError(
        std::sync::PoisonError<
            std::sync::RwLockReadGuard<'static, std::option::Option<crate::Token>>,
        >,
    ),
}

impl From<reqwest::Error> for Error {
    fn from(error: reqwest::Error) -> Self {
        Error::Request(error)
    }
}

impl
    From<
        std::sync::PoisonError<
            std::sync::RwLockReadGuard<'static, std::option::Option<crate::Token>>,
        >,
    > for Error
{
    fn from(
        error: std::sync::PoisonError<
            std::sync::RwLockReadGuard<'static, std::option::Option<crate::Token>>,
        >,
    ) -> Self {
        Error::PoisonError(error)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Request(inner) => write!(f, "{}", inner),
            Error::Config(inner) => write!(f, "{}", inner),
            Error::HaApi(inner) => write!(f, "{}", inner),
            Error::PoisonError(inner) => write!(f, "{}", inner),
            Error::Refresh() => write!(f, "Tried to refresh a long lived access token"),
            Error::NoAuth() => write!(f, "There are no Authentication Credentials"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Request(inner) => Some(inner),
            _ => None,
        }
    }
}