claxon/
error.rs

1// Claxon -- A FLAC decoding library in Rust
2// Copyright 2014 Ruud van Asseldonk
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// A copy of the License has been included in the root of the repository.
7
8//! The `error` module defines the error and result types.
9
10use std::error;
11use std::fmt;
12use std::io;
13use std::result;
14use std::string;
15
16/// An error that prevents successful decoding of the FLAC stream.
17#[derive(Debug)]
18pub enum Error {
19    /// Not a decoding error, but a problem with the underlying IO.
20    IoError(io::Error),
21
22    /// An ill-formed FLAC stream was encountered.
23    FormatError(&'static str),
24
25    /// A currently unsupported feature of the FLAC format was encountered.
26    ///
27    /// Claxon reads the FLAC format as it was with FLAC 1.3.1. Values in the
28    /// specification that are marked as reserved will cause a `FormatError`;
29    /// `Unsupported` is used for features that are in the specification, but
30    /// which are not implemented by Claxon.
31    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        // Vendor strings and Vorbis comments are the only place where UTF-8 is
91        // parsed into a String.
92        Error::FormatError("Vorbis comment or vendor string is not valid UTF-8")
93    }
94}
95
96/// A type for results generated by Claxon where the error type is hard-wired.
97pub type Result<T> = result::Result<T, Error>;
98
99/// Shorthand for producing a format error with reason.
100pub fn fmt_err<T>(reason: &'static str) -> Result<T> {
101    Err(Error::FormatError(reason))
102}