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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use ffi;
use std::ffi::NulError;
use std::os::raw::c_int;
use std::{error, fmt};

pub type Result<T> = ::std::result::Result<T, Error>;

/// An enumeration of possible errors that can happen when working with cubeb.
#[derive(PartialEq, Eq, Clone, Debug, Copy)]
pub enum ErrorCode {
    /// GenericError
    Error,
    /// Requested format is invalid
    InvalidFormat,
    /// Requested parameter is invalid
    InvalidParameter,
    /// Requested operation is not supported
    NotSupported,
    /// Requested device is unavailable
    DeviceUnavailable,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Error {
    code: ErrorCode,
}

impl Error {
    #[allow(clippy::self_named_constructors)]
    pub fn error() -> Self {
        Error {
            code: ErrorCode::Error,
        }
    }
    pub fn invalid_format() -> Self {
        Error {
            code: ErrorCode::InvalidFormat,
        }
    }
    pub fn invalid_parameter() -> Self {
        Error {
            code: ErrorCode::InvalidParameter,
        }
    }
    pub fn not_supported() -> Self {
        Error {
            code: ErrorCode::NotSupported,
        }
    }
    pub fn device_unavailable() -> Self {
        Error {
            code: ErrorCode::DeviceUnavailable,
        }
    }

    pub fn from_raw(code: c_int) -> Error {
        let code = match code {
            ffi::CUBEB_ERROR_INVALID_FORMAT => ErrorCode::InvalidFormat,
            ffi::CUBEB_ERROR_INVALID_PARAMETER => ErrorCode::InvalidParameter,
            ffi::CUBEB_ERROR_NOT_SUPPORTED => ErrorCode::NotSupported,
            ffi::CUBEB_ERROR_DEVICE_UNAVAILABLE => ErrorCode::DeviceUnavailable,
            // Everything else is just the generic error
            _ => ErrorCode::Error,
        };

        Error { code }
    }

    pub fn code(&self) -> ErrorCode {
        self.code
    }

    pub fn raw_code(&self) -> c_int {
        match self.code {
            ErrorCode::Error => ffi::CUBEB_ERROR,
            ErrorCode::InvalidFormat => ffi::CUBEB_ERROR_INVALID_FORMAT,
            ErrorCode::InvalidParameter => ffi::CUBEB_ERROR_INVALID_PARAMETER,
            ErrorCode::NotSupported => ffi::CUBEB_ERROR_NOT_SUPPORTED,
            ErrorCode::DeviceUnavailable => ffi::CUBEB_ERROR_DEVICE_UNAVAILABLE,
        }
    }
}

impl Default for Error {
    fn default() -> Self {
        Error::error()
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match self.code {
            ErrorCode::Error => "Error",
            ErrorCode::InvalidFormat => "Invalid format",
            ErrorCode::InvalidParameter => "Invalid parameter",
            ErrorCode::NotSupported => "Not supported",
            ErrorCode::DeviceUnavailable => "Device unavailable",
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

impl From<ErrorCode> for Error {
    fn from(code: ErrorCode) -> Error {
        Error { code }
    }
}

impl From<NulError> for Error {
    fn from(_: NulError) -> Error {
        Error::from_raw(ffi::CUBEB_ERROR)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ffi;

    #[test]
    fn test_from_raw() {
        macro_rules! test {
            ( $($raw:ident => $err:ident),* ) => {{
                $(
                    let e = Error::from_raw(ffi::$raw);
                    assert_eq!(e.raw_code(), ffi::$raw);
                    assert_eq!(e.code(), ErrorCode::$err);
                )*
            }};
        }
        test!(CUBEB_ERROR => Error,
              CUBEB_ERROR_INVALID_FORMAT => InvalidFormat,
              CUBEB_ERROR_INVALID_PARAMETER => InvalidParameter,
              CUBEB_ERROR_NOT_SUPPORTED => NotSupported,
              CUBEB_ERROR_DEVICE_UNAVAILABLE => DeviceUnavailable
        );
    }

    #[test]
    fn test_from_error_code() {
        macro_rules! test {
            ( $($raw:ident => $err:ident),* ) => {{
                $(
                    let e = Error::from(ErrorCode::$err);
                    assert_eq!(e.raw_code(), ffi::$raw);
                    assert_eq!(e.code(), ErrorCode::$err);
                )*
            }};
        }
        test!(CUBEB_ERROR => Error,
              CUBEB_ERROR_INVALID_FORMAT => InvalidFormat,
              CUBEB_ERROR_INVALID_PARAMETER => InvalidParameter,
              CUBEB_ERROR_NOT_SUPPORTED => NotSupported,
              CUBEB_ERROR_DEVICE_UNAVAILABLE => DeviceUnavailable
        );
    }
}