1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use std::error::Error;
use std::fmt::Display;

#[derive(Debug)]
pub enum JfifError {
    JfifMarkerNotFound,
    InvalidMarker(u8),
    InvalidMarkerLength(usize),
    IoError(std::io::Error),
}

impl From<std::io::Error> for JfifError {
    fn from(err: std::io::Error) -> JfifError {
        JfifError::IoError(err)
    }
}

impl Display for JfifError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use JfifError::*;
        match self {
            JfifMarkerNotFound => write!(f, "Not a JFIF file"),
            InvalidMarker(value) => write!(f, "Invalid marker: 0x{:X}", value),
            InvalidMarkerLength(length) => write!(f, "Invalid length for marker: {}", length),
            IoError(err) => err.fmt(f),
        }
    }
}

impl Error for JfifError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            JfifError::IoError(err) => Some(err),
            _ => None,
        }
    }
}