scsys_core/error/
std_error.rs

1/*
2    Appellation: std_error <module>
3    Contrib: @FL03
4*/
5use crate::error::ErrorKind;
6use alloc::string::{String, ToString};
7
8/// The [`StdError`] type is a generic error type that is generic over the error kind; this
9/// enables us to distinguish between distinctly different error types.
10pub struct StdError<K: ErrorKind = String> {
11    pub(crate) kind: K,
12    pub(crate) message: String,
13}
14
15impl<K> StdError<K>
16where
17    K: ErrorKind,
18{
19    /// returns a reference to the kind of error
20    pub fn kind(&self) -> &K {
21        &self.kind
22    }
23    /// returns a mutable reference to the kind of error
24    pub fn kind_mut(&mut self) -> &mut K {
25        &mut self.kind
26    }
27    /// returns a reference to the message
28    pub fn message(&self) -> &str {
29        &self.message
30    }
31    /// update the kind before returning a mutable reference to the current instance.
32    pub fn set_kind(&mut self, value: K) -> &mut Self {
33        self.kind = value;
34        self
35    }
36    /// update the message before returning a mutable reference to the current instance.
37    pub fn set_message<V: ToString>(&mut self, value: V) -> &mut Self {
38        self.message = value.to_string();
39        self
40    }
41    /// consumes the current instance to create another with the given kind
42    pub fn with_kind(self, kind: K) -> Self {
43        Self { kind, ..self }
44    }
45    /// consumes the current instance to create another with the given message.
46    pub fn with_message(self, message: String) -> Self {
47        Self { message, ..self }
48    }
49}
50
51unsafe impl<K: ErrorKind> Send for StdError<K> {}
52
53unsafe impl<K: ErrorKind> Sync for StdError<K> {}