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
163
use std;
use capi;
use std::ffi::CStr;
type ErrorInt = i32;
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct PAErr(pub ErrorInt);
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Code {
Ok = 0,
Access,
Command,
Invalid,
Exist,
NoEntity,
ConnectionRefused,
Protocol,
Timeout,
AuthKey,
Internal,
ConnectionTerminated,
Killed,
InvalidServer,
ModInitFailed,
BadState,
NoData,
Version,
TooLarge,
NotSupported,
Unknown,
NoExtension,
Obsolete,
NotImplemented,
Forked,
Io,
Busy,
}
#[test]
fn code_compare_capi(){
assert_eq!(std::mem::size_of::<Code>(), std::mem::size_of::<capi::pa_error_code_t>());
assert_eq!(std::mem::align_of::<Code>(), std::mem::align_of::<capi::pa_error_code_t>());
}
impl PAErr {
pub fn to_string(&self) -> Option<String> {
let ptr = unsafe { capi::pa_strerror(self.0) };
match ptr.is_null() {
false => Some(unsafe { CStr::from_ptr(ptr).to_string_lossy().into_owned() }),
true => None,
}
}
}
impl std::fmt::Display for PAErr {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self.to_string() {
Some(s) => write!(f, "{}", s),
None => write!(f, ""),
}
}
}
impl Code {
pub fn to_string(self) -> Option<String> {
PAErr::from(self).to_string()
}
}
impl From<Code> for PAErr {
#[inline]
fn from(c: Code) -> Self {
PAErr(-(c as ErrorInt))
}
}
impl From<PAErr> for Code {
fn from(e: PAErr) -> Self {
let abs = -(e.0);
match abs {
x if x == Code::Ok as ErrorInt => Code::Ok,
x if x == Code::Access as ErrorInt => Code::Access,
x if x == Code::Command as ErrorInt => Code::Command,
x if x == Code::Invalid as ErrorInt => Code::Invalid,
x if x == Code::Exist as ErrorInt => Code::Exist,
x if x == Code::NoEntity as ErrorInt => Code::NoEntity,
x if x == Code::ConnectionRefused as ErrorInt => Code::ConnectionRefused,
x if x == Code::Protocol as ErrorInt => Code::Protocol,
x if x == Code::Timeout as ErrorInt => Code::Timeout,
x if x == Code::AuthKey as ErrorInt => Code::AuthKey,
x if x == Code::Internal as ErrorInt => Code::Internal,
x if x == Code::ConnectionTerminated as ErrorInt => Code::ConnectionTerminated,
x if x == Code::Killed as ErrorInt => Code::Killed,
x if x == Code::InvalidServer as ErrorInt => Code::InvalidServer,
x if x == Code::ModInitFailed as ErrorInt => Code::ModInitFailed,
x if x == Code::BadState as ErrorInt => Code::BadState,
x if x == Code::NoData as ErrorInt => Code::NoData,
x if x == Code::Version as ErrorInt => Code::Version,
x if x == Code::TooLarge as ErrorInt => Code::TooLarge,
x if x == Code::NotSupported as ErrorInt => Code::NotSupported,
x if x == Code::Unknown as ErrorInt => Code::Unknown,
x if x == Code::NoExtension as ErrorInt => Code::NoExtension,
x if x == Code::Obsolete as ErrorInt => Code::Obsolete,
x if x == Code::NotImplemented as ErrorInt => Code::NotImplemented,
x if x == Code::Forked as ErrorInt => Code::Forked,
x if x == Code::Io as ErrorInt => Code::Io,
x if x == Code::Busy as ErrorInt => Code::Busy,
_ => Code::Unknown,
}
}
}