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
use std::error::Error as StdError;
use std::io;
use std::fmt;
use std::path::PathBuf;


/// Errors that can be return from various operatiors
///
#[derive(Debug)]
pub enum Error {
    /// Loading a library failed
    Load(io::Error),
    /// File copy operation failed
    Copy(io::Error, PathBuf, PathBuf),
    /// Timeout of file copy happend.
    CopyTimeOut(PathBuf, PathBuf),
    /// Failed to find library
    Find(String),
}

impl StdError for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Load(_) => "Unable to load library",
            Error::Copy(_, _, _) => "Unable to copy",
            Error::CopyTimeOut(_, _) => "Unable to copy due to time out",
            Error::Find(_) => "Unable to find",
        }
    }

    fn cause(&self) -> Option<&StdError> {
        match *self {
            Error::Load(ref e) => e.cause(),
            Error::Copy(ref e, _, _) => e.cause(),
            Error::CopyTimeOut(_, _) => None,
            Error::Find(_) => None,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::Load(ref e) => {
                write!(fmt,
                       "{} {}\nDue to: {:?}",
                       self.description(),
                       e.description(),
                       self.cause())
            }
            Error::Copy(ref e, ref src, ref dest) => {
                write!(fmt,
                       "{} {:?} to {:?}\n{}\nDue to: {:?}",
                       self.description(),
                       src,
                       dest,
                       e.description(),
                       self.cause())
            }
            Error::CopyTimeOut(ref src, ref dest) => {
                write!(fmt, "{} {:?} to {:?}", self.description(), src, dest)
            }
            Error::Find(ref name) => write!(fmt, "{} {}", self.description(), name),
        }
    }
}