dlopen/
err.rs

1use std::error::Error as ErrorTrait;
2use std::fmt::{Display, Formatter, Result as FmtResult};
3use std::convert::From;
4use std::ffi::NulError;
5use std::io::Error as IoError;
6
7///This is a library-specific error that is returned by all calls to all APIs.
8#[derive(Debug)]
9pub enum Error {
10    ///Provided string could not be coverted into `std::ffi::CString` because it contained null
11    /// character.
12    NullCharacter(NulError),
13    ///The library could not be opened.
14    OpeningLibraryError(IoError),
15    ///The symbol could not be obtained.
16    SymbolGettingError(IoError),
17    ///Value of the symbol was null.
18    NullSymbol,
19    ///Address could not be matched to a dynamic link library
20    AddrNotMatchingDll(IoError)
21}
22
23impl ErrorTrait for Error {
24    fn description(&self) -> &str {
25        use self::Error::*;
26        match self {
27            &NullCharacter(_) => "String had a null character",
28            &OpeningLibraryError(_) => "Could not open library",
29            &SymbolGettingError(_) => "Could not obtain symbol from the library",
30            &NullSymbol => "The symbol is NULL",
31            &AddrNotMatchingDll(_) => "Address does not match any dynamic link library"
32        }
33    }
34
35    fn cause(&self) -> Option<&ErrorTrait> {
36        use self::Error::*;
37        match self {
38            &NullCharacter(ref val) => Some(val),
39            &OpeningLibraryError(_) | &SymbolGettingError(_) | &NullSymbol | &AddrNotMatchingDll(_)=> {
40                None
41            }
42        }
43    }
44}
45
46impl Display for Error {
47    fn fmt(&self, f: &mut Formatter) -> FmtResult {
48        use self::Error::*;
49        f.write_str(self.description())?;
50        match self {
51            &OpeningLibraryError(ref msg) => {
52                f.write_str(": ")?;
53                msg.fmt(f)
54            }
55            &SymbolGettingError(ref msg) => {
56                f.write_str(": ")?;
57                msg.fmt(f)
58            }
59            &NullSymbol | &NullCharacter(_) | &AddrNotMatchingDll(_)=> Ok(()),
60        }
61    }
62}
63
64impl From<NulError> for Error {
65    fn from(val: NulError) -> Error {
66        Error::NullCharacter(val)
67    }
68}