libsoxr/
error_handling.rs

1//! Convenience functions for error handling
2use std::borrow::Cow;
3use std::fmt;
4
5#[derive(Debug)]
6pub enum ErrorType {
7    InvalidString,
8    CreateError(String),
9    ChangeError(String),
10    ProcessError(String),
11}
12
13impl fmt::Display for ErrorType {
14    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15        match *self {
16            ErrorType::InvalidString => write!(f, "Invalid string"),
17            ErrorType::CreateError(ref s) => write!(f, "Could not create soxr struct: {}", s),
18            ErrorType::ChangeError(ref s) => write!(f, "Could not change soxr struct: {}", s),
19            ErrorType::ProcessError(ref s) => write!(f, "Could not process data: {}", s),
20        }
21    }
22}
23
24#[derive(Debug)]
25pub struct Error(pub(crate) Option<Cow<'static, str>>, pub(crate) ErrorType);
26
27impl Error {
28    pub fn new(func: Option<Cow<'static, str>>, t: ErrorType) -> Error {
29        Error(func, t)
30    }
31    pub fn invalid_str(func: &'static str) -> Error {
32        Error(Some(func.into()), ErrorType::InvalidString)
33    }
34}
35
36impl ::std::error::Error for Error {
37    fn description(&self) -> &str {
38        "SOXR error"
39    }
40}
41
42impl fmt::Display for Error {
43    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44        match self.0 {
45            Some(ref s) => write!(f, "SOXR error: '{}' from function '{}'", s, self.1),
46            None => write!(f, "SOXR error: '{}'", self.1),
47        }
48    }
49}
50
51pub type Result<T> = ::std::result::Result<T, Error>;