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

use serde::{Serialize, Serializer};

#[derive(Debug)]
pub enum ScraperError {
    Request(reqwest::Error),
    Parse(String),
}

impl Serialize for ScraperError {
    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error> where
        S: Serializer {
        match self {
            ScraperError::Request(req) => serializer.serialize_str(req.description()),
            ScraperError::Parse(string) => serializer.serialize_str(string),
        }
    }
}

impl From<String> for ScraperError {
    fn from(error: String) -> Self {
        ScraperError::Parse(error)
    }
}

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

impl Display for ScraperError {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
        match *self {
            ScraperError::Request(ref inner) => ::std::fmt::Display::fmt(inner, f),
            ScraperError::Parse(ref inner) => ::std::fmt::Display::fmt(inner, f),
        }
    }
}

impl Error for ScraperError {}