blf_lib_derivable/
result.rs

1use std::fmt::{Debug, Display};
2use std::num::ParseIntError;
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}
23
24impl From<ParseIntError> for BLFLibError {
25    fn from(e: ParseIntError) -> BLFLibError {
26        BLFLibError(e.into())
27    }
28}
29
30impl From<std::array::TryFromSliceError> for BLFLibError {
31    fn from(e: std::array::TryFromSliceError) -> BLFLibError {
32        BLFLibError(e.into())
33    }
34}
35
36impl From<std::string::FromUtf8Error> for BLFLibError {
37    fn from(e: std::string::FromUtf8Error) -> BLFLibError {
38        BLFLibError(e.into())
39    }
40}
41
42impl From<std::string::FromUtf16Error> for BLFLibError {
43    fn from(e: std::string::FromUtf16Error) -> BLFLibError {
44        BLFLibError(e.into())
45    }
46}
47
48impl From<Box<dyn std::error::Error>> for BLFLibError {
49    fn from(err: Box<dyn std::error::Error>) -> Self {
50        BLFLibError(err)
51    }
52}
53
54impl From<std::fmt::Error> for BLFLibError {
55    fn from(e: std::fmt::Error) -> Self {
56        BLFLibError(e.into())
57    }
58}
59
60impl From<binrw::Error> for BLFLibError {
61    fn from(e: binrw::Error) -> BLFLibError {
62        BLFLibError(e.into())
63    }
64}
65
66impl From<csv::Error> for BLFLibError {
67    fn from(e: csv::Error) -> BLFLibError {
68        BLFLibError(e.into())
69    }
70}
71
72impl From<serde_json::Error> for BLFLibError {
73    fn from(e: serde_json::Error) -> BLFLibError {
74        BLFLibError(e.into())
75    }
76}
77
78impl From<regex::Error> for BLFLibError {
79    fn from(e: regex::Error) -> BLFLibError {
80        BLFLibError(e.into())
81    }
82}
83
84impl From<BLFLibError> for binrw::Error {
85    fn from(err: BLFLibError) -> Self {
86        binrw::error::Error::Custom {
87            pos: u64::MAX,
88            err: Box::new(err.0.to_string()),
89        }
90    }
91}
92
93
94impl Display for BLFLibError {
95    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
96        std::fmt::Display::fmt(&self.0, f)
97    }
98}
99
100impl Debug for BLFLibError {
101    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
102        std::fmt::Display::fmt(&self.0, f)
103    }
104}
105
106impl std::error::Error for BLFLibError {
107    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
108        self.0.source()
109    }
110}
111pub type BLFLibResult<T = ()> = Result<T, BLFLibError>;