web_scrape/scrape/
error.rs

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
use std::fmt::{Display, Formatter};

use clerr::Report;

/// A scraping error.
#[derive(Debug)]
pub enum Error {
    /// An invalid selection.
    InvalidSelection { selection: String, message: String },

    /// Expected a single value but got multiple.
    ExpectedOneGotMultiple { selection: String },

    /// Expected a single value but got none.
    ExpectedOneGotNone { selection: String },

    /// Expected a single value or no values but got multiple.
    ExpectedOptionalGotMultiple { selection: String },

    /// A generic error report.
    Generic(Report),

    /// Another generic error.
    Other(String)
}

impl From<Report> for Error {
    fn from(report:  Report) -> Self {
        Self::Generic(report)
    }
}

impl From<String> for Error {
    fn from(s: String) -> Self {
        Self::Other(s)
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
         match self {
            Self::InvalidSelection { selection, message } => {
                write!(f, "invalid selection '{}': {}", selection, message)
            }
            Self::ExpectedOneGotMultiple { selection } => {
                write!(f, "expected one, got multiple: {}", selection)
            }
            Self::ExpectedOneGotNone { selection } => {
                write!(f, "expected one, got none: {}", selection)
            }
            Self::ExpectedOptionalGotMultiple { selection } => {
                write!(f, "expected one or none, got multiple: {}", selection)
            }
            Self::Generic(report) => {
                write!(f, "{}", report)
            }
            Self::Other(s) => {
                write!(f, "{}", s)
            }
        }
    }
}

impl std::error::Error for Error {}