1 | use glob::{GlobError, PatternError}; |
2 | use std::ffi::OsString; |
3 | use std::fmt::{self, Display}; |
4 | use std::io; |
5 | use std::path::PathBuf; |
6 | |
7 | #[derive(Debug)] |
8 | pub enum Error { |
9 | Cargo(io::Error), |
10 | CargoFail, |
11 | GetManifest(PathBuf, Box<Error>), |
12 | Glob(GlobError), |
13 | Io(io::Error), |
14 | Metadata(serde_json::Error), |
15 | Mismatch, |
16 | NoWorkspaceManifest, |
17 | Open(PathBuf, io::Error), |
18 | Pattern(PatternError), |
19 | ProjectDir, |
20 | ReadStderr(io::Error), |
21 | RunFailed, |
22 | ShouldNotHaveCompiled, |
23 | Toml(basic_toml::Error), |
24 | UpdateVar(OsString), |
25 | WriteStderr(io::Error), |
26 | } |
27 | |
28 | pub type Result<T> = std::result::Result<T, Error>; |
29 | |
30 | impl Display for Error { |
31 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
32 | use self::Error::*; |
33 | |
34 | match self { |
35 | Cargo(e) => write!(f, "failed to execute cargo: {}" , e), |
36 | CargoFail => write!(f, "cargo reported an error" ), |
37 | GetManifest(path, e) => write!(f, "failed to read manifest {}: {}" , path.display(), e), |
38 | Glob(e) => write!(f, "{}" , e), |
39 | Io(e) => write!(f, "{}" , e), |
40 | Metadata(e) => write!(f, "failed to read cargo metadata: {}" , e), |
41 | Mismatch => write!(f, "compiler error does not match expected error" ), |
42 | NoWorkspaceManifest => write!(f, "Cargo.toml uses edition.workspace=true, but no edition found in workspace's manifest" ), |
43 | Open(path, e) => write!(f, "{}: {}" , path.display(), e), |
44 | Pattern(e) => write!(f, "{}" , e), |
45 | ProjectDir => write!(f, "failed to determine name of project dir" ), |
46 | ReadStderr(e) => write!(f, "failed to read stderr file: {}" , e), |
47 | RunFailed => write!(f, "execution of the test case was unsuccessful" ), |
48 | ShouldNotHaveCompiled => { |
49 | write!(f, "expected test case to fail to compile, but it succeeded" ) |
50 | } |
51 | Toml(e) => write!(f, "{}" , e), |
52 | UpdateVar(var) => write!( |
53 | f, |
54 | "unrecognized value of TRYBUILD: {:?}" , |
55 | var.to_string_lossy(), |
56 | ), |
57 | WriteStderr(e) => write!(f, "failed to write stderr file: {}" , e), |
58 | } |
59 | } |
60 | } |
61 | |
62 | impl Error { |
63 | pub fn already_printed(&self) -> bool { |
64 | use self::Error::*; |
65 | |
66 | matches!( |
67 | self, |
68 | CargoFail | Mismatch | RunFailed | ShouldNotHaveCompiled |
69 | ) |
70 | } |
71 | } |
72 | |
73 | impl From<GlobError> for Error { |
74 | fn from(err: GlobError) -> Self { |
75 | Error::Glob(err) |
76 | } |
77 | } |
78 | |
79 | impl From<PatternError> for Error { |
80 | fn from(err: PatternError) -> Self { |
81 | Error::Pattern(err) |
82 | } |
83 | } |
84 | |
85 | impl From<io::Error> for Error { |
86 | fn from(err: io::Error) -> Self { |
87 | Error::Io(err) |
88 | } |
89 | } |
90 | |
91 | impl From<basic_toml::Error> for Error { |
92 | fn from(err: basic_toml::Error) -> Self { |
93 | Error::Toml(err) |
94 | } |
95 | } |
96 | |