34 lines
953 B
Rust
34 lines
953 B
Rust
use scraper::selector::ToCss;
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum Error {
|
|
#[error("couldn't find expected element on the lodestone: {0}")]
|
|
MissingElement(String),
|
|
#[error("the content scraped from the lodestone was invalid: {0}")]
|
|
InvalidContent(String),
|
|
#[error("invalid page (1 through {0} available)")]
|
|
InvalidPage(u64),
|
|
|
|
#[error("invalid number: {0}")]
|
|
InvalidNumber(std::num::ParseIntError),
|
|
#[error("invalid url: {0}")]
|
|
InvalidUrl(url::ParseError),
|
|
}
|
|
|
|
impl Error {
|
|
pub fn missing_element(select: &scraper::Selector) -> Self {
|
|
let css = select.to_css_string();
|
|
Error::MissingElement(css)
|
|
}
|
|
|
|
pub fn invalid_content(expecting: &str, found: Option<&str>) -> Self {
|
|
let s = match found {
|
|
Some(f) => format!("expecting `{}`, found `{}`", expecting, f),
|
|
None => format!("expecting `{}`", expecting),
|
|
};
|
|
Error::InvalidContent(s)
|
|
}
|
|
}
|