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
use libc::c_int;
use std::error;
use std::ffi::CStr;
use std::fmt;
pub struct Error(c_int);
impl fmt::Debug for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("Error")
.field("code", &self.0)
.field("message", &self.as_str())
.finish()
}
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self.as_str(), fmt)
}
}
impl error::Error for Error {}
impl Error {
pub(crate) fn new() -> Error {
unsafe { Error(dw_sys::dwfl_errno()) }
}
fn as_str(&self) -> &str {
unsafe {
let s = dw_sys::dwfl_errmsg(self.0);
if s.is_null() {
"unknown error"
} else {
CStr::from_ptr(s).to_str().unwrap()
}
}
}
}