web_image_meta/
lib.rs

1pub mod jpeg;
2pub mod png;
3
4use std::error::Error as StdError;
5use std::fmt;
6
7#[derive(Debug)]
8pub enum Error {
9    /// 無効な画像フォーマット
10    InvalidFormat(String),
11    /// I/Oエラー
12    Io(std::io::Error),
13    /// パースエラー
14    ParseError(String),
15}
16
17impl fmt::Display for Error {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        match self {
20            Error::InvalidFormat(msg) => write!(f, "Invalid format: {msg}"),
21            Error::Io(err) => write!(f, "IO error: {err}"),
22            Error::ParseError(msg) => write!(f, "Parse error: {msg}"),
23        }
24    }
25}
26
27impl StdError for Error {
28    fn source(&self) -> Option<&(dyn StdError + 'static)> {
29        match self {
30            Error::Io(err) => Some(err),
31            _ => None,
32        }
33    }
34}
35
36impl From<std::io::Error> for Error {
37    fn from(err: std::io::Error) -> Self {
38        Error::Io(err)
39    }
40}
41
42impl From<jpeg_decoder::Error> for Error {
43    fn from(err: jpeg_decoder::Error) -> Self {
44        Error::ParseError(format!("JPEG decode error: {err}"))
45    }
46}
47
48impl From<jpeg_encoder::EncodingError> for Error {
49    fn from(err: jpeg_encoder::EncodingError) -> Self {
50        Error::ParseError(format!("JPEG encode error: {err}"))
51    }
52}