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
38
39
40
41
42
43
44
pub use std::io::Error as IoError;
use std::{
    error::Error,
    fmt::{Debug, Display, Formatter},
};

use oxipng::PngError;
mod for_std;
mod for_walkdir;
pub type TinyResult<T = ()> = Result<T, TinyError>;

#[derive(Debug)]
pub enum TinyError {
    IoError(String),
    FormatError(String),
    TimedOut,
    ImageOptimized,
    ChunkError(String),
    UnknownError,
}

impl From<PngError> for TinyError {
    fn from(error: PngError) -> Self {
        match error {
            PngError::DeflatedDataTooLong(e) => TinyError::ChunkError(format!("DeflatedDataTooLong {}", e)),
            PngError::TimedOut => TinyError::TimedOut,
            PngError::NotPNG => TinyError::FormatError(format!("Except png, found unknown")),
            PngError::APNGNotSupported => TinyError::ChunkError("APNGNotSupported".to_string()),
            PngError::InvalidData => TinyError::ChunkError("InvalidData".to_string()),
            PngError::TruncatedData => TinyError::ChunkError("TruncatedData".to_string()),
            PngError::ChunkMissing(e) => TinyError::ChunkError(e.to_string()),
            PngError::Other(e) => TinyError::ChunkError(e.to_string()),
            _ => panic!("Unsolved error {}", error),
        }
    }
}

impl Display for TinyError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        Debug::fmt(self, f)
    }
}

impl Error for TinyError {}