wavefile/
error.rs

1use std::io;
2use std::fmt::{self,Display};
3use std::error::Error;
4
5use byteorder;
6
7#[derive(Debug)]
8pub enum WaveError {
9  IoError(io::Error),
10  Unsupported(String),
11  ParseError(String)
12}
13
14
15impl From<io::Error> for WaveError {
16  fn from(e: io::Error) -> Self {
17    WaveError::IoError(e)
18  }
19}
20
21impl From<byteorder::Error> for WaveError {
22  fn from(e: byteorder::Error) -> Self {
23    match e {
24      byteorder::Error::UnexpectedEOF => WaveError::ParseError("Unexpected EOF".into()),
25      byteorder::Error::Io(e) => WaveError::IoError(e)
26    }
27  }
28}
29
30impl Error for WaveError {
31  fn description(&self) -> &str {
32    match self {
33      &WaveError::ParseError(ref s)  => &s,
34      &WaveError::Unsupported(ref s) => &s,
35      &WaveError::IoError(ref e)     => e.description()
36    }
37  }
38
39  fn cause(&self) -> Option<&Error> {
40    match self {
41      &WaveError::IoError(ref e) => Some(e),
42      _ => None
43    }
44  }
45}
46
47impl Display for WaveError {
48  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49    match self {
50      &WaveError::IoError(ref e)     => write!(f, "IO Error: {}", e),
51      &WaveError::ParseError(ref s)  => write!(f, "Parse Error: {}", s),
52      &WaveError::Unsupported(ref s) => write!(f, "Unsupported Format Error: {}", s)
53    }
54  }
55}