use crate::service::Handle;
use crate::service::endpoint::DisconnectReason;
use std::fmt::{Display, Formatter};
use std::io;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum IOServiceOperation {
CreateEndpoint,
Resolve,
CreateTarget,
PollSelector,
Register,
Unregister,
}
impl Display for IOServiceOperation {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::CreateEndpoint => f.write_str("create endpoint"),
Self::Resolve => f.write_str("resolve endpoint"),
Self::CreateTarget => f.write_str("create endpoint target"),
Self::PollSelector => f.write_str("poll selector"),
Self::Register => f.write_str("register endpoint"),
Self::Unregister => f.write_str("unregister endpoint"),
}
}
}
#[derive(Debug)]
pub enum IOServiceError {
IO {
handle: Option<Handle>,
operation: IOServiceOperation,
source: io::Error,
},
EndpointNotRecreatable {
handle: Handle,
reason: DisconnectReason,
},
InvalidState {
handle: Option<Handle>,
message: &'static str,
},
}
impl Display for IOServiceError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::IO {
handle,
operation,
source,
} => match handle {
Some(handle) => write!(f, "failed to {operation} for endpoint {handle:?}: {source}"),
None => write!(f, "failed to {operation}: {source}"),
},
Self::EndpointNotRecreatable { handle, reason } => {
write!(f, "endpoint {handle:?} cannot be recreated after {reason}")
}
Self::InvalidState { handle, message } => match handle {
Some(handle) => write!(f, "invalid state for endpoint {handle:?}: {message}"),
None => write!(f, "invalid I/O service state: {message}"),
},
}
}
}
impl std::error::Error for IOServiceError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::IO { source, .. } => Some(source),
Self::EndpointNotRecreatable {
reason: DisconnectReason::IO(source),
..
} => Some(source),
Self::EndpointNotRecreatable { .. } | Self::InvalidState { .. } => None,
}
}
}
impl IOServiceError {
pub(crate) fn io(handle: Option<Handle>, operation: IOServiceOperation, source: io::Error) -> Self {
Self::IO {
handle,
operation,
source,
}
}
}