1use std::error;
2use std::fmt;
3use std::io;
4use std::path::PathBuf;
5
6use cargo_shim;
7
8#[derive(Debug)]
9pub enum Error {
10 ConfigurationError( String ),
11 EnvironmentError( String ),
12 RuntimeError( String, Box< error::Error > ),
13 BuildError,
14 NoDefaultPackage,
15 EmscriptenNotAvailable,
16 CargoShimError( cargo_shim::Error ),
17 CannotLoadFile( PathBuf, io::Error ),
18 CannotRemoveDirectory( PathBuf, io::Error ),
19 CannotRemoveFile( PathBuf, io::Error ),
20 CannotCreateFile( PathBuf, io::Error ),
21 CannotWriteToFile( PathBuf, io::Error ),
22 CannotCopyFile( PathBuf, PathBuf, io::Error ),
23 Other( Box< error::Error > )
24}
25
26impl error::Error for Error {
27 fn description( &self ) -> &str {
28 match *self {
29 Error::ConfigurationError( ref message ) => &message,
30 Error::EnvironmentError( ref message ) => &message,
31 Error::RuntimeError( ref message, _ ) => &message,
32 Error::BuildError => "build failed",
33 Error::NoDefaultPackage => "no default package; you can specify a crate to use with the `-p` argument",
34 Error::EmscriptenNotAvailable => "prepackaged Emscripten is not available for this platform",
35 Error::CargoShimError( ref error ) => error.description(),
36 Error::CannotLoadFile( .. ) => "cannot load file",
37 Error::CannotRemoveDirectory( .. ) => "cannot remove directory",
38 Error::CannotRemoveFile( .. ) => "cannot remove file",
39 Error::CannotCreateFile( .. ) => "cannot create file",
40 Error::CannotWriteToFile( .. ) => "cannot write to file",
41 Error::CannotCopyFile( .. ) => "cannot copy file",
42 Error::Other( ref error ) => error.description()
43 }
44 }
45}
46
47impl From< cargo_shim::Error > for Error {
48 fn from( err: cargo_shim::Error ) -> Self {
49 Error::CargoShimError( err )
50 }
51}
52
53impl From< Box< error::Error > > for Error {
54 fn from( err: Box< error::Error > ) -> Self {
55 Error::Other( err )
56 }
57}
58
59impl From< String > for Error {
60 fn from( err: String ) -> Self {
61 Error::Other( err.into() )
62 }
63}
64
65impl< 'a > From< &'a str > for Error {
66 fn from( err: &'a str ) -> Self {
67 Error::Other( err.into() )
68 }
69}
70
71impl fmt::Display for Error {
72 fn fmt( &self, formatter: &mut fmt::Formatter ) -> fmt::Result {
73 use std::error::Error as StdError;
74 match self {
75 &Error::RuntimeError( _, ref inner ) => write!( formatter, "{}: {}", self.description(), inner ),
76 &Error::CargoShimError( cargo_shim::Error::CargoFailed( ref message ) ) => write!( formatter, "{}", message ),
77 &Error::CargoShimError( ref inner ) => write!( formatter, "{}", inner ),
78 &Error::CannotLoadFile( ref path, ref inner ) => write!( formatter, "cannot load file {:?}: {}", path, inner ),
79 &Error::CannotRemoveDirectory( ref path, ref inner ) => write!( formatter, "cannot remove directory {:?}: {}", path, inner ),
80 &Error::CannotRemoveFile( ref path, ref inner ) => write!( formatter, "cannot remove file {:?}: {}", path, inner ),
81 &Error::CannotCreateFile( ref path, ref inner ) => write!( formatter, "cannot create file {:?}: {}", path, inner ),
82 &Error::CannotWriteToFile( ref path, ref inner ) => write!( formatter, "cannot write to file {:?}: {}", path, inner ),
83 &Error::CannotCopyFile( ref src_path, ref dst_path, ref inner ) => write!( formatter, "cannot copy file from {:?} to {:?}: {}", src_path, dst_path, inner ),
84 &Error::Other( ref inner ) => write!( formatter, "{}", inner ),
85 _ => write!( formatter, "{}", self.description() )
86 }
87 }
88}