use thiserror::Error;
pub type WhisperResult<T> = Result<T, WhisperError>;
#[derive(Debug, Error)]
pub enum WhisperError {
#[error("audio error: {0}")]
Audio(String),
#[error("model error: {0}")]
Model(String),
#[error("format error: {0}")]
Format(String),
#[error("tokenizer error: {0}")]
Tokenizer(String),
#[error("inference error: {0}")]
Inference(String),
#[cfg(feature = "std")]
#[error("io error: {0}")]
Io(#[from] std::io::Error),
#[cfg(feature = "wasm")]
#[error("wasm error: {0}")]
Wasm(String),
#[error("diarization error: {0}")]
Diarization(String),
#[error("auth error: {0}")]
Auth(String),
#[error("config error: {0}")]
Config(String),
#[error("verification error: {0}")]
Verification(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = WhisperError::Audio("invalid sample rate".into());
assert_eq!(err.to_string(), "audio error: invalid sample rate");
}
#[test]
fn test_error_variants() {
let audio_err = WhisperError::Audio("test".into());
let model_err = WhisperError::Model("test".into());
let format_err = WhisperError::Format("test".into());
let tokenizer_err = WhisperError::Tokenizer("test".into());
let inference_err = WhisperError::Inference("test".into());
let diarization_err = WhisperError::Diarization("test".into());
assert!(matches!(audio_err, WhisperError::Audio(_)));
assert!(matches!(model_err, WhisperError::Model(_)));
assert!(matches!(format_err, WhisperError::Format(_)));
assert!(matches!(tokenizer_err, WhisperError::Tokenizer(_)));
assert!(matches!(inference_err, WhisperError::Inference(_)));
assert!(matches!(diarization_err, WhisperError::Diarization(_)));
}
#[test]
fn test_diarization_error_display() {
let err = WhisperError::Diarization("speaker identification failed".into());
assert_eq!(
err.to_string(),
"diarization error: speaker identification failed"
);
}
#[test]
fn test_auth_error() {
let err = WhisperError::Auth("missing token".into());
assert_eq!(err.to_string(), "auth error: missing token");
assert!(matches!(err, WhisperError::Auth(_)));
}
#[test]
fn test_config_error() {
let err = WhisperError::Config("invalid setting".into());
assert_eq!(err.to_string(), "config error: invalid setting");
assert!(matches!(err, WhisperError::Config(_)));
}
#[test]
fn test_verification_error() {
let err = WhisperError::Verification("checksum mismatch".into());
assert_eq!(err.to_string(), "verification error: checksum mismatch");
assert!(matches!(err, WhisperError::Verification(_)));
}
#[cfg(feature = "std")]
#[test]
fn test_io_error_from() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let err: WhisperError = io_err.into();
assert!(matches!(err, WhisperError::Io(_)));
assert!(err.to_string().contains("file not found"));
}
#[test]
fn test_error_debug() {
let err = WhisperError::Model("test error".into());
let debug_str = format!("{:?}", err);
assert!(debug_str.contains("Model"));
}
#[cfg(feature = "wasm")]
#[test]
fn test_wasm_error() {
let err = WhisperError::Wasm("wasm runtime error".into());
assert_eq!(err.to_string(), "wasm error: wasm runtime error");
assert!(matches!(err, WhisperError::Wasm(_)));
}
}