contained_core/errors/
asynchronous.rs

1/*
2    Appellation: error <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4    Description: ... Summary ...
5*/
6use super::Error;
7use serde::{Deserialize, Serialize};
8use smart_default::SmartDefault;
9use strum::{Display, EnumString, EnumVariantNames};
10
11#[derive(
12    Clone,
13    Debug,
14    Deserialize,
15    Display,
16    EnumString,
17    EnumVariantNames,
18    Eq,
19    Hash,
20    Ord,
21    PartialEq,
22    PartialOrd,
23    Serialize,
24    SmartDefault,
25)]
26#[strum(serialize_all = "title_case")]
27pub enum AsyncError {
28    BufError(String),
29    CapacityError(String),
30    ConnectionError(String),
31    #[default]
32    Error(String),
33    IOError(String),
34    RecvError(String),
35    RuntimeError(String),
36    SendError(String),
37    SyncError(String),
38}
39
40impl AsyncError {
41    pub fn as_bytes(&self) -> &[u8] {
42        match self {
43            AsyncError::Error(e) => e.as_bytes(),
44            AsyncError::IOError(e) => e.as_bytes(),
45            AsyncError::BufError(e) => e.as_bytes(),
46            AsyncError::ConnectionError(e) => e.as_bytes(),
47            AsyncError::RecvError(e) => e.as_bytes(),
48            AsyncError::SendError(e) => e.as_bytes(),
49            AsyncError::RuntimeError(e) => e.as_bytes(),
50            AsyncError::SyncError(e) => e.as_bytes(),
51            AsyncError::CapacityError(e) => e.as_bytes(),
52        }
53    }
54    pub fn boxed(self) -> Box<Self> {
55        Box::new(self)
56    }
57}
58
59impl std::error::Error for AsyncError {}
60
61impl From<AsyncError> for Error {
62    fn from(error: AsyncError) -> Self {
63        Self::AsyncError(error)
64    }
65}
66
67impl From<Box<dyn std::error::Error + Send + Sync>> for AsyncError {
68    fn from(error: Box<dyn std::error::Error + Send + Sync>) -> Self {
69        Self::Error(error.to_string())
70    }
71}
72
73impl From<anyhow::Error> for AsyncError {
74    fn from(error: anyhow::Error) -> Self {
75        Self::Error(error.to_string())
76    }
77}
78
79impl From<serde_json::Error> for AsyncError {
80    fn from(error: serde_json::Error) -> Self {
81        Self::Error(error.to_string())
82    }
83}
84
85impl From<tokio::io::Error> for AsyncError {
86    fn from(error: tokio::io::Error) -> Self {
87        Self::IOError(error.to_string())
88    }
89}
90
91impl From<tokio::net::tcp::ReuniteError> for AsyncError {
92    fn from(error: tokio::net::tcp::ReuniteError) -> Self {
93        Self::ConnectionError(error.to_string())
94    }
95}
96
97impl From<tokio::sync::AcquireError> for AsyncError {
98    fn from(error: tokio::sync::AcquireError) -> Self {
99        Self::ConnectionError(error.to_string())
100    }
101}
102
103impl<T> From<tokio::sync::SetError<T>> for AsyncError {
104    fn from(error: tokio::sync::SetError<T>) -> Self {
105        Self::ConnectionError(error.to_string())
106    }
107}
108
109impl<T> From<tokio::sync::mpsc::error::SendError<T>> for AsyncError {
110    fn from(error: tokio::sync::mpsc::error::SendError<T>) -> Self {
111        Self::SendError(error.to_string())
112    }
113}
114
115impl From<tokio::sync::oneshot::error::RecvError> for AsyncError {
116    fn from(error: tokio::sync::oneshot::error::RecvError) -> Self {
117        Self::RecvError(error.to_string())
118    }
119}