1use std::error;
11use std::fmt;
12use std::io;
13use std::result;
14use std::string;
15
16#[derive(Debug)]
18pub enum Error {
19 IoError(io::Error),
21
22 FormatError(&'static str),
24
25 Unsupported(&'static str),
32}
33
34impl PartialEq for Error {
35 fn eq(&self, other: &Error) -> bool {
36 use error::Error::{IoError, FormatError, Unsupported};
37 match (self, other) {
38 (&FormatError(r1), &FormatError(r2)) => r1 == r2,
39 (&Unsupported(f1), &Unsupported(f2)) => f1 == f2,
40 (&IoError(_), _) => false,
41 (&FormatError(_), _) => false,
42 (&Unsupported(_), _) => false,
43 }
44 }
45}
46
47impl fmt::Display for Error {
48 fn fmt(&self, formatter: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
49 match *self {
50 Error::IoError(ref err) => err.fmt(formatter),
51 Error::FormatError(reason) => {
52 try!(formatter.write_str("Ill-formed FLAC stream: "));
53 formatter.write_str(reason)
54 }
55 Error::Unsupported(feature) => {
56 try!(formatter.write_str("A currently unsupported feature of the FLAC format \
57 was encountered: "));
58 formatter.write_str(feature)
59 }
60 }
61 }
62}
63
64impl error::Error for Error {
65 fn description(&self) -> &str {
66 match *self {
67 Error::IoError(ref err) => err.description(),
68 Error::FormatError(reason) => reason,
69 Error::Unsupported(_) => "unsupported feature",
70 }
71 }
72
73 fn cause(&self) -> Option<&error::Error> {
74 match *self {
75 Error::IoError(ref err) => Some(err),
76 Error::FormatError(_) => None,
77 Error::Unsupported(_) => None,
78 }
79 }
80}
81
82impl From<io::Error> for Error {
83 fn from(err: io::Error) -> Error {
84 Error::IoError(err)
85 }
86}
87
88impl From<string::FromUtf8Error> for Error {
89 fn from(_: string::FromUtf8Error) -> Error {
90 Error::FormatError("Vorbis comment or vendor string is not valid UTF-8")
93 }
94}
95
96pub type Result<T> = result::Result<T, Error>;
98
99pub fn fmt_err<T>(reason: &'static str) -> Result<T> {
101 Err(Error::FormatError(reason))
102}