use alloc::string::String;
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq, Default, Error)]
pub enum SocketError {
#[error("Unsupported address family")]
UnsupportedAddressFamily,
#[error("Errno: {0}")]
Errno(i32),
#[error("Errno: {0} ({1})")]
ErrnoWithDescription(i32, String),
#[error("{0}")]
Other(String),
#[default]
#[error("Unknown error")]
Unknown,
}
impl SocketError {
#[must_use]
pub fn new_errno_with_description<S>(errno: i32, description: S) -> Self
where
S: Into<String>,
{
SocketError::ErrnoWithDescription(errno, description.into())
}
}
impl embedded_io::Error for SocketError {
fn kind(&self) -> embedded_io::ErrorKind {
match self {
SocketError::UnsupportedAddressFamily => embedded_io::ErrorKind::Unsupported,
_ => embedded_io::ErrorKind::Other,
}
}
}
#[derive(Debug, Clone, Error)]
pub enum TlsSocketError {
#[error("TLS error: {}", 0)]
TlsError(TlsError),
#[error("Socket error: {0}")]
SocketError(#[from] SocketError),
}
impl TlsSocketError {
#[must_use]
pub fn is_tls_error(&self) -> bool {
matches!(self, Self::TlsError(..))
}
#[must_use]
pub fn is_socket_error(&self) -> bool {
matches!(self, Self::SocketError(..))
}
}
impl From<TlsError> for TlsSocketError {
fn from(value: TlsError) -> Self {
TlsSocketError::TlsError(value)
}
}
impl embedded_io::Error for TlsSocketError {
fn kind(&self) -> embedded_io::ErrorKind {
match self {
TlsSocketError::TlsError(tls_error) => tls_error.kind(),
TlsSocketError::SocketError(socket_error) => socket_error.kind(),
}
}
}
pub type TlsError = embedded_tls::TlsError;