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
|
use std::ffi;
use std::ffi::CString;
use std::fmt;
use std::str;
use libc::c_char;
use regex;
#[derive(Debug)]
pub struct Error {
message: Option<CString>,
kind: ErrorKind,
}
#[derive(Debug)]
pub enum ErrorKind {
None,
Str(str::Utf8Error),
Regex(regex::Error),
Nul(ffi::NulError),
}
impl Error {
pub fn new(kind: ErrorKind) -> Error {
Error { message: None, kind: kind }
}
pub fn is_err(&self) -> bool {
match self.kind {
ErrorKind::None => false,
ErrorKind::Str(_) | ErrorKind::Regex(_) | ErrorKind::Nul(_) => {
true
}
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.kind {
ErrorKind::None => write!(f, "no error"),
ErrorKind::Str(ref e) => e.fmt(f),
ErrorKind::Regex(ref e) => e.fmt(f),
ErrorKind::Nul(ref e) => e.fmt(f),
}
}
}
ffi_fn! {
fn rure_error_new() -> *mut Error {
Box::into_raw(Box::new(Error::new(ErrorKind::None)))
}
}
ffi_fn! {
fn rure_error_free(err: *mut Error) {
unsafe { drop(Box::from_raw(err)); }
}
}
ffi_fn! {
fn rure_error_message(err: *mut Error) -> *const c_char {
let err = unsafe { &mut *err };
let cmsg = match CString::new(format!("{}", err)) {
Ok(msg) => msg,
Err(err) => {
// I guess this can probably happen if the regex itself has a
// NUL, and that NUL re-occurs in the context presented by the
// error message. In this case, just show as much as we can.
let nul = err.nul_position();
let msg = err.into_vec();
CString::new(msg[0..nul].to_owned()).unwrap()
}
};
let p = cmsg.as_ptr();
err.message = Some(cmsg);
p
}
}
|