lodestone-parser/src/error.rs

33 lines
1005 B
Rust
Raw Normal View History

2018-09-02 19:12:52 +00:00
pub type Result<T> = std::result::Result<T, Error>;
2022-06-16 14:10:03 +00:00
#[derive(Debug, thiserror::Error)]
2018-09-02 19:12:52 +00:00
pub enum Error {
2022-06-16 14:10:03 +00:00
#[error("couldn't find expected element on the lodestone: {0}")]
2018-09-02 19:12:52 +00:00
MissingElement(String),
2022-06-16 14:10:03 +00:00
#[error("the content scraped from the lodestone was invalid: {0}")]
2018-09-02 19:12:52 +00:00
InvalidContent(String),
2022-06-16 14:10:03 +00:00
#[error("invalid page (1 through {0} available)")]
2018-09-05 13:39:21 +00:00
InvalidPage(u64),
2018-09-02 19:12:52 +00:00
2022-06-16 14:10:03 +00:00
#[error("invalid number: {0}")]
2018-09-02 19:12:52 +00:00
InvalidNumber(std::num::ParseIntError),
2022-06-16 14:10:03 +00:00
#[error("invalid url: {0}")]
2018-09-02 19:12:52 +00:00
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(" ");
2018-09-02 19:12:52 +00:00
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)
}
}