use std::io;
use thiserror::Error;
#[derive(Error, Debug, Clone)]
#[non_exhaustive] pub enum ZmqError {
#[error("I/O error: {kind} - {message}")]
IoError {
kind: io::ErrorKind, message: String, },
#[error("Invalid argument provided: {0}")]
InvalidArgument(String),
#[error("Operation timed out")]
Timeout,
#[error("Address already in use: {0}")]
AddrInUse(String), #[error("Address not available: {0}")]
AddrNotAvailable(String),
#[error("Connection refused by peer: {0}")]
ConnectionRefused(String),
#[error("Host is unreachable: {0}")]
HostUnreachable(String),
#[error("Network is unreachable: {0}")]
NetworkUnreachable(String),
#[error("Connection closed by peer or transport")]
ConnectionClosed,
#[error("Permission denied for endpoint: {0}")]
PermissionDenied(String),
#[error("Invalid endpoint format: {0}")]
InvalidEndpoint(String),
#[error("Endpoint resolution failed: {0}")]
EndpointResolutionFailed(String),
#[error("DNS resolution failed: {0}")]
DnsResolutionFailed(String),
#[error("Invalid socket option ID: {0}")]
InvalidOption(i32), #[error("Invalid value provided for option ID {0}")]
InvalidOptionValue(i32),
#[error("Operation is invalid for the socket type ({0})")]
InvalidSocketType(&'static str), #[error("Operation is invalid for the current socket state: {0}")]
InvalidState(&'static str),
#[error("ZMTP protocol violation: {0}")]
ProtocolViolation(String), #[error("Invalid message format for operation: {0}")]
InvalidMessage(String),
#[error("Security error: {0}")]
SecurityError(String),
#[error("Invalid Curve25519 key provided")]
InvalidCurveKey,
#[error("Authentication failed: {0}")]
AuthenticationFailure(String),
#[error("Encryption/Decryption error: {0}")]
EncryptionError(String),
#[error("Resource limit reached (e.g., HWM)")]
ResourceLimitReached,
#[error("Transport scheme not supported or enabled: {0}")]
UnsupportedTransport(String),
#[error("Socket option not supported: {0}")]
UnsupportedOption(i32),
#[error("Feature not supported or enabled: {0}")]
UnsupportedFeature(&'static str),
#[error("Internal library error: {0}")]
Internal(String),
}
impl From<io::Error> for ZmqError {
fn from(e: io::Error) -> Self {
ZmqError::IoError {
kind: e.kind(), message: e.to_string(), }
}
}
#[cfg(feature = "curve")]
impl From<dryoc::Error> for ZmqError {
fn from(e: dryoc::Error) -> Self {
ZmqError::SecurityError(e.to_string())
}
}
impl From<core::array::TryFromSliceError> for ZmqError {
fn from(e: core::array::TryFromSliceError) -> Self {
ZmqError::ProtocolViolation(format!(
"Failed to parse message component due to incorrect length: {}",
e
))
}
}
impl ZmqError {
pub fn from_io_endpoint(e: io::Error, endpoint: &str) -> Self {
match e.kind() {
io::ErrorKind::AddrInUse => ZmqError::AddrInUse(endpoint.to_string()),
io::ErrorKind::AddrNotAvailable => ZmqError::AddrNotAvailable(endpoint.to_string()),
io::ErrorKind::ConnectionRefused => ZmqError::ConnectionRefused(endpoint.to_string()),
io::ErrorKind::PermissionDenied => ZmqError::PermissionDenied(endpoint.to_string()),
io::ErrorKind::TimedOut => ZmqError::Timeout, io::ErrorKind::ConnectionReset | io::ErrorKind::BrokenPipe => ZmqError::ConnectionClosed,
_ => ZmqError::from(e), }
}
}
pub type ZmqResult<T, E = ZmqError> = std::result::Result<T, E>;