use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
InvalidBits {
bits: u32,
},
InvalidBlock {
block: usize,
},
InvalidTolerance,
LengthMismatch {
expected: usize,
got: usize,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidBits { bits } => {
write!(f, "bit width {bits} is outside the supported range 2..=16")
}
Self::InvalidBlock { block } => {
write!(f, "block size {block} must be at least 1")
}
Self::InvalidTolerance => {
write!(f, "tolerance must be a finite number greater than 0")
}
Self::LengthMismatch { expected, got } => {
write!(f, "length mismatch: expected {expected}, got {got}")
}
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
pub type Result<T, E = Error> = core::result::Result<T, E>;
pub(crate) fn check_bits(bits: u32) -> Result<()> {
if (2..=16).contains(&bits) {
Ok(())
} else {
Err(Error::InvalidBits { bits })
}
}
pub(crate) fn check_block(block: usize) -> Result<()> {
if block == 0 {
Err(Error::InvalidBlock { block })
} else {
Ok(())
}
}
pub(crate) fn check_len(expected: usize, got: usize) -> Result<()> {
if expected == got {
Ok(())
} else {
Err(Error::LengthMismatch { expected, got })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_bits_rejects_one_and_seventeen() {
assert!(matches!(check_bits(1), Err(Error::InvalidBits { bits: 1 })));
assert!(matches!(
check_bits(17),
Err(Error::InvalidBits { bits: 17 })
));
}
#[test]
fn display_mentions_expected_length() {
let err = Error::LengthMismatch {
expected: 4,
got: 1,
};
assert_eq!(err.to_string(), "length mismatch: expected 4, got 1");
}
}