use std::fmt;
use decibri_resampler::ResamplerError;
use crate::codec::{CodecId, FourCc};
#[derive(Debug)]
#[non_exhaustive]
pub enum DecodeError {
UnsupportedContainer {
tag: FourCc,
},
UnsupportedCodec {
codec: CodecId,
},
UnsupportedSampleFormat {
format: CodecId,
bits_per_sample: u16,
},
UnsupportedChannelLayout {
channels: u16,
},
Malformed {
expected: &'static str,
offset: u64,
},
Truncated {
expected: u64,
available: u64,
},
ContainerCodecMismatch {
declared: CodecId,
found: CodecId,
},
Resample(ResamplerError),
}
impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnsupportedContainer { tag } => {
write!(f, "unrecognised container, leading bytes were '{tag}'")
}
Self::UnsupportedCodec { codec } => {
write!(f, "container names {codec}, which this build cannot decode")
}
Self::UnsupportedSampleFormat {
format,
bits_per_sample,
} => write!(
f,
"{format} at {bits_per_sample} bits per sample is not a supported sample format"
),
Self::UnsupportedChannelLayout { channels } => {
write!(f, "{channels}-channel audio is not a supported layout")
}
Self::Malformed { expected, offset } => {
write!(f, "malformed input at byte {offset}: expected {expected}")
}
Self::Truncated {
expected,
available,
} => write!(
f,
"input ended early: {expected} needed, {available} available"
),
Self::ContainerCodecMismatch { declared, found } => write!(
f,
"container declares {declared} but the payload is {found}"
),
Self::Resample(source) => write!(f, "resampling failed: {source}"),
}
}
}
impl std::error::Error for DecodeError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Resample(source) => Some(source),
_ => None,
}
}
}
impl From<ResamplerError> for DecodeError {
fn from(source: ResamplerError) -> Self {
Self::Resample(source)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn one_of_each() -> Vec<DecodeError> {
vec![
DecodeError::UnsupportedContainer {
tag: FourCc(*b"\x00\x01\x02\x03"),
},
DecodeError::UnsupportedCodec {
codec: CodecId::WaveFormatTag(0x0011),
},
DecodeError::UnsupportedSampleFormat {
format: CodecId::WaveFormatTag(1),
bits_per_sample: 24,
},
DecodeError::UnsupportedChannelLayout { channels: 8 },
DecodeError::Malformed {
expected: "a 'data' chunk header",
offset: 36,
},
DecodeError::Truncated {
expected: 4096,
available: 1200,
},
DecodeError::ContainerCodecMismatch {
declared: CodecId::WaveFormatTag(1),
found: CodecId::WaveFormatTag(7),
},
DecodeError::Resample(ResamplerError::ProcessAfterFlush),
]
}
#[test]
fn every_variant_is_constructible() {
for error in one_of_each() {
match error {
DecodeError::UnsupportedContainer { .. }
| DecodeError::UnsupportedCodec { .. }
| DecodeError::UnsupportedSampleFormat { .. }
| DecodeError::UnsupportedChannelLayout { .. }
| DecodeError::Malformed { .. }
| DecodeError::Truncated { .. }
| DecodeError::ContainerCodecMismatch { .. }
| DecodeError::Resample(_) => {}
}
}
}
#[test]
fn display_names_the_specific_thing() {
assert_eq!(
DecodeError::UnsupportedSampleFormat {
format: CodecId::WaveFormatTag(1),
bits_per_sample: 24,
}
.to_string(),
"WAVE format tag 0x0001 at 24 bits per sample is not a supported sample format"
);
assert_eq!(
DecodeError::UnsupportedCodec {
codec: CodecId::WaveFormatTag(7),
}
.to_string(),
"container names WAVE format tag 0x0007, which this build cannot decode"
);
let mulaw = DecodeError::UnsupportedCodec {
codec: CodecId::WaveFormatTag(7),
}
.to_string();
let alaw = DecodeError::UnsupportedCodec {
codec: CodecId::WaveFormatTag(6),
}
.to_string();
let pcm24 = DecodeError::UnsupportedSampleFormat {
format: CodecId::WaveFormatTag(1),
bits_per_sample: 24,
}
.to_string();
assert_ne!(mulaw, alaw);
assert_ne!(alaw, pcm24);
assert_ne!(mulaw, pcm24);
}
#[test]
fn no_display_string_is_empty_or_generic() {
for error in one_of_each() {
let text = error.to_string();
assert!(!text.is_empty());
assert!(
!text.eq_ignore_ascii_case("unsupported"),
"variant reported a bare category: {text}"
);
}
}
#[test]
fn only_the_resample_variant_chains_a_source() {
use std::error::Error as _;
for error in one_of_each() {
let chains = error.source().is_some();
assert_eq!(
chains,
matches!(error, DecodeError::Resample(_)),
"source chaining is for the Resample variant only: {error}"
);
}
}
#[test]
fn resampler_errors_convert() {
let error: DecodeError = ResamplerError::ZeroSampleRate.into();
assert!(matches!(error, DecodeError::Resample(_)));
assert_eq!(
error.to_string(),
"resampling failed: sample rate must be greater than 0"
);
}
#[test]
fn the_error_crosses_threads() {
fn assert_send_sync<T: Send + Sync + 'static>() {}
assert_send_sync::<DecodeError>();
}
}