concision_core/error/
err.rs1use super::ErrorKind;
6use crate::uuid;
7
8#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
9#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize,))]
10pub struct Error {
11 id: String,
12 kind: ErrorKind,
13 message: String,
14}
15
16impl Error {
17 pub fn new(kind: ErrorKind, message: impl ToString) -> Self {
18 Self {
19 id: uuid().to_string(),
20 kind,
21 message: message.to_string(),
22 }
23 }
24
25 pub fn from_kind<K>(kind: K) -> Self
26 where
27 K: Into<ErrorKind>,
28 {
29 Self::new(kind.into(), "")
30 }
31
32 pub fn id(&self) -> &str {
33 &self.id
34 }
35
36 pub fn kind(&self) -> &ErrorKind {
37 &self.kind
38 }
39
40 pub fn message(&self) -> &str {
41 &self.message
42 }
43
44 pub fn with_message(mut self, message: impl ToString) -> Self {
45 self.message = message.to_string();
46 self
47 }
48}
49
50impl core::fmt::Display for Error {
51 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
52 write!(f, "{}: {}", self.kind, self.message)
53 }
54}
55#[cfg(feature = "std")]
56impl std::error::Error for Error {}
57
58impl From<ErrorKind> for Error {
59 fn from(kind: ErrorKind) -> Self {
60 Self::from_kind(kind)
61 }
62}
63
64impl<'a, K> From<&'a K> for Error
65where
66 K: Clone + Into<ErrorKind>,
67{
68 fn from(kind: &'a K) -> Self {
69 Self::from_kind(kind.clone())
70 }
71}