youtube_metadata/
error.rs

1use std::{
2    error::Error as StdError,
3    fmt::{Display, Formatter, Result as FmtResult},
4};
5
6use reqwest::Error as ReqwestError;
7
8pub type Result<T> = std::result::Result<T, Error>;
9
10#[derive(Debug)]
11pub enum Error {
12    Reqwest(ReqwestError),
13    Parse(Parsing),
14}
15
16impl Display for Error {
17    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
18        use Error::*;
19        match self {
20            Reqwest(e) => e.fmt(f),
21            Parse(_) => write!(f, "parse error"),
22        }
23    }
24}
25
26impl StdError for Error {
27    fn source(&self) -> Option<&(dyn StdError + 'static)> {
28        use Error::*;
29        match self {
30            Reqwest(e) => e.source(),
31            Parse(e) => Some(e),
32        }
33    }
34}
35
36impl From<Parsing> for Error {
37    fn from(s: Parsing) -> Self {
38        Self::Parse(s)
39    }
40}
41
42impl From<ReqwestError> for Error {
43    fn from(e: ReqwestError) -> Self {
44        Self::Reqwest(e)
45    }
46}
47
48#[derive(Debug)]
49pub enum Parsing {
50    MissingElement(String),
51    MissingAttribute(String),
52}
53
54impl Display for Parsing {
55    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
56        use Parsing::*;
57        match self {
58            MissingAttribute(s) => write!(f, "missing attribute: {}", s),
59            MissingElement(s) => write!(f, "missing element: {}", s),
60        }
61    }
62}
63
64impl StdError for Parsing {}