//! Typed errors for the browser crate, replacing `Result<_, String>`. use std::path::PathBuf; use thiserror::Error; /// Errors from audio preview decoding. #[derive(Error, Debug)] pub enum PreviewError { #[error("failed to open {path}: {source}")] Open { path: PathBuf, source: std::io::Error, }, #[error("failed to probe format: {0}")] Probe(String), #[error("no audio track found")] NoTrack, #[error("failed to create decoder: {0}")] Decoder(String), #[error("packet read error: {0}")] Packet(String), #[error("decode error: {0}")] Decode(String), #[error("no audio data decoded")] NoData, #[error("invalid hash: {0}")] InvalidHash(String), #[error("file not found: {0}")] FileNotFound(PathBuf), } /// Errors from theme file loading. #[derive(Error, Debug)] pub enum ThemeError { #[error("failed to read {path}: {source}")] Read { path: PathBuf, source: std::io::Error, }, #[error("failed to parse {path}: {source}")] Parse { path: PathBuf, source: toml::de::Error, }, } #[cfg(test)] mod tests { use super::*; #[test] fn preview_error_display() { let err = PreviewError::NoTrack; assert_eq!(err.to_string(), "no audio track found"); } #[test] fn preview_error_open_includes_path() { let err = PreviewError::Open { path: PathBuf::from("/tmp/test.wav"), source: std::io::Error::new(std::io::ErrorKind::NotFound, "not found"), }; let msg = err.to_string(); assert!(msg.contains("/tmp/test.wav")); assert!(msg.contains("not found")); } #[test] fn theme_error_display() { let err = ThemeError::Read { path: PathBuf::from("theme.toml"), source: std::io::Error::new(std::io::ErrorKind::NotFound, "missing"), }; assert!(err.to_string().contains("theme.toml")); } #[test] fn preview_error_variants_exhaustive() { // Verify all variants construct without panic let _ = PreviewError::Open { path: PathBuf::new(), source: std::io::Error::other(""), }; let _ = PreviewError::Probe("test".into()); let _ = PreviewError::NoTrack; let _ = PreviewError::Decoder("test".into()); let _ = PreviewError::Packet("test".into()); let _ = PreviewError::Decode("test".into()); let _ = PreviewError::NoData; } }