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    /// Another error.
19    Other(String),
20}
21
22impl<S: Into<String>> From<S> for ScrapeError {
23    fn from(message: S) -> Self {
24        Self::Other(message.into())
25    }
26}
27
28impl Display for ScrapeError {
29    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
30        match self {
31            Self::InvalidSelection { selection, message } => {
32                write!(f, "invalid selection '{}': {}", selection, message)
33            }
34            Self::ExpectedOneGotMultiple { selection } => {
35                write!(f, "expected one, got multiple: {}", selection)
36            }
37            Self::ExpectedOneGotNone { selection } => {
38                write!(f, "expected one, got none: {}", selection)
39            }
40            Self::ExpectedOptionalGotMultiple { selection } => {
41                write!(f, "expected one or none, got multiple: {}", selection)
42            }
43            Self::Other(message) => {
44                write!(f, "error scraping page: {}", message)
45            }
46        }
47    }
48}
49
50impl std::error::Error for ScrapeError {}