1 use crate::CatResult;
2 use std::fmt;
3 use std::io;
4 use std::os::raw::c_int;
5 
6 #[repr(C)]
7 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
8 #[allow(non_camel_case_types)]
9 pub enum GifskiError {
10     OK = 0,
11     NULL_ARG,
12     INVALID_STATE,
13     QUANT,
14     GIF,
15     THREAD_LOST,
16     NOT_FOUND,
17     PERMISSION_DENIED,
18     ALREADY_EXISTS,
19     INVALID_INPUT,
20     TIMED_OUT,
21     WRITE_ZERO,
22     INTERRUPTED,
23     UNEXPECTED_EOF,
24     ABORTED,
25     OTHER,
26 }
27 
28 impl Into<io::Error> for GifskiError {
into(self) -> io::Error29     fn into(self) -> io::Error {
30         use std::io::ErrorKind as EK;
31         use GifskiError::*;
32         match self {
33             OK => panic!("wrong err code"),
34             NOT_FOUND => EK::NotFound,
35             PERMISSION_DENIED => EK::PermissionDenied,
36             ALREADY_EXISTS => EK::AlreadyExists,
37             INVALID_INPUT => EK::InvalidInput,
38             TIMED_OUT => EK::TimedOut,
39             WRITE_ZERO => EK::WriteZero,
40             INTERRUPTED => EK::Interrupted,
41             UNEXPECTED_EOF => EK::UnexpectedEof,
42             _ => return io::Error::new(EK::Other, self),
43         }.into()
44     }
45 }
46 
47 impl From<c_int> for GifskiError {
from(res: c_int) -> Self48     fn from(res: c_int) -> Self {
49         use GifskiError::*;
50         match res {
51             x if x == OK as c_int => OK,
52             x if x == NULL_ARG as c_int => NULL_ARG,
53             x if x == INVALID_STATE as c_int => INVALID_STATE,
54             x if x == QUANT as c_int => QUANT,
55             x if x == GIF as c_int => GIF,
56             x if x == THREAD_LOST as c_int => THREAD_LOST,
57             x if x == NOT_FOUND as c_int => NOT_FOUND,
58             x if x == PERMISSION_DENIED as c_int => PERMISSION_DENIED,
59             x if x == ALREADY_EXISTS as c_int => ALREADY_EXISTS,
60             x if x == INVALID_INPUT as c_int => INVALID_INPUT,
61             x if x == TIMED_OUT as c_int => TIMED_OUT,
62             x if x == WRITE_ZERO as c_int => WRITE_ZERO,
63             x if x == INTERRUPTED as c_int => INTERRUPTED,
64             x if x == UNEXPECTED_EOF as c_int => UNEXPECTED_EOF,
65             x if x == ABORTED as c_int => ABORTED,
66             _ => OTHER,
67         }
68     }
69 }
70 
71 impl From<CatResult<()>> for GifskiError {
from(res: CatResult<()>) -> Self72     fn from(res: CatResult<()>) -> Self {
73         use crate::error::Error::*;
74         match res {
75             Ok(_) => GifskiError::OK,
76             Err(err) => match err {
77                 Quant(_) => GifskiError::QUANT,
78                 Pal(_) => GifskiError::GIF,
79                 ThreadSend => GifskiError::THREAD_LOST,
80                 Io(ref err) => err.kind().into(),
81                 _ => GifskiError::OTHER,
82             },
83         }
84     }
85 }
86 
87 impl From<io::ErrorKind> for GifskiError {
from(res: io::ErrorKind) -> Self88     fn from(res: io::ErrorKind) -> Self {
89         use std::io::ErrorKind as EK;
90         match res {
91             EK::NotFound => GifskiError::NOT_FOUND,
92             EK::PermissionDenied => GifskiError::PERMISSION_DENIED,
93             EK::AlreadyExists => GifskiError::ALREADY_EXISTS,
94             EK::InvalidInput | EK::InvalidData => GifskiError::INVALID_INPUT,
95             EK::TimedOut => GifskiError::TIMED_OUT,
96             EK::WriteZero => GifskiError::WRITE_ZERO,
97             EK::Interrupted => GifskiError::INTERRUPTED,
98             EK::UnexpectedEof => GifskiError::UNEXPECTED_EOF,
99             _ => GifskiError::OTHER,
100         }
101     }
102 }
103 
104 impl std::error::Error for GifskiError {}
105 
106 impl fmt::Display for GifskiError {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result107     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108         fmt::Debug::fmt(self, f)
109     }
110 }
111