Skip to main content

max / audiofiles

2.3 KB · 88 lines History Blame Raw
1 //! Typed errors for the browser crate, replacing `Result<_, String>`.
2
3 use std::path::PathBuf;
4 use thiserror::Error;
5
6 /// Errors from audio preview decoding.
7 #[derive(Error, Debug)]
8 pub enum PreviewError {
9 #[error("failed to open {path}: {source}")]
10 Open {
11 path: PathBuf,
12 source: std::io::Error,
13 },
14 #[error("failed to probe format: {0}")]
15 Probe(String),
16 #[error("no audio track found")]
17 NoTrack,
18 #[error("failed to create decoder: {0}")]
19 Decoder(String),
20 #[error("packet read error: {0}")]
21 Packet(String),
22 #[error("decode error: {0}")]
23 Decode(String),
24 #[error("no audio data decoded")]
25 NoData,
26 }
27
28 /// Errors from theme file loading.
29 #[derive(Error, Debug)]
30 pub enum ThemeError {
31 #[error("failed to read {path}: {source}")]
32 Read {
33 path: PathBuf,
34 source: std::io::Error,
35 },
36 #[error("failed to parse {path}: {source}")]
37 Parse {
38 path: PathBuf,
39 source: toml::de::Error,
40 },
41 }
42
43 #[cfg(test)]
44 mod tests {
45 use super::*;
46
47 #[test]
48 fn preview_error_display() {
49 let err = PreviewError::NoTrack;
50 assert_eq!(err.to_string(), "no audio track found");
51 }
52
53 #[test]
54 fn preview_error_open_includes_path() {
55 let err = PreviewError::Open {
56 path: PathBuf::from("/tmp/test.wav"),
57 source: std::io::Error::new(std::io::ErrorKind::NotFound, "not found"),
58 };
59 let msg = err.to_string();
60 assert!(msg.contains("/tmp/test.wav"));
61 assert!(msg.contains("not found"));
62 }
63
64 #[test]
65 fn theme_error_display() {
66 let err = ThemeError::Read {
67 path: PathBuf::from("theme.toml"),
68 source: std::io::Error::new(std::io::ErrorKind::NotFound, "missing"),
69 };
70 assert!(err.to_string().contains("theme.toml"));
71 }
72
73 #[test]
74 fn preview_error_variants_exhaustive() {
75 // Verify all variants construct without panic
76 let _ = PreviewError::Open {
77 path: PathBuf::new(),
78 source: std::io::Error::other(""),
79 };
80 let _ = PreviewError::Probe("test".into());
81 let _ = PreviewError::NoTrack;
82 let _ = PreviewError::Decoder("test".into());
83 let _ = PreviewError::Packet("test".into());
84 let _ = PreviewError::Decode("test".into());
85 let _ = PreviewError::NoData;
86 }
87 }
88