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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#[doc(no_inline)]
pub use pam_sys::types::PamReturnCode as ReturnCode;
use pam_sys::types::{PamHandle};
use pam_sys::wrapped::strerror;
use std::any::type_name;
use std::convert::TryFrom;
use std::cmp::{Eq, PartialEq};
use std::error;
use std::hash::{Hash, Hasher};
use std::fmt::{Display, Formatter, Debug, Result as FmtResult};
use std::marker::PhantomData;
use std::io;
#[derive(Copy, Clone, Debug)]
pub enum NoPayload {}
impl Display for NoPayload {
fn fmt(&self, _: &mut Formatter<'_>) -> FmtResult { match *self {} }
}
impl PartialEq for NoPayload {
fn eq(&self, _: &NoPayload) -> bool { match *self {} }
}
impl Eq for NoPayload {}
impl Hash for NoPayload {
fn hash<H: Hasher>(&self, _: &mut H) { match *self {} }
}
enum DisplayHelper<T> { Some(PhantomData<T>), None }
impl<T> DisplayHelper<T> {
#[inline]
fn new(option: &Option<T>) -> Self {
match option {
None => Self::None,
Some(_) => Self::Some(PhantomData)
}
}
}
impl<T> Debug for DisplayHelper<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match *self {
Self::None => write!(f, "None"),
Self::Some(_) => write!(f, "<{}>", type_name::<T>())
}
}
}
#[must_use]
#[derive(Clone)]
pub struct ErrorWith<T> {
code: ReturnCode,
msg: String,
payload: Option<T>
}
impl<T> ErrorWith<T> {
pub fn with_payload(handle: &mut PamHandle, code: ReturnCode, payload: Option<T>) -> ErrorWith<T> {
assert_ne!(code, ReturnCode::SUCCESS);
Self {
code,
msg: match strerror(handle, code) {
None => String::new(),
Some(s) => s.into()
},
payload
}
}
pub const fn code(&self) -> ReturnCode {
self.code
}
pub fn message(&self) -> Option<&str> {
if self.msg.is_empty() { None } else { Some(&self.msg) }
}
#[rustversion::attr(since(1.48), const)]
pub fn payload(&self) -> Option<&T> {
self.payload.as_ref()
}
pub fn take_payload(&mut self) -> Option<T> {
match self.payload {
Some(_) => self.payload.take(),
None => None
}
}
#[inline]
pub fn drop_payload(self) -> Error {
Error {
code: self.code,
msg: self.msg,
payload: None
}
}
pub fn map<U>(self, func: impl FnOnce(T) -> U) -> ErrorWith<U> {
ErrorWith::<U> {
code: self.code,
msg: self.msg,
payload: match self.payload {
None => None,
Some(object) => Some(func(object))
}
}
}
}
impl<T> Debug for ErrorWith<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
if type_name::<T>() == type_name::<NoPayload>() {
f.debug_struct("pam_client::Error")
.field("code", &self.code)
.field("msg", &self.msg)
.finish()
} else {
f.debug_struct("pam_client::ErrorWith")
.field("code", &self.code)
.field("msg", &self.msg)
.field("payload", &DisplayHelper::new(&self.payload))
.finish()
}
}
}
pub type Error = ErrorWith<NoPayload>;
impl Error {
pub fn new(handle: &mut PamHandle, code: ReturnCode) -> Error {
assert_ne!(code, ReturnCode::SUCCESS);
Error {
code,
msg: match strerror(handle, code) {
None => String::new(),
Some(s) => s.into()
},
payload: None
}
}
pub fn into_with_payload<T>(self, payload: T) -> ErrorWith<T> {
ErrorWith::<T> {
code: self.code,
msg: self.msg,
payload: Some(payload)
}
}
pub fn into<T>(self) -> ErrorWith<T> {
ErrorWith::<T> {
code: self.code,
msg: self.msg,
payload: None
}
}
}
impl<T> Display for ErrorWith<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
if self.msg.is_empty() {
write!(f, "<{}>", self.code as i32)
} else {
f.write_str(&self.msg)
}
}
}
impl<T> error::Error for ErrorWith<T> {}
impl<T> PartialEq for ErrorWith<T> where T: PartialEq {
fn eq(&self, other: &Self) -> bool {
self.code == other.code && self.payload == other.payload
}
}
impl<T> Eq for ErrorWith<T> where T: Eq {}
impl<T> Hash for ErrorWith<T> where T: Hash {
fn hash<H: Hasher>(&self, state: &mut H) {
(self.code as i32).hash(state);
self.payload.hash(state);
}
}
impl TryFrom<ReturnCode> for Error {
type Error = ();
fn try_from(code: ReturnCode) -> Result<Self, ()> {
if code == ReturnCode::SUCCESS {
Err(())
} else {
Ok(Error { code, msg: String::new(), payload: None })
}
}
}
impl<T: Send + Sync + Debug + 'static> From<ErrorWith<T>> for io::Error {
fn from(error: ErrorWith<T>) -> Self {
io::Error::new(match error.code {
ReturnCode::INCOMPLETE | ReturnCode::TRY_AGAIN => io::ErrorKind::Interrupted,
ReturnCode::BAD_ITEM | ReturnCode::USER_UNKNOWN => io::ErrorKind::NotFound,
ReturnCode::CRED_INSUFFICIENT | ReturnCode::PERM_DENIED => io::ErrorKind::PermissionDenied,
_ => io::ErrorKind::Other
}, Box::new(error))
}
}