favicon-scrapper: Don't panic on error

This commit is contained in:
Bilal Elmoussaoui 2022-06-06 09:12:29 +02:00
parent c75adc536e
commit be9502a5b8
2 changed files with 12 additions and 6 deletions

View file

@ -5,6 +5,7 @@ pub enum Error {
Io(std::io::Error), Io(std::io::Error),
Image(image::ImageError), Image(image::ImageError),
NoResults, NoResults,
Utf8(std::str::Utf8Error),
} }
impl From<std::io::Error> for Error { impl From<std::io::Error> for Error {
@ -31,6 +32,12 @@ impl From<image::ImageError> for Error {
} }
} }
impl From<std::str::Utf8Error> for Error {
fn from(e: std::str::Utf8Error) -> Self {
Self::Utf8(e)
}
}
impl std::error::Error for Error {} impl std::error::Error for Error {}
impl std::fmt::Display for Error { impl std::fmt::Display for Error {
@ -41,6 +48,7 @@ impl std::fmt::Display for Error {
Self::Url(e) => write!(f, "Url Parse Error{}", e), Self::Url(e) => write!(f, "Url Parse Error{}", e),
Self::Io(e) => write!(f, "IO Error {}", e), Self::Io(e) => write!(f, "IO Error {}", e),
Self::Image(e) => write!(f, "Image Error {}", e), Self::Image(e) => write!(f, "Image Error {}", e),
Self::Utf8(e) => write!(f, "String conversion error {}", e),
} }
} }
} }

View file

@ -33,7 +33,7 @@ impl Scrapper {
#[allow(dead_code)] #[allow(dead_code)]
pub async fn from_file(path: PathBuf, base_url: Option<Url>) -> Result<Self, Error> { pub async fn from_file(path: PathBuf, base_url: Option<Url>) -> Result<Self, Error> {
let bytes = tokio::fs::read(path).await?; let bytes = tokio::fs::read(path).await?;
let body = std::str::from_utf8(&bytes).unwrap(); let body = std::str::from_utf8(&bytes)?;
Self::from_string(body.to_owned(), base_url) Self::from_string(body.to_owned(), base_url)
} }
@ -133,7 +133,7 @@ impl Scrapper {
key: b"content", key: b"content",
value, value,
}) => { }) => {
let mut href = String::from_utf8(value.into_owned()).unwrap(); let mut href = String::from_utf8(value.into_owned()).ok()?;
if href.starts_with("//") { if href.starts_with("//") {
href = format!("https:{}", href); href = format!("https:{}", href);
} }
@ -178,7 +178,7 @@ impl Scrapper {
key: b"href", key: b"href",
value, value,
}) => { }) => {
let mut href = String::from_utf8(value.into_owned()).unwrap(); let mut href = String::from_utf8(value.into_owned()).ok()?;
if href.starts_with("data:") { if href.starts_with("data:") {
// only bitmap icons contain ';' as a separator, svgs uses ',' // only bitmap icons contain ';' as a separator, svgs uses ','
let mut icon_data = if href.contains(';') { let mut icon_data = if href.contains(';') {
@ -233,9 +233,7 @@ impl Scrapper {
key: b"sizes", key: b"sizes",
value, value,
}) => { }) => {
let size_inner = String::from_utf8(value.into_owned()) let size_inner = String::from_utf8(value.into_owned()).ok()?.to_lowercase();
.unwrap()
.to_lowercase();
let mut size_inner = size_inner.split('x'); let mut size_inner = size_inner.split('x');
let width = size_inner.next().and_then(|w| w.parse::<u32>().ok()); let width = size_inner.next().and_then(|w| w.parse::<u32>().ok());
let height = size_inner.next().and_then(|h| h.parse::<u32>().ok()); let height = size_inner.next().and_then(|h| h.parse::<u32>().ok());