use std::fmt::Display;
#[derive(Debug)]
pub enum Endianness {
BigEndian,
LittleEndian,
}
#[derive(Debug)]
pub enum SerializerError {
IoError(std::io::Error),
TooManyBits,
TooFewBits,
ValueOutOfRange,
}
impl Eq for SerializerError {}
impl PartialEq for SerializerError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::IoError(l0), Self::IoError(r0)) => l0.kind() == r0.kind(),
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
}
}
}
impl From<std::io::Error> for SerializerError {
fn from(value: std::io::Error) -> Self {
Self::IoError(value)
}
}
impl Display for SerializerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SerializerError::IoError(io_error) => write!(f, "IoError({})", io_error),
SerializerError::TooManyBits => write!(f, "TooManyBits"),
SerializerError::TooFewBits => write!(f, "TooFewBits"),
SerializerError::ValueOutOfRange => write!(f, "ValueOutOfRange"),
}
}
}
#[cfg(test)]
mod tests {
use super::SerializerError;
#[test]
fn compare_enums() {
let e1: SerializerError = std::io::Error::from(std::io::ErrorKind::NotFound).into();
let e2: SerializerError = std::io::Error::from(std::io::ErrorKind::PermissionDenied).into();
assert_ne!(e1, e2);
}
#[test]
fn convert_to_string() {
let e1: SerializerError = std::io::Error::from(std::io::ErrorKind::NotFound).into();
let e2 = SerializerError::TooManyBits;
let e3 = SerializerError::TooFewBits;
let e4 = SerializerError::ValueOutOfRange;
assert_eq!(
format!("{}", e1),
format!("IoError({})", std::io::ErrorKind::NotFound)
);
assert_eq!(format!("{}", e2), "TooManyBits");
assert_eq!(format!("{}", e3), "TooFewBits");
assert_eq!(format!("{}", e4), "ValueOutOfRange");
assert_ne!(e1, e2);
}
}