deaf/
errors.rs

1//! A custom error type that aggregates internal errors
2//!
3
4use thiserror::Error as ThisError;
5
6use num_enum::{TryFromPrimitiveError,TryFromPrimitive};
7use enumflags2::{FromBitsError,BitFlag};
8
9/// Type alias for 'Result' that uses our Error type
10pub type Result<T> = std::result::Result<T, Error>;
11
12/// Custom error type used everywhere in this crate
13#[derive(ThisError, Debug)]
14pub enum Error {
15
16    /// A resource could not be found
17    #[error("That resource or value could not be found")]
18    NotFound,
19
20    /// Binary data could not be parsed into fields
21    #[error("Binary could not be parsed")]
22    ParseError,
23
24    /// A value could not be converted to a different representation
25    #[error("Value could not be converted")]
26    ConversionError,
27
28    /// Collection was accessed with an out-of-bounds index
29    #[error("Slice or access is out of bounds")]
30    OutOfBoundsError,
31
32    /// Given data has missing or wrong values for the action
33    #[error("Given data has the wrong shape for operation")]
34    MalformedDataError,
35
36    /// Filed to convert section to a table or array
37    #[error("Given section is of the wrong type")]
38    WrongSectionError,
39
40    /// Filed to convert type to some specialized type
41    #[error("Given object is of the wrong type")]
42    WrongTypeError,
43
44    /// Could not parse complex type from primitive
45    #[error("Could not convert from complex value")]
46    FromComplexError,
47
48    /// Could not parse complex type from primitive
49    #[error("Could not convert from primitive value")]
50    FromPrimitiveError(String),
51
52    /// Failed to access shared data because Mutex is poisoned
53    #[error("Mutex is poisoned and data is unavailable")]
54    PoisonError(String),
55
56    /// Bytes with no nul terminator could not be parsed as c-string
57    #[error("Could not parse bytes into CStr representation")]
58    FromBytesWithNulError(#[from] std::ffi::FromBytesWithNulError),
59
60    /// String with no nul terminator could not be parsed as c-string
61    #[error("Could not convert String to CString")]
62    FromStringError(#[from] std::ffi::NulError),
63
64    /// CString failed to convert to UTF-8 encoded String
65    #[error("Could not convert CString to String")]
66    IntoStringError(#[from] std::ffi::IntoStringError),
67
68    /// This error will never actually be created
69    #[error("This error will never actually be created")]
70    InfallibleError(#[from] std::convert::Infallible),
71
72    /// Failed to convert bytes to a value representation
73    #[error("Failed while converting bytes to integer values")]
74    ParseValueError(#[from] std::array::TryFromSliceError),
75
76    /// Bytes could not be converted to UTF-8 encoded String
77    #[error("Failed while converting bytes to str")]
78    ParseUtf8Error(#[from] std::str::Utf8Error),
79
80    /// Could not open a file for reading
81    #[error("Could not open a file for reading")]
82    IOError(#[from] std::io::Error),
83
84    /// Could not convert integer to a different integer type
85    #[error("Failed while converting integer to different integer")]
86    IntConvertError(#[from] std::num::TryFromIntError),
87
88}
89
90impl<T> From<std::sync::PoisonError<T>> for Error {
91    fn from(e: std::sync::PoisonError<T>) -> Self {
92        Error::PoisonError(format!("std::sync::PoisonError: {}",e.to_string()))
93    }
94}
95
96impl<T> From<TryFromPrimitiveError<T>> for Error
97where 
98    T: TryFromPrimitive
99{
100    fn from(e: TryFromPrimitiveError<T>) -> Self {
101        Error::FromPrimitiveError(format!("TryFromPrimitiveError: {}",e.to_string()))
102    }
103}
104
105impl<T> From<FromBitsError<T>> for Error
106where
107    T: BitFlag,
108    T::Numeric: core::fmt::LowerHex
109{
110    fn from(e: FromBitsError<T>) -> Self {
111        Error::FromPrimitiveError(format!("FromBitsError: {:x}",e.invalid_bits()))
112    }
113}