ico_builder/
error.rs

1use core::fmt;
2use std::path::PathBuf;
3use std::{error, io};
4
5#[derive(Debug)]
6#[non_exhaustive]
7pub enum Error {
8    Image(image::ImageError),
9    Io(io::Error),
10    MissingIconSize(u32),
11    NonSquareImage {
12        path: PathBuf,
13        width: u32,
14        height: u32,
15    },
16}
17
18impl error::Error for Error {
19    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
20        match self {
21            Error::Image(e) => e.source(),
22            Error::Io(e) => e.source(),
23            Error::MissingIconSize(..) => None,
24            Error::NonSquareImage { .. } => None,
25        }
26    }
27}
28impl fmt::Display for Error {
29    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
30        match self {
31            Error::Image(e) => e.fmt(f),
32            Error::Io(e) => e.fmt(f),
33            Error::MissingIconSize(size) => write!(f, "No icon in the sources is >= {size}px"),
34            Error::NonSquareImage {
35                path,
36                width,
37                height,
38            } => write!(
39                f,
40                "Image {p} ({width} × {height}) is not a square",
41                p = path.display()
42            ),
43        }
44    }
45}
46
47impl From<image::ImageError> for Error {
48    fn from(source: image::ImageError) -> Self {
49        Error::Image(source)
50    }
51}
52
53impl From<io::Error> for Error {
54    fn from(source: io::Error) -> Self {
55        Error::Io(source)
56    }
57}