use {crate::protocol::ProtocolError, core::fmt};
pub type ClientResult<T> = Result<T, Error>;
#[derive(Debug)]
pub enum Error {
IoError(std::io::Error),
ConnectionSetupErr(ConnectionSetupError),
ProtocolError(ProtocolError),
ServerError(u16),
ParseError(ParseError),
}
impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::IoError(e) => write!(f, "io error: {e}"),
Self::ConnectionSetupErr(e) => write!(f, "connection setup error: {e}"),
Self::ProtocolError(e) => write!(f, "protocol error: {e}"),
Self::ServerError(e) => write!(f, "server error: {e}"),
Self::ParseError(e) => write!(f, "application parse error: {e}"),
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub enum ParseError {
TypeMismatch,
ResponseMismatch,
Other(String),
}
impl std::error::Error for ParseError {}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TypeMismatch => write!(f, "data type mismatch"),
Self::ResponseMismatch => write!(f, "response type mismatch"),
Self::Other(e) => write!(f, "{e}"),
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub enum ConnectionSetupError {
Other(String),
HandshakeError(u8),
InvalidServerHandshake,
}
impl std::error::Error for ConnectionSetupError {}
impl fmt::Display for ConnectionSetupError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Other(e) => write!(f, "{e}"),
Self::HandshakeError(e) => write!(f, "handshake error code {e}"),
Self::InvalidServerHandshake => write!(f, "server sent invalid handshake"),
}
}
}
impl std::error::Error for ProtocolError {}
impl fmt::Display for ProtocolError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidServerResponseForData => write!(f, "invalid data received from server"),
Self::InvalidServerResponseUnknownDataType => {
write!(f, "new or unknown data type received from server")
}
Self::InvalidPacket => write!(f, "invalid packet received from server"),
}
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Self::IoError(e)
}
}
impl From<ProtocolError> for Error {
fn from(e: ProtocolError) -> Self {
Self::ProtocolError(e)
}
}
impl From<ConnectionSetupError> for Error {
fn from(e: ConnectionSetupError) -> Self {
Self::ConnectionSetupErr(e)
}
}