lodestone-parser/src/error.rs

33 lines
1005 B
Rust

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 {
use cssparser::ToCss;
let css = select.selectors.iter().map(ToCss::to_css_string).collect::<Vec<_>>().join(" ");
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)
}
}