use std::error::Error;
use std::fmt;
use std::io;
#[derive(Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum NetbatError {
Io {
kind: io::ErrorKind,
},
EmptyStream,
LineTooLong {
max: usize,
},
MalformedRequest {
reason: &'static str,
},
UnsupportedProtocolVersion {
version: String,
},
OperationNameTooLong {
max: usize,
},
InputTooLarge {
max: usize,
},
OutputTooLarge {
max: usize,
},
Runtime(syncbat::RuntimeError),
}
impl fmt::Display for NetbatError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io { kind } => write!(f, "io error: {kind:?}"),
Self::EmptyStream => f.write_str("empty stream"),
Self::LineTooLong { max } => {
write!(f, "request line exceeded {max} bytes")
}
Self::MalformedRequest { reason } => write!(f, "malformed request: {reason}"),
Self::UnsupportedProtocolVersion { version } => {
write!(f, "unsupported protocol version: {version}")
}
Self::OperationNameTooLong { max } => {
write!(f, "operation name exceeded {max} bytes")
}
Self::InputTooLarge { max } => write!(f, "input exceeded {max} bytes"),
Self::OutputTooLarge { max } => write!(f, "output exceeded {max} bytes"),
Self::Runtime(error) => write!(f, "runtime error: {error}"),
}
}
}
impl Error for NetbatError {}
impl From<io::Error> for NetbatError {
fn from(error: io::Error) -> Self {
Self::Io { kind: error.kind() }
}
}
impl From<syncbat::RuntimeError> for NetbatError {
fn from(error: syncbat::RuntimeError) -> Self {
Self::Runtime(error)
}
}
impl NetbatError {
#[must_use]
pub fn code(&self) -> &'static str {
match self {
Self::Io { .. } => "io",
Self::EmptyStream => "empty_stream",
Self::LineTooLong { .. } => "line_too_long",
Self::MalformedRequest { .. } => "malformed_request",
Self::UnsupportedProtocolVersion { .. } => "unsupported_protocol_version",
Self::OperationNameTooLong { .. } => "operation_name_too_long",
Self::InputTooLarge { .. } => "input_too_large",
Self::OutputTooLarge { .. } => "output_too_large",
Self::Runtime(syncbat::RuntimeError::UnknownOperation { .. }) => "unknown_operation",
Self::Runtime(syncbat::RuntimeError::MissingHandler { .. }) => "missing_handler",
Self::Runtime(syncbat::RuntimeError::Handler { .. }) => "handler",
Self::Runtime(syncbat::RuntimeError::ReceiptSink { .. }) => "receipt_sink",
Self::Runtime(_) => "runtime",
}
}
}