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
use std::error::Error as ErrorTrait;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::convert::From;
use std::ffi::NulError;
use std::io::Error as IoError;
#[derive(Debug)]
pub enum Error {
NullCharacter(NulError),
OpeningLibraryError(IoError),
SymbolGettingError(IoError),
NullSymbol,
}
impl ErrorTrait for Error {
fn description(&self) -> &str {
match self {
&Error::NullCharacter(_) => "String had a null character",
&Error::OpeningLibraryError(_) => "Could not open library",
&Error::SymbolGettingError(_) => "Could not obtain symbol from the library",
&Error::NullSymbol => "The symbol is NULL",
}
}
fn cause(&self) -> Option<&ErrorTrait> {
match self {
&Error::NullCharacter(ref val) => Some(val),
&Error::OpeningLibraryError(_) | &Error::SymbolGettingError(_) | &Error::NullSymbol => {
None
}
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
f.write_str(self.description())?;
match self {
&Error::OpeningLibraryError(ref msg) => {
f.write_str(": ")?;
msg.fmt(f)
}
&Error::SymbolGettingError(ref msg) => {
f.write_str(": ")?;
msg.fmt(f)
}
&Error::NullSymbol | &Error::NullCharacter(_) => Ok(()),
}
}
}
impl From<NulError> for Error {
fn from(val: NulError) -> Error {
Error::NullCharacter(val)
}
}