use hyprwire_core::message;
use std::{error, fmt, io};
#[derive(Debug)]
pub enum Error {
ConnectionClosed,
HandshakeTimeout,
VersionNegotiationFailed,
VersionOutOfRange {
requested: u32,
max: u32,
},
ProtocolViolation(message::Error),
Io(io::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ConnectionClosed => write!(f, "connection closed"),
Self::HandshakeTimeout => write!(f, "handshake timed out"),
Self::VersionNegotiationFailed => {
write!(f, "version negotiation failed: no common protocol version")
}
Self::VersionOutOfRange { requested, max } => write!(
f,
"requested version {requested} exceeds spec maximum {max}"
),
Self::ProtocolViolation(e) => write!(f, "protocol violation: {e}"),
Self::Io(e) => write!(f, "I/O error: {e}"),
}
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Self::ProtocolViolation(e) => Some(e),
Self::Io(e) => Some(e),
_ => None,
}
}
}
impl From<message::Error> for Error {
fn from(e: message::Error) -> Self {
match e {
message::Error::VersionNegotiationFailed => Self::VersionNegotiationFailed,
other => Self::ProtocolViolation(other),
}
}
}