#[derive(Debug)]
#[non_exhaustive]
pub enum InstantiateError {
SampleRateUnsupported(u32),
OutOfMemory,
Other(&'static str),
}
impl core::fmt::Display for InstantiateError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::SampleRateUnsupported(rate) => {
write!(f, "sample rate {rate} Hz is not supported")
}
Self::OutOfMemory => f.write_str("out of memory during instantiate"),
Self::Other(reason) => write!(f, "instantiate failed: {reason}"),
}
}
}
impl std::error::Error for InstantiateError {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_formats_sample_rate() {
let s = InstantiateError::SampleRateUnsupported(96_000).to_string();
assert!(s.contains("96000"));
}
#[test]
fn display_formats_other() {
let s = InstantiateError::Other("missing config").to_string();
assert!(s.contains("missing config"));
}
#[test]
fn implements_std_error() {
fn assert_error<E: std::error::Error>() {}
assert_error::<InstantiateError>();
}
}