Skip to main content

web_scrape/scrape/
scrape_error.rs

1use std::fmt::{Display, Formatter};
2
3/// A scraping error.
4#[derive(Debug)]
5pub enum ScrapeError {
6    /// An invalid selection.
7    InvalidSelection { selection: String, message: String },
8
9    /// Expected a single value but got multiple.
10    ExpectedOneGotMultiple { selection: String },
11
12    /// Expected a single value but got none.
13    ExpectedOneGotNone { selection: String },
14
15    /// Expected a single value or no values but got multiple.
16    ExpectedOptionalGotMultiple { selection: String },
17
18    /// An uncategorized error.
19    Other(String),
20}
21
22impl From<String> for ScrapeError {
23    fn from(message: String) -> Self {
24        Self::Other(message)
25    }
26}
27
28impl From<&str> for ScrapeError {
29    fn from(message: &str) -> Self {
30        Self::Other(message.to_string())
31    }
32}
33
34impl Display for ScrapeError {
35    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
36        match self {
37            Self::InvalidSelection { selection, message } => {
38                write!(f, "invalid selection '{}': {}", selection, message)
39            }
40            Self::ExpectedOneGotMultiple { selection } => {
41                write!(f, "expected one, got multiple: {}", selection)
42            }
43            Self::ExpectedOneGotNone { selection } => {
44                write!(f, "expected one, got none: {}", selection)
45            }
46            Self::ExpectedOptionalGotMultiple { selection } => {
47                write!(f, "expected one or none, got multiple: {}", selection)
48            }
49            Self::Other(message) => {
50                write!(f, "error scraping page: {}", message)
51            }
52        }
53    }
54}
55
56impl std::error::Error for ScrapeError {}