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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#[derive(Debug, Default, Clone, Hash)]
pub struct AlruError {
    msg: heapless::Vec<u8, 128>,
}

#[cfg(feature = "derive")]
pub mod derive {
    pub use crate::alrulab_macros::CloneValued;
}

impl AlruError {

    #[inline]
    #[must_use]
    pub fn new(msg: &str) -> AlruError {
        if msg.len() > 128 {
            panic!("msg to long");
        }
        AlruError { msg: heapless::Vec::from_slice(msg.as_bytes()).unwrap_or_default() }
    }

    #[inline]
    #[must_use]
    pub fn from_utf8(msg: &[u8]) -> AlruError {
        match core::str::from_utf8(msg) {
            Ok(_) => AlruError { msg: heapless::Vec::from_slice(msg).unwrap_or_default() },
            Err(_) => AlruError { msg: heapless::Vec::from_slice("An Error was detected".as_bytes()).unwrap_or_default() },
        }
    }

    #[inline]
    pub unsafe fn from_utf8_unchecked(msg: &[u8]) -> AlruError {
        AlruError { msg: heapless::Vec::from_slice(msg).unwrap_or_default() }
    }

    pub fn as_bytes(&self) -> &[u8] {
        &self.msg
    }

    pub fn as_mut_bytes(&mut self) -> &mut [u8] {
        &mut self.msg
    }

    pub fn as_str(&self) -> &str {
        core::str::from_utf8(self.as_bytes()).unwrap_or("An Error was detected")
    }

    pub fn as_mut_str(&mut self) -> &mut str {
        core::str::from_utf8_mut(self.as_mut_bytes()).unwrap_or_default()
    }

    pub fn default(&self) -> &str {
        let str = "An Error was detected";
        let bytes = str.as_bytes();
        core::str::from_utf8(bytes).unwrap_or("An Error was detected")
    }

}

impl core::fmt::Display for AlruError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

impl core::error::Error for AlruError {
    fn description(&self) -> &str {
        self.as_str()
    }
    fn cause(&self) -> Option<&dyn core::error::Error> {
        None
    }
}

#[derive(Debug, Clone, Hash)]
pub enum Value<T> {
    Okay(T),
    Error(AlruError),
}

impl<T: Default> Value<T> {

    #[inline]
    #[must_use]
    pub const fn new_okay(value: T) -> Value<T> {
        Value::Okay(value)
    }

    #[inline]
    #[must_use]
    pub const fn new_error(error: AlruError) -> Value<T> {
        Value::Error(error)
    }

    #[inline]
    #[must_use]
    pub const fn nok(value: T) -> Value<T> {
        Value::new_okay(value)
    }

    #[inline]
    #[must_use]
    pub const fn nerr(error: AlruError) -> Value<T> {
        Value::new_error(error)
    }

    #[inline]
    pub fn is_ok(&self) -> bool {
        matches!(self, Value::Okay(_))
    }

    #[inline]
    pub fn is_err(&self) -> bool {
        matches!(self, Value::Error(_))
    }

    #[inline]
    #[must_use = "self will be dropped, if the result is not using"]
    pub fn into_result(self) -> Result<T, AlruError> {
        match self {
            Value::Okay(val) => Ok(val),
            Value::Error(err) => Err(err),
        }
    }

    #[inline]
    #[must_use = "self will be dropped, if the result is not using"]
    pub fn unwrap(self) -> T {
        match self {
            Value::Okay(val) => val,
            Value::Error(err) => panic!("{}", err),
        }
    }

    #[inline]
    #[must_use = "self will be dropped, if the result is not using"]
    pub fn unwrap_err(self) -> AlruError {
        match self {
            Value::Okay(_) => panic!("value is ok"),
            Value::Error(err) => err,
        }
    }

    #[inline]
    #[must_use = "self will be dropped, if the result is not using"]
    pub fn unwrap_or(self, default: T) -> T {
        match self {
            Value::Okay(val) => val,
            Value::Error(_) => default,
        }
    }

    #[inline]
    #[must_use = "self will be dropped, if the result is not using"]
    pub fn unwrap_err_or(self, default: AlruError) -> AlruError {
        match self {
            Value::Okay(_) => panic!("value is ok"),
            Value::Error(err) => err,
        }
    }

    #[inline]
    #[must_use = "self will be dropped, if the result is not using"]
    pub fn unwrap_or_else(self, default: impl FnOnce() -> T) -> T {
        match self {
            Value::Okay(val) => val,
            Value::Error(_) => default(),
        }
    }

    #[inline]
    #[must_use = "self will be dropped, if the result is not using"]
    pub fn unwrap_err_or_else(self, default: impl FnOnce() -> AlruError) -> AlruError {
        match self {
            Value::Okay(_) => panic!("value is ok"),
            Value::Error(err) => err,
        }
    }

    #[inline]
    #[must_use = "self will be dropped, if the result is not using"]
    pub fn unwrap_or_default(self) -> T {
        match self {
            Value::Okay(val) => val,
            Value::Error(_) => Default::default(),
        }
    }

    #[inline]
    #[must_use = "self will be dropped, if the result is not using"]
    pub fn unwrap_err_or_default(self) -> AlruError {
        match self {
            Value::Okay(_) => Default::default(),
            Value::Error(err) => err,
        }
    }

    #[inline]
    #[must_use = "self will be dropped, if the result is not using"]
    pub fn expect(self, msg: &str) -> T {
        match self {
            Value::Okay(val) => val,
            Value::Error(err) => panic!("paniced: {} = Out: {}", msg, err),
        }
    }

    #[inline]
    #[must_use = "self will be dropped, if the result is not using"]
    pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Value<U> {
        match self {
            Value::Okay(val) => Value::Okay(f(val)),
            Value::Error(err) => Value::Error(err),
        }
    }

    #[inline]
    #[must_use = "self will be dropped, if the result is not using"]
    pub fn map_err(self, f: impl FnOnce(AlruError) -> AlruError) -> Value<T> {
        match self {
            Value::Okay(val) => Value::Okay(val),
            Value::Error(err) => Value::Error(f(err)),
        }
    }

}

impl<T> core::ops::Try for Value<T> {
    type Output = T;
    type Residual = Value<core::convert::Infallible>;

    fn from_output(output: Self::Output) -> Self {
        Value::Okay(output)
    }

    fn branch(self) -> core::ops::ControlFlow<Self::Residual, Self::Output> {
        match self {
            Value::Okay(val) => core::ops::ControlFlow::Continue(val),
            Value::Error(err) => core::ops::ControlFlow::Break(Value::Error(err)),
        }
    }
}

impl<T> core::ops::FromResidual<Value<core::convert::Infallible>> for Value<T> {
    fn from_residual(residual: Value<core::convert::Infallible>) -> Self {
        match residual {
            Value::Okay(_) => unreachable!(),
            Value::Error(err) => Value::Error(err),
        }
    }
}

pub trait CloneValued {
    fn clone_to_valued(&self) -> Value<Self>
    where
        Self: Sized;
    fn clone_from_valued(&mut self, value: Value<Self>)
    where
        Self: Sized;
}

pub trait AsValued<T> {
    fn as_valued(&self) -> Value<T>
    where
        Self: Sized;
}