use std::{error::Error, fmt::Display};
#[derive(Debug)]
pub enum NcwError {
InvalidFileSignature,
InvalidBlockSignature,
InvalidHeader(&'static str),
UnsupportedBitDepth(i16),
TruncatedData { expected: usize, actual: usize },
ReadError(usize),
IoError(std::io::Error),
}
impl Error for NcwError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::IoError(e) => Some(e),
_ => None,
}
}
}
impl Display for NcwError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidFileSignature => write!(f, "invalid NCW file signature"),
Self::InvalidBlockSignature => write!(f, "invalid NCW block signature"),
Self::InvalidHeader(what) => write!(f, "invalid NCW header: {what}"),
Self::UnsupportedBitDepth(bits) => write!(f, "unsupported block bit depth: {bits}"),
Self::TruncatedData { expected, actual } => write!(
f,
"decoded {actual} samples per channel, header promised {expected}"
),
Self::ReadError(n) => write!(f, "failed to read {n} bytes"),
Self::IoError(e) => write!(f, "io error: {e}"),
}
}
}
impl From<std::io::Error> for NcwError {
fn from(e: std::io::Error) -> Self {
Self::IoError(e)
}
}