rosomaxa/utils/
error.rs

1use std::hash::{Hash, Hasher};
2
3/// A basic error type which, essentially, a wrapper on String type.
4#[derive(Clone, Debug)]
5pub struct GenericError(String);
6
7/// A type alias for result type with `GenericError`.
8pub type GenericResult<T> = Result<T, GenericError>;
9
10impl GenericError {
11    /// Joins many errors with separator
12    pub fn join_many(errs: &[GenericError], separator: &str) -> String {
13        // TODO is there better way to have join method used?
14        errs.iter().map(|err| err.0.clone()).collect::<Vec<_>>().join(separator)
15    }
16}
17
18impl std::fmt::Display for GenericError {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        write!(f, "{}", self.0)
21    }
22}
23
24impl std::error::Error for GenericError {}
25
26impl From<String> for GenericError {
27    fn from(msg: String) -> Self {
28        Self(msg)
29    }
30}
31
32impl<'a> From<&'a str> for GenericError {
33    fn from(value: &'a str) -> Self {
34        Self(value.to_string())
35    }
36}
37
38impl From<Box<dyn std::error::Error>> for GenericError {
39    fn from(value: Box<dyn std::error::Error>) -> Self {
40        Self(value.to_string())
41    }
42}
43
44impl From<std::io::Error> for GenericError {
45    fn from(value: std::io::Error) -> Self {
46        Self(value.to_string())
47    }
48}
49
50impl PartialEq<Self> for GenericError {
51    fn eq(&self, other: &Self) -> bool {
52        self.0.eq(&other.0)
53    }
54}
55
56impl Eq for GenericError {}
57
58impl Hash for GenericError {
59    fn hash<H: Hasher>(&self, state: &mut H) {
60        self.0.hash(state);
61    }
62}