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
80
81
82
83
84
85
86
87
88
use std::error;
use std::fmt;
use std::io;
use std::path::PathBuf;

use cargo_shim;

#[derive(Debug)]
pub enum Error {
    ConfigurationError( String ),
    EnvironmentError( String ),
    RuntimeError( String, Box< error::Error > ),
    BuildError,
    NoDefaultPackage,
    EmscriptenNotAvailable,
    CargoShimError( cargo_shim::Error ),
    CannotLoadFile( PathBuf, io::Error ),
    CannotRemoveDirectory( PathBuf, io::Error ),
    CannotRemoveFile( PathBuf, io::Error ),
    CannotCreateFile( PathBuf, io::Error ),
    CannotWriteToFile( PathBuf, io::Error ),
    CannotCopyFile( PathBuf, PathBuf, io::Error ),
    Other( Box< error::Error > )
}

impl error::Error for Error {
    fn description( &self ) -> &str {
        match *self {
            Error::ConfigurationError( ref message ) => &message,
            Error::EnvironmentError( ref message ) => &message,
            Error::RuntimeError( ref message, _ ) => &message,
            Error::BuildError => "build failed",
            Error::NoDefaultPackage => "no default package; you can specify a crate to use with the `-p` argument",
            Error::EmscriptenNotAvailable => "prepackaged Emscripten is not available for this platform",
            Error::CargoShimError( ref error ) => error.description(),
            Error::CannotLoadFile( .. ) => "cannot load file",
            Error::CannotRemoveDirectory( .. ) => "cannot remove directory",
            Error::CannotRemoveFile( .. ) => "cannot remove file",
            Error::CannotCreateFile( .. ) => "cannot create file",
            Error::CannotWriteToFile( .. ) => "cannot write to file",
            Error::CannotCopyFile( .. ) => "cannot copy file",
            Error::Other( ref error ) => error.description()
        }
    }
}

impl From< cargo_shim::Error > for Error {
    fn from( err: cargo_shim::Error ) -> Self {
        Error::CargoShimError( err )
    }
}

impl From< Box< error::Error > > for Error {
    fn from( err: Box< error::Error > ) -> Self {
        Error::Other( err )
    }
}

impl From< String > for Error {
    fn from( err: String ) -> Self {
        Error::Other( err.into() )
    }
}

impl< 'a > From< &'a str > for Error {
    fn from( err: &'a str ) -> Self {
        Error::Other( err.into() )
    }
}

impl fmt::Display for Error {
    fn fmt( &self, formatter: &mut fmt::Formatter ) -> fmt::Result {
        use std::error::Error as StdError;
        match self {
            &Error::RuntimeError( _, ref inner ) => write!( formatter, "{}: {}", self.description(), inner ),
            &Error::CargoShimError( cargo_shim::Error::CargoFailed( ref message ) ) => write!( formatter, "{}", message ),
            &Error::CargoShimError( ref inner ) => write!( formatter, "{}", inner ),
            &Error::CannotLoadFile( ref path, ref inner ) => write!( formatter, "cannot load file {:?}: {}", path, inner ),
            &Error::CannotRemoveDirectory( ref path, ref inner ) => write!( formatter, "cannot remove directory {:?}: {}", path, inner ),
            &Error::CannotRemoveFile( ref path, ref inner ) => write!( formatter, "cannot remove file {:?}: {}", path, inner ),
            &Error::CannotCreateFile( ref path, ref inner ) => write!( formatter, "cannot create file {:?}: {}", path, inner ),
            &Error::CannotWriteToFile( ref path, ref inner ) => write!( formatter, "cannot write to file {:?}: {}", path, inner ),
            &Error::CannotCopyFile( ref src_path, ref dst_path, ref inner ) => write!( formatter, "cannot copy file from {:?} to {:?}: {}", src_path, dst_path, inner ),
            &Error::Other( ref inner ) => write!( formatter, "{}", inner ),
            _ => write!( formatter, "{}", self.description() )
        }
    }
}