Skip to main content

max / audiofiles

2.5 KB · 92 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 #[error("invalid hash: {0}")]
27 InvalidHash(String),
28 #[error("file not found: {0}")]
29 FileNotFound(PathBuf),
30 }
31
32 /// Errors from theme file loading.
33 #[derive(Error, Debug)]
34 pub enum ThemeError {
35 #[error("failed to read {path}: {source}")]
36 Read {
37 path: PathBuf,
38 source: std::io::Error,
39 },
40 #[error("failed to parse {path}: {source}")]
41 Parse {
42 path: PathBuf,
43 source: toml::de::Error,
44 },
45 }
46
47 #[cfg(test)]
48 mod tests {
49 use super::*;
50
51 #[test]
52 fn preview_error_display() {
53 let err = PreviewError::NoTrack;
54 assert_eq!(err.to_string(), "no audio track found");
55 }
56
57 #[test]
58 fn preview_error_open_includes_path() {
59 let err = PreviewError::Open {
60 path: PathBuf::from("/tmp/test.wav"),
61 source: std::io::Error::new(std::io::ErrorKind::NotFound, "not found"),
62 };
63 let msg = err.to_string();
64 assert!(msg.contains("/tmp/test.wav"));
65 assert!(msg.contains("not found"));
66 }
67
68 #[test]
69 fn theme_error_display() {
70 let err = ThemeError::Read {
71 path: PathBuf::from("theme.toml"),
72 source: std::io::Error::new(std::io::ErrorKind::NotFound, "missing"),
73 };
74 assert!(err.to_string().contains("theme.toml"));
75 }
76
77 #[test]
78 fn preview_error_variants_exhaustive() {
79 // Verify all variants construct without panic
80 let _ = PreviewError::Open {
81 path: PathBuf::new(),
82 source: std::io::Error::other(""),
83 };
84 let _ = PreviewError::Probe("test".into());
85 let _ = PreviewError::NoTrack;
86 let _ = PreviewError::Decoder("test".into());
87 let _ = PreviewError::Packet("test".into());
88 let _ = PreviewError::Decode("test".into());
89 let _ = PreviewError::NoData;
90 }
91 }
92