stream_wave_parser/
error.rs1pub type Result<T> = std::result::Result<T, Error>;
5
6type BoxError = Box<dyn std::error::Error + Send + Sync>;
7
8pub enum Error {
10 RiffChunkHeaderIsNotFound,
12
13 WaveChunkHeaderIsNotFound,
15
16 FmtChunkIsNotFound,
18
19 DataIsNotEnough,
21
22 MixerConstruction(&'static str),
24
25 Custom(BoxError),
27}
28
29impl Error {
30 pub fn custom(e: impl std::error::Error + Send + Sync + 'static) -> Self {
32 Self::Custom(Box::new(e))
33 }
34}
35
36impl std::error::Error for Error {}
37
38impl std::fmt::Debug for Error {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
40 if let Self::Custom(e) = self {
41 return format!("custom: {e:?}").fmt(f);
42 }
43
44 <Self as std::fmt::Display>::fmt(self, f)
45 }
46}
47
48impl std::fmt::Display for Error {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
50 use Error::*;
51
52 match self {
53 RiffChunkHeaderIsNotFound => "`RIFF` label is not found.".fmt(f),
54
55 WaveChunkHeaderIsNotFound => "`WAVE` label is not found.".fmt(f),
56
57 FmtChunkIsNotFound => "`fmt ` label is not found.".fmt(f),
58
59 DataIsNotEnough => "data is not enough.".fmt(f),
60
61 MixerConstruction(reason) => {
62 for msg in ["failed to construct a `WaveChannelMixer`: ", reason, "."] {
63 msg.fmt(f)?;
64 }
65 Ok(())
66 }
67
68 Custom(e) => format!("custom: {e}.").fmt(f),
69 }
70 }
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 #[test]
78 fn test_fmt() {
79 assert_eq!(
80 format!("{}", Error::MixerConstruction("foo")),
81 "failed to construct a `WaveChannelMixer`: foo."
82 );
83 }
84}