imageinfo/
defs.rs

1use serde::Serialize;
2
3#[derive(Debug, PartialEq, Serialize)]
4pub struct ImageSize {
5    pub width: i64,
6    pub height: i64,
7}
8
9impl std::fmt::Display for ImageSize {
10    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
11        f.write_fmt(format_args!(
12            "{{width: {}, height: {}}}",
13            self.width, self.height
14        ))
15    }
16}
17
18#[derive(Debug)]
19pub enum ImageInfoError {
20    UnrecognizedFormat,
21    IoError(std::io::Error),
22}
23
24impl std::error::Error for ImageInfoError {}
25
26impl std::fmt::Display for ImageInfoError {
27    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
28        match self {
29            Self::UnrecognizedFormat => f.write_str("unrecognized image format"),
30            Self::IoError(err) => err.fmt(f),
31        }
32    }
33}
34
35impl From<std::io::Error> for ImageInfoError {
36    fn from(err: std::io::Error) -> ImageInfoError {
37        ImageInfoError::IoError(err)
38    }
39}
40
41impl Serialize for ImageInfoError {
42    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
43    where
44        S: serde::Serializer,
45    {
46        serializer.serialize_str(self.to_string().as_ref())
47    }
48}
49
50pub type ImageInfoResult<T> = Result<T, ImageInfoError>;