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
extern crate libsoundio_sys as raw;
use std::error;
use std::ffi::CStr;
use std::fmt;
use std::result;
use std::os::raw::c_int;
#[derive(Debug, Copy, Clone)]
pub enum Error {
NoMem,
InitAudioBackend,
SystemResources,
OpeningDevice,
NoSuchDevice,
Invalid,
BackendUnavailable,
Streaming,
IncompatibleDevice,
NoSuchClient,
IncompatibleBackend,
BackendDisconnected,
Interrupted,
Underflow,
EncodingString,
Unknown,
}
impl From<c_int> for Error {
fn from(err: c_int) -> Error {
match err {
1 => Error::NoMem,
2 => Error::InitAudioBackend,
3 => Error::SystemResources,
4 => Error::OpeningDevice,
5 => Error::NoSuchDevice,
6 => Error::Invalid,
7 => Error::BackendUnavailable,
8 => Error::Streaming,
9 => Error::IncompatibleDevice,
10 => Error::NoSuchClient,
11 => Error::IncompatibleBackend,
12 => Error::BackendDisconnected,
13 => Error::Interrupted,
14 => Error::Underflow,
15 => Error::EncodingString,
_ => Error::Unknown,
}
}
}
impl From<Error> for c_int {
fn from(err: Error) -> c_int {
match err {
Error::NoMem => 1,
Error::InitAudioBackend => 2,
Error::SystemResources => 3,
Error::OpeningDevice => 4,
Error::NoSuchDevice => 5,
Error::Invalid => 6,
Error::BackendUnavailable => 7,
Error::Streaming => 8,
Error::IncompatibleDevice => 9,
Error::NoSuchClient => 10,
Error::IncompatibleBackend => 11,
Error::BackendDisconnected => 12,
Error::Interrupted => 13,
Error::Underflow => 14,
Error::EncodingString => 15,
Error::Unknown => -1,
}
}
}
pub type Result<T> = result::Result<T, Error>;
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use std::error::Error;
f.write_str(self.description())
}
}
impl error::Error for Error {
fn description(&self) -> &str {
let c_str: &CStr = unsafe { CStr::from_ptr(raw::soundio_strerror((*self).into())) };
c_str.to_str().unwrap()
}
fn cause(&self) -> Option<&dyn error::Error> {
None
}
}
impl From<Error> for String {
fn from(err: Error) -> String {
use std::error::Error;
err.description().to_string()
}
}