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
//! Our custom error handling.

use std::error;
use std::ffi::NulError;
use std::fmt;
use std::str::Utf8Error;

/// Crate-specific errors.
#[derive(Debug)]
pub enum Error {
    /// Wrapper around `std::ffi::NulError`.
    Nul(NulError),
    /// An error that occurs during a scanifc call.
    ///
    /// Scanifc errors are retrieved from the scanifc library if a ffi function returns nonzero.
    Scanifc(i32, String),
    /// Wrapper around `std::str::Utf8Error`.
    Utf8(Utf8Error),
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Nul(ref err) => err.description(),
            Error::Scanifc(..) => "scanifc error",
            Error::Utf8(ref err) => err.description(),
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match *self {
            Error::Nul(ref err) => Some(err),
            Error::Utf8(ref err) => Some(err),
            _ => None,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::Nul(ref err) => write!(f, "Nul error: {}", err),
            Error::Scanifc(n, ref s) => write!(f, "Scanlib error {}: {}", n, s),
            Error::Utf8(ref err) => write!(f, "Utf8 error: {}", err),
        }
    }
}

impl From<NulError> for Error {
    fn from(err: NulError) -> Error {
        Error::Nul(err)
    }
}

impl From<Utf8Error> for Error {
    fn from(err: Utf8Error) -> Error {
        Error::Utf8(err)
    }
}