#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ProtocolError {
Malformed(&'static str),
}
impl std::fmt::Display for ProtocolError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Malformed(why) => write!(f, "malformed frame: {why}"),
}
}
}
impl std::error::Error for ProtocolError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CmdError {
Wire(&'static str),
}
impl CmdError {
pub fn as_wire(self) -> &'static str {
match self {
Self::Wire(s) => s,
}
}
}
impl std::fmt::Display for CmdError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_wire())
}
}
impl std::error::Error for CmdError {}
impl From<&'static str> for CmdError {
fn from(s: &'static str) -> Self {
Self::Wire(s)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_malformed_frame_renders_its_reason_and_has_no_source() {
let e = ProtocolError::Malformed("bad bulk length");
assert_eq!(e.to_string(), "malformed frame: bad bulk length");
assert!(std::error::Error::source(&e).is_none());
}
}