use failure::Fail; pub type Result = std::result::Result; #[derive(Debug, Fail)] pub enum Error { #[fail(display = "couldn't find expected element on the Lodestone: {}", _0)] MissingElement(String), #[fail(display = "the content scraped from the Lodestone was invalid: {}", _0)] InvalidContent(String), #[fail(display = "invalid number: {}", _0)] InvalidNumber(std::num::ParseIntError), #[fail(display = "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(|x| x.to_css_string()).collect::>().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) } }