1use irox_bits::BitsError;
5use std::fmt::{Display, Formatter};
6use std::io::ErrorKind;
7
8#[derive(Debug, Copy, Clone, Eq, PartialEq)]
9pub enum ErrorType {
10 IOError,
11 InvalidData,
12 StructError,
13 UnimplementedMessage,
14}
15#[derive(Debug, Clone)]
16pub struct Error {
17 error_type: ErrorType,
18 error: String,
19}
20
21impl Display for Error {
22 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23 f.write_fmt(format_args!("Error({:?}): {}", self.error_type, self.error))
24 }
25}
26
27impl std::error::Error for Error {}
28
29impl Error {
30 pub fn kind(&self) -> ErrorType {
31 self.error_type
32 }
33 pub(crate) fn new(error_type: ErrorType, error: &'static str) -> Error {
34 Error::new_str(error_type, String::from(error))
35 }
36 pub(crate) fn new_str(error_type: ErrorType, error: String) -> Error {
37 Error { error, error_type }
38 }
39 pub(crate) fn unsupported<T>(msg: &'static str) -> Result<T, Error> {
40 Err(Error::new(ErrorType::UnimplementedMessage, msg))
41 }
42}
43
44impl From<std::io::Error> for Error {
45 fn from(value: std::io::Error) -> Self {
46 Error::new_str(ErrorType::IOError, format!("{value:?}"))
47 }
48}
49
50impl From<BitsError> for Error {
51 fn from(value: BitsError) -> Self {
52 Error::new_str(ErrorType::IOError, format!("{value:?}"))
53 }
54}
55
56impl From<std::io::ErrorKind> for Error {
57 fn from(value: ErrorKind) -> Self {
58 Error::new_str(ErrorType::IOError, format!("{value:?}"))
59 }
60}
61impl From<ErrorType> for Error {
62 fn from(value: ErrorType) -> Self {
63 Error::new_str(value, format!("{value:?}"))
64 }
65}