1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use alloc::string::FromUtf8Error;
use bincode::ErrorKind;
use core::fmt::{Display, Formatter};
use cosmic_space::err::{CoreReflector, SpaceErr};
use cosmic_space::substance::Substance;
use cosmic_space::wave::core::http2::StatusCode;
use cosmic_space::wave::core::ReflectedCore;

pub trait MechErr:
    CoreReflector
    + ToString
    + From<Box<bincode::ErrorKind>>
    + From<MembraneErr>
    + From<SpaceErr>
    + From<String>
    + From<&'static str>
    + From<GuestErr>
    + From<FromUtf8Error>
{
    fn to_uni_err(self) -> SpaceErr;
}

#[derive(Debug, Clone)]
pub struct GuestErr {
    pub message: String,
}

impl ToString for GuestErr {
    fn to_string(&self) -> String {
        self.message.clone()
    }
}

impl From<MembraneErr> for GuestErr {
    fn from(e: MembraneErr) -> Self {
        Self {
            message: e.to_string(),
        }
    }
}

impl From<SpaceErr> for GuestErr {
    fn from(err: SpaceErr) -> Self {
        Self {
            message: err.to_string(),
        }
    }
}

impl CoreReflector for GuestErr {
    fn as_reflected_core(self) -> ReflectedCore {
        ReflectedCore {
            headers: Default::default(),
            status: StatusCode::from_u16(500u16).unwrap(),
            body: Substance::Text(self.message),
        }
    }
}

impl From<String> for GuestErr {
    fn from(s: String) -> Self {
        Self { message: s }
    }
}

impl From<&str> for GuestErr {
    fn from(s: &str) -> Self {
        Self {
            message: s.to_string(),
        }
    }
}

impl From<FromUtf8Error> for GuestErr {
    fn from(e: FromUtf8Error) -> Self {
        Self {
            message: e.to_string(),
        }
    }
}

impl MechErr for GuestErr {
    fn to_uni_err(self) -> SpaceErr {
        SpaceErr::server_error(self.to_string())
    }
}

impl From<Box<bincode::ErrorKind>> for GuestErr {
    fn from(e: Box<ErrorKind>) -> Self {
        Self {
            message: e.to_string(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct MembraneErr {
    pub error: String,
}

impl From<&str> for MembraneErr {
    fn from(e: &str) -> Self {
        MembraneErr {
            error: format!("{:?}", e),
        }
    }
}

impl From<String> for MembraneErr {
    fn from(e: String) -> Self {
        MembraneErr {
            error: format!("{:?}", e),
        }
    }
}

/*
impl<T> From<PoisonError<T>> for Error {
    fn from(e: PoisonError<T>) -> Self {
        Error {
            error: format!("{:?}", e),
        }
    }
}

 */

impl From<FromUtf8Error> for MembraneErr {
    fn from(e: FromUtf8Error) -> Self {
        MembraneErr {
            error: format!("{:?}", e),
        }
    }
}

impl Display for MembraneErr {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.write_fmt(format_args!("{}", self.error))
    }
}