use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Delivery {
NotSent,
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
pub enum ErrorKind {
#[error("connection closed")]
Closed,
#[error("peer closed before terminal completion")]
PeerClosed,
#[error("peer closed inside a frame")]
TruncatedFrame,
#[error("transport I/O failed ({0:?})")]
Io(std::io::ErrorKind),
#[error("local deadline expired")]
Timeout,
#[error("invalid protocol data")]
InvalidData,
#[error("invalid client configuration")]
InvalidOptions,
#[error("client capacity exhausted")]
Capacity,
#[error("correlation ID range exhausted")]
IdRangeExhausted,
#[error("stream is closed or not owned by this connection")]
StreamClosed,
#[error("operation is unsupported by the peer")]
UnsupportedOperation,
#[error("could not encode protocol message")]
Encode,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[error("{kind} (delivery: {delivery:?})")]
pub struct ClientError {
pub kind: ErrorKind,
pub delivery: Delivery,
}
pub type ClientResult<T> = Result<T, ClientError>;
impl ClientError {
pub fn new(kind: ErrorKind) -> Self {
Self {
kind,
delivery: Delivery::NotSent,
}
}
pub fn with_delivery(self, delivery: Delivery) -> Self {
Self { delivery, ..self }
}
}
impl From<std::io::Error> for ClientError {
fn from(error: std::io::Error) -> Self {
Self::new(ErrorKind::Io(error.kind()))
}
}
impl From<microsandbox_protocol::wire::WireError> for ClientError {
fn from(_: microsandbox_protocol::wire::WireError) -> Self {
Self::new(ErrorKind::InvalidData)
}
}