1use thiserror::Error as ThisError;
5
6use num_enum::{TryFromPrimitiveError,TryFromPrimitive};
7use enumflags2::{FromBitsError,BitFlag};
8
9pub type Result<T> = std::result::Result<T, Error>;
11
12#[derive(ThisError, Debug)]
14pub enum Error {
15
16 #[error("That resource or value could not be found")]
18 NotFound,
19
20 #[error("Binary could not be parsed")]
22 ParseError,
23
24 #[error("Value could not be converted")]
26 ConversionError,
27
28 #[error("Slice or access is out of bounds")]
30 OutOfBoundsError,
31
32 #[error("Given data has the wrong shape for operation")]
34 MalformedDataError,
35
36 #[error("Given section is of the wrong type")]
38 WrongSectionError,
39
40 #[error("Given object is of the wrong type")]
42 WrongTypeError,
43
44 #[error("Could not convert from complex value")]
46 FromComplexError,
47
48 #[error("Could not convert from primitive value")]
50 FromPrimitiveError(String),
51
52 #[error("Mutex is poisoned and data is unavailable")]
54 PoisonError(String),
55
56 #[error("Could not parse bytes into CStr representation")]
58 FromBytesWithNulError(#[from] std::ffi::FromBytesWithNulError),
59
60 #[error("Could not convert String to CString")]
62 FromStringError(#[from] std::ffi::NulError),
63
64 #[error("Could not convert CString to String")]
66 IntoStringError(#[from] std::ffi::IntoStringError),
67
68 #[error("This error will never actually be created")]
70 InfallibleError(#[from] std::convert::Infallible),
71
72 #[error("Failed while converting bytes to integer values")]
74 ParseValueError(#[from] std::array::TryFromSliceError),
75
76 #[error("Failed while converting bytes to str")]
78 ParseUtf8Error(#[from] std::str::Utf8Error),
79
80 #[error("Could not open a file for reading")]
82 IOError(#[from] std::io::Error),
83
84 #[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}