web_scrape/scrape/
error.rs

1use std::fmt::{Display, Formatter};
2
3/// A scraping error.
4#[derive(Debug)]
5pub enum Error {
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
19impl Display for Error {
20    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
21        match self {
22            Self::InvalidSelection { selection, message } => {
23                write!(f, "invalid selection '{}': {}", selection, message)
24            }
25            Self::ExpectedOneGotMultiple { selection } => {
26                write!(f, "expected one, got multiple: {}", selection)
27            }
28            Self::ExpectedOneGotNone { selection } => {
29                write!(f, "expected one, got none: {}", selection)
30            }
31            Self::ExpectedOptionalGotMultiple { selection } => {
32                write!(f, "expected one or none, got multiple: {}", selection)
33            }
34        }
35    }
36}
37
38impl std::error::Error for Error {}