1use alloc::string::String;
2use thiserror::Error;
3
4#[derive(Debug, Clone, PartialEq, Eq, Default, Error)]
6pub enum SocketError {
7 #[error("Unsupported address family")]
9 UnsupportedAddressFamily,
10 #[error("Errno: {0}")]
12 Errno(i32),
13 #[error("Errno: {0} ({1})")]
15 ErrnoWithDescription(i32, String),
16 #[error("{0}")]
18 Other(String),
19 #[default]
21 #[error("Unknown error")]
22 Unknown,
23}
24
25impl SocketError {
26 #[must_use]
28 pub fn new_errno_with_description<S>(errno: i32, description: S) -> Self
29 where
30 S: Into<String>,
31 {
32 SocketError::ErrnoWithDescription(errno, description.into())
33 }
34}
35
36impl embedded_io::Error for SocketError {
37 fn kind(&self) -> embedded_io::ErrorKind {
38 match self {
39 SocketError::UnsupportedAddressFamily => embedded_io::ErrorKind::Unsupported,
40 _ => embedded_io::ErrorKind::Other,
41 }
42 }
43}
44
45#[derive(Debug, Clone, Error)]
50pub enum TlsSocketError {
51 #[error("TLS error: {}", 0)]
53 TlsError(TlsError),
54 #[error("Socket error: {0}")]
56 SocketError(#[from] SocketError),
57}
58
59impl TlsSocketError {
60 #[must_use]
64 pub fn is_tls_error(&self) -> bool {
65 matches!(self, Self::TlsError(..))
66 }
67
68 #[must_use]
72 pub fn is_socket_error(&self) -> bool {
73 matches!(self, Self::SocketError(..))
74 }
75}
76
77impl From<TlsError> for TlsSocketError {
78 fn from(value: TlsError) -> Self {
79 TlsSocketError::TlsError(value)
80 }
81}
82
83impl embedded_io::Error for TlsSocketError {
84 fn kind(&self) -> embedded_io::ErrorKind {
85 match self {
86 TlsSocketError::TlsError(tls_error) => tls_error.kind(),
87 TlsSocketError::SocketError(socket_error) => socket_error.kind(),
88 }
89 }
90}
91
92pub type TlsError = embedded_tls::TlsError;