blf_lib_derivable/
result.rs

1use std::fmt::{Debug, Display};
2use std::num::{ParseIntError, TryFromIntError};
3
4pub struct BLFLibError(Box<dyn std::error::Error>);
5
6impl From<String> for BLFLibError {
7    fn from(e: String) -> BLFLibError {
8        BLFLibError(e.into())
9    }
10}
11
12impl From<&str> for BLFLibError {
13    fn from(e: &str) -> BLFLibError {
14        BLFLibError(e.into())
15    }
16}
17
18impl From<std::io::Error> for BLFLibError {
19    fn from(e: std::io::Error) -> BLFLibError {
20        BLFLibError(e.into())
21    }
22}
23impl From<TryFromIntError> for BLFLibError {
24    fn from(e: TryFromIntError) -> Self {
25        BLFLibError(e.into())
26    }
27}
28
29impl From<std::convert::Infallible> for BLFLibError {
30    fn from(_: std::convert::Infallible) -> Self {
31        unreachable!("Infallible conversion should never fail")
32    }
33}
34
35impl From<ParseIntError> for BLFLibError {
36    fn from(e: ParseIntError) -> BLFLibError {
37        BLFLibError(e.into())
38    }
39}
40
41impl From<std::array::TryFromSliceError> for BLFLibError {
42    fn from(e: std::array::TryFromSliceError) -> BLFLibError {
43        BLFLibError(e.into())
44    }
45}
46
47impl From<std::string::FromUtf8Error> for BLFLibError {
48    fn from(e: std::string::FromUtf8Error) -> BLFLibError {
49        BLFLibError(e.into())
50    }
51}
52
53impl From<std::string::FromUtf16Error> for BLFLibError {
54    fn from(e: std::string::FromUtf16Error) -> BLFLibError {
55        BLFLibError(e.into())
56    }
57}
58
59impl From<Box<dyn std::error::Error>> for BLFLibError {
60    fn from(err: Box<dyn std::error::Error>) -> Self {
61        BLFLibError(err)
62    }
63}
64
65impl From<std::fmt::Error> for BLFLibError {
66    fn from(e: std::fmt::Error) -> Self {
67        BLFLibError(e.into())
68    }
69}
70
71impl From<binrw::Error> for BLFLibError {
72    fn from(e: binrw::Error) -> BLFLibError {
73        BLFLibError(e.into())
74    }
75}
76
77impl From<csv::Error> for BLFLibError {
78    fn from(e: csv::Error) -> BLFLibError {
79        BLFLibError(e.into())
80    }
81}
82
83impl From<serde_json::Error> for BLFLibError {
84    fn from(e: serde_json::Error) -> BLFLibError {
85        BLFLibError(e.into())
86    }
87}
88
89impl From<regex::Error> for BLFLibError {
90    fn from(e: regex::Error) -> BLFLibError {
91        BLFLibError(e.into())
92    }
93}
94
95impl From<BLFLibError> for binrw::Error {
96    fn from(err: BLFLibError) -> Self {
97        binrw::error::Error::Custom {
98            pos: u64::MAX,
99            err: Box::new(err.0.to_string()),
100        }
101    }
102}
103
104
105impl Display for BLFLibError {
106    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
107        std::fmt::Display::fmt(&self.0, f)
108    }
109}
110
111impl Debug for BLFLibError {
112    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
113        std::fmt::Display::fmt(&self.0, f)
114    }
115}
116
117impl std::error::Error for BLFLibError {
118    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
119        self.0.source()
120    }
121}
122pub type BLFLibResult<T = ()> = Result<T, BLFLibError>;