use thiserror::Error;
#[derive(Clone, Copy, Debug, Error)]
pub enum SocketError {
#[error("the requested socket type is invalid")]
InvalidSocketType,
#[error("the provided context is invalid")]
InvalidContext,
#[error("the limit on the total number of open ØMQ sockets has been reached")]
SocketLimitReached,
#[error("the context specified was terminated")]
ContextTerminated,
#[error("an unexpected error occurred: {0}")]
Unexpected(#[source] zmq::Error),
}
impl SocketError {
fn to_zmq_error(self) -> zmq::Error {
match self {
SocketError::InvalidSocketType => zmq::Error::EINVAL,
SocketError::InvalidContext => zmq::Error::EFAULT,
SocketError::SocketLimitReached => zmq::Error::EMFILE,
SocketError::ContextTerminated => zmq::Error::ETERM,
SocketError::Unexpected(error) => error,
}
}
}
impl From<SocketError> for zmq::Error {
fn from(other: SocketError) -> Self {
other.to_zmq_error()
}
}
impl From<zmq::Error> for SocketError {
fn from(other: zmq::Error) -> Self {
match other {
zmq::Error::EINVAL => SocketError::InvalidSocketType,
zmq::Error::EFAULT => SocketError::InvalidContext,
zmq::Error::EMFILE => SocketError::SocketLimitReached,
zmq::Error::ETERM => SocketError::ContextTerminated,
error => SocketError::Unexpected(error),
}
}
}
#[derive(Clone, Copy, Debug, Error)]
pub enum SendError {
#[error("the context specified was terminated")]
ContextTerminated,
#[error("the message cannot be routed")]
HostUnreachable,
#[error("the message is invalid")]
InvalidMessage,
#[error("the operation was interrupted by delivery of a signal before the message was sent")]
Interrupted,
#[error("an unexpected error occurred: {0}")]
Unexpected(#[source] zmq::Error),
}
impl SendError {
fn to_zmq_error(self) -> zmq::Error {
match self {
SendError::ContextTerminated => zmq::Error::ETERM,
SendError::HostUnreachable => zmq::Error::EHOSTUNREACH,
SendError::InvalidMessage => zmq::Error::EFAULT,
SendError::Interrupted => zmq::Error::EINTR,
SendError::Unexpected(error) => error,
}
}
}
impl From<SendError> for zmq::Error {
fn from(other: SendError) -> Self {
other.to_zmq_error()
}
}
impl From<zmq::Error> for SendError {
fn from(other: zmq::Error) -> Self {
match other {
zmq::Error::ETERM => SendError::ContextTerminated,
zmq::Error::EHOSTUNREACH => SendError::HostUnreachable,
zmq::Error::EFAULT => SendError::InvalidMessage,
zmq::Error::EINTR => SendError::Interrupted,
error => SendError::Unexpected(error),
}
}
}
#[derive(Clone, Copy, Debug, Error)]
pub enum RecvError {
#[error("the context specified was terminated")]
ContextTerminated,
#[error(
"the operation was interrupted by delivery of a signal before the message was received"
)]
Interrupted,
#[error("an unexpected error occurred: {0}")]
Unexpected(#[source] zmq::Error),
}
impl RecvError {
fn to_zmq_error(self) -> zmq::Error {
match self {
RecvError::ContextTerminated => zmq::Error::ETERM,
RecvError::Interrupted => zmq::Error::EINTR,
RecvError::Unexpected(error) => error,
}
}
}
impl From<RecvError> for zmq::Error {
fn from(other: RecvError) -> Self {
other.to_zmq_error()
}
}
impl From<zmq::Error> for RecvError {
fn from(other: zmq::Error) -> Self {
match other {
zmq::Error::ETERM => RecvError::ContextTerminated,
zmq::Error::EINTR => RecvError::Interrupted,
error => RecvError::Unexpected(error),
}
}
}
#[derive(Clone, Copy, Debug, Error)]
pub enum RequestReplyError {
#[error("this socket cannot send when it is awaiting a reply")]
AwaitingReply,
#[error("the context specified was terminated")]
ContextTerminated,
#[error("the message cannot be routed")]
HostUnreachable,
#[error("the operation was interrupted by delivery of a signal before the message was sent")]
Interrupted,
#[error("an unexpected error occurred: {0}")]
Unexpected(#[source] zmq::Error),
}
impl RequestReplyError {
fn to_zmq_error(self) -> zmq::Error {
match self {
RequestReplyError::AwaitingReply => zmq::Error::EFSM,
RequestReplyError::ContextTerminated => zmq::Error::ETERM,
RequestReplyError::HostUnreachable => zmq::Error::EHOSTUNREACH,
RequestReplyError::Interrupted => zmq::Error::EINTR,
RequestReplyError::Unexpected(error) => error,
}
}
}
impl From<RequestReplyError> for zmq::Error {
fn from(other: RequestReplyError) -> Self {
other.to_zmq_error()
}
}
impl From<zmq::Error> for RequestReplyError {
fn from(other: zmq::Error) -> Self {
match other {
zmq::Error::EFSM => RequestReplyError::AwaitingReply,
zmq::Error::ETERM => RequestReplyError::ContextTerminated,
zmq::Error::EHOSTUNREACH => RequestReplyError::HostUnreachable,
zmq::Error::EINTR => RequestReplyError::Interrupted,
error => RequestReplyError::Unexpected(error),
}
}
}
#[derive(Clone, Copy, Debug, Error)]
pub enum SubscribeError {
#[error("the context specified was terminated")]
ContextTerminated,
#[error("the operation was interrupted by delivery of a signal before the message was sent")]
Interrupted,
#[error("an unexpected error occurred: {0}")]
Unexpected(#[source] zmq::Error),
}
impl SubscribeError {
fn to_zmq_error(self) -> zmq::Error {
match self {
SubscribeError::ContextTerminated => zmq::Error::ETERM,
SubscribeError::Interrupted => zmq::Error::EINTR,
SubscribeError::Unexpected(error) => error,
}
}
}
impl From<SubscribeError> for zmq::Error {
fn from(other: SubscribeError) -> Self {
other.to_zmq_error()
}
}
impl From<zmq::Error> for SubscribeError {
fn from(other: zmq::Error) -> Self {
match other {
zmq::Error::ETERM => SubscribeError::ContextTerminated,
zmq::Error::EINTR => SubscribeError::Interrupted,
error => SubscribeError::Unexpected(error),
}
}
}