1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//! Types to represent errors in `stream-wave-parser`.

/// A specialized `Result` type for `stream-wave-parser`.
pub type Result<T> = std::result::Result<T, Error>;

/// The error type for `stream-wave-parser`.
pub enum Error {
    /// The data does not begin `RIFF`.
    RiffChunkHeaderIsNotFound,

    /// The RIFF data does not have a `WAVE` chunk.
    WaveChunkHeaderIsNotFound,

    /// The RIFF WAVE data does not have a `fmt ` chunk before `data` chunk.
    FmtChunkIsNotFound,

    /// The length of `data` chunk is not enough for the amount that represented by the `length` field.
    DataIsNotEnough,
}

impl std::error::Error for Error {}

impl std::fmt::Debug for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        <Self as std::fmt::Display>::fmt(self, f)
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        use Error::*;

        match self {
            RiffChunkHeaderIsNotFound => "`RIFF` label is not found.".fmt(f),

            WaveChunkHeaderIsNotFound => "`WAVE` label is not found.".fmt(f),

            FmtChunkIsNotFound => "`fmt ` label is not found.".fmt(f),

            DataIsNotEnough => "data is not enough".fmt(f),
        }
    }
}