whisper-apr 0.3.3

WASM-first automatic speech recognition engine implementing OpenAI Whisper
Documentation
//! Error types for Whisper.apr

use thiserror::Error;

/// Result type alias for Whisper operations
pub type WhisperResult<T> = Result<T, WhisperError>;

/// Errors that can occur during Whisper operations
#[derive(Debug, Error)]
pub enum WhisperError {
    /// Invalid audio format or parameters
    #[error("audio error: {0}")]
    Audio(String),

    /// Model loading or inference error
    #[error("model error: {0}")]
    Model(String),

    /// Invalid .apr format
    #[error("format error: {0}")]
    Format(String),

    /// Tokenization error
    #[error("tokenizer error: {0}")]
    Tokenizer(String),

    /// Inference error
    #[error("inference error: {0}")]
    Inference(String),

    /// I/O error
    #[cfg(feature = "std")]
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),

    /// WASM-specific error
    #[cfg(feature = "wasm")]
    #[error("wasm error: {0}")]
    Wasm(String),

    /// Speaker diarization error
    #[error("diarization error: {0}")]
    Diarization(String),

    /// Authentication error (e.g., missing HF_TOKEN)
    #[error("auth error: {0}")]
    Auth(String),

    /// Configuration error
    #[error("config error: {0}")]
    Config(String),

    /// Verification error
    #[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(_)));
    }
}