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
//!
//! A module for implementing the Portaudio Error type and
//! implementing the std Error trait.
//!

use ffi;

enum_from_primitive! {
/// Error codes returned by PortAudio functions.
#[repr(i32)]
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug, Eq)]
pub enum Error {
    /// No Error
    NoError =
        ffi::PaErrorCode_paNoError,
    /// No audio devices
    NoDevice =
        ffi::PA_NO_DEVICE,
    /// Portaudio not initialized
    NotInitialized =
        ffi::PaErrorCode_paNotInitialized,
    /// Unanticipated error from the host
    UnanticipatedHostError =
        ffi::PaErrorCode_paUnanticipatedHostError,
    /// Invalid channel count
    InvalidChannelCount =
        ffi::PaErrorCode_paInvalidChannelCount,
    /// Invalid sample rate
    InvalidSampleRate =
        ffi::PaErrorCode_paInvalidSampleRate,
    /// Invalid Device
    InvalidDevice =
        ffi::PaErrorCode_paInvalidDevice,
    /// Invalid Flag
    InvalidFlag =
        ffi::PaErrorCode_paInvalidFlag,
    /// The Sample format is not supported
    SampleFormatNotSupported =
        ffi::PaErrorCode_paSampleFormatNotSupported,
    /// Input device not compatible with output device
    BadIODeviceCombination =
        ffi::PaErrorCode_paBadIODeviceCombination,
    /// Memory insufficient
    InsufficientMemory =
        ffi::PaErrorCode_paInsufficientMemory,
    /// The buffer is too big
    BufferTooBig =
        ffi::PaErrorCode_paBufferTooBig,
    /// The buffer is too small
    BufferTooSmall =
        ffi::PaErrorCode_paBufferTooSmall,
    /// Invalid callback
    NullCallback =
        ffi::PaErrorCode_paNullCallback,
    /// Invalid Stream
    BadStreamPtr =
        ffi::PaErrorCode_paBadStreamPtr,
    /// Time out
    TimedOut =
        ffi::PaErrorCode_paTimedOut,
    /// Portaudio internal error
    InternalError =
        ffi::PaErrorCode_paInternalError,
    /// Device unavailable
    DeviceUnavailable =
        ffi::PaErrorCode_paDeviceUnavailable,
    /// Stream info not compatible with the host
    IncompatibleHostApiSpecificStreamInfo =
        ffi::PaErrorCode_paIncompatibleHostApiSpecificStreamInfo,
    /// The stream is stopped
    StreamIsStopped =
        ffi::PaErrorCode_paStreamIsStopped,
    /// The stream is not stopped
    StreamIsNotStopped =
        ffi::PaErrorCode_paStreamIsNotStopped,
    /// The input stream has overflowed
    InputOverflowed =
        ffi::PaErrorCode_paInputOverflowed,
    /// The output has underflowed
    OutputUnderflowed =
        ffi::PaErrorCode_paOutputUnderflowed,
    /// The host API is not found by Portaudio
    HostApiNotFound =
        ffi::PaErrorCode_paHostApiNotFound,
    /// The host API is invalid
    InvalidHostApi =
        ffi::PaErrorCode_paInvalidHostApi,
    /// Portaudio cannot read from the callback stream
    CanNotReadFromACallbackStream =
        ffi::PaErrorCode_paCanNotReadFromACallbackStream,
    /// Portaudio cannot write to the callback stream
    CanNotWriteToACallbackStream =
        ffi::PaErrorCode_paCanNotWriteToACallbackStream,
    /// Portaudio cannot read from an output only stream
    CanNotReadFromAnOutputOnlyStream =
        ffi::PaErrorCode_paCanNotReadFromAnOutputOnlyStream,
    /// Portaudio cannot write to an input only stream
    CanNotWriteToAnInputOnlyStream =
        ffi::PaErrorCode_paCanNotWriteToAnInputOnlyStream,
    /// The stream is not compatible with the host API
    IncompatibleStreamHostApi =
        ffi::PaErrorCode_paIncompatibleStreamHostApi,
    /// Invalid buffer
    BadBufferPtr =
        ffi::PaErrorCode_paBadBufferPtr,
}
}

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

impl ::std::error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::NoError => "No Error",
            Error::NoDevice => "No Device",
            Error::NotInitialized => "PortAudio not initialized",
            Error::UnanticipatedHostError => "Unanticipated error from the host",
            Error::InvalidChannelCount => "Invalid number of channels",
            Error::InvalidSampleRate => "Invalid sample rate",
            Error::InvalidDevice => "Invalid device",
            Error::InvalidFlag => "Invalid flag",
            Error::SampleFormatNotSupported => "Sample format is not supported",
            Error::BadIODeviceCombination => "Input device not compatible with output device",
            Error::InsufficientMemory => "Memory insufficient",
            Error::BufferTooBig => "The buffer is too big",
            Error::BufferTooSmall => "The buffer is too small",
            Error::NullCallback => "Invalid callback",
            Error::BadStreamPtr => "Invalid stream",
            Error::TimedOut => "Time out",
            Error::InternalError => "Portaudio internal error",
            Error::DeviceUnavailable => "Device unavailable",
            Error::IncompatibleHostApiSpecificStreamInfo => {
                "Stream info not compatible with the host"
            }
            Error::StreamIsStopped => "The stream is stopped",
            Error::StreamIsNotStopped => "The stream is not stopped",
            Error::InputOverflowed => "The input stream has overflowed",
            Error::OutputUnderflowed => "The output stream has underflowed",
            Error::HostApiNotFound => "The host api is not found by Portaudio",
            Error::InvalidHostApi => "The host API is invalid",
            Error::CanNotReadFromACallbackStream => {
                "Portaudio cannot read from the callback stream"
            }
            Error::CanNotWriteToACallbackStream => "Portaudio cannot write to the callback stream",
            Error::CanNotReadFromAnOutputOnlyStream => {
                "Portaudio cannot read from an output only stream"
            }
            Error::CanNotWriteToAnInputOnlyStream => {
                "Portaudio cannot write to an input only stream"
            }
            Error::IncompatibleStreamHostApi => "The stream is not compatible with the host API",
            Error::BadBufferPtr => "Invalid buffer",
        }
    }
}