vviz/
utilities.rs

1//! Utilities. Collection of functions which don't fit in other modules.
2
3/// Union error type.
4#[derive(Debug)]
5pub enum ImageFrumUrlError {
6    /// error from reqwest crate
7    Reqwest(reqwest::Error),
8    /// error from image-rs crate
9    Image(image::ImageError),
10}
11
12impl From<reqwest::Error> for ImageFrumUrlError {
13    fn from(e: reqwest::Error) -> Self {
14        ImageFrumUrlError::Reqwest(e)
15    }
16}
17
18impl From<image::ImageError> for ImageFrumUrlError {
19    fn from(e: image::ImageError) -> Self {
20        ImageFrumUrlError::Image(e)
21    }
22}
23
24/// Load image from web.
25pub fn load_image_from_url<T: reqwest::IntoUrl>(
26    url: T,
27) -> Result<image::DynamicImage, ImageFrumUrlError> {
28    let bytes = reqwest::blocking::get(url)?.bytes()?;
29    let cursor = std::io::Cursor::new(bytes);
30    let img =
31        image::io::Reader::with_format(std::io::BufReader::new(cursor), image::ImageFormat::Png)
32            .decode()?;
33    Ok(img)
34}