trybuild_internals_api/
error.rs

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