]> git.proxmox.com Git - rustc.git/blob - vendor/cargo_metadata/src/errors.rs
New upstream version 1.44.1+dfsg1
[rustc.git] / vendor / cargo_metadata / src / errors.rs
1 use std::fmt;
2 use std::io;
3 use std::str::Utf8Error;
4 use std::string::FromUtf8Error;
5
6 /// Custom result type for `cargo_metadata::Error`
7 pub type Result<T> = ::std::result::Result<T, Error>;
8
9 /// Error returned when executing/parsing `cargo metadata` fails.
10 ///
11 /// # Note about Backtraces
12 ///
13 /// This error type does not contain backtraces, but each error variant
14 /// comes from _one_ specific place, so it's not really needed for the
15 /// inside of this crate. If you need a backtrace down to, but not inside
16 /// of, a failed call of `cargo_metadata` you can do one of multiple thinks:
17 ///
18 /// 1. Convert it to a `failure::Error` (possible using the `?` operator),
19 /// which is similar to a `Box<::std::error::Error + 'static + Send + Sync>`.
20 /// 2. Have appropriate variants in your own error type. E.g. you could wrap
21 /// a `failure::Context<Error>` or add a `failure::Backtrace` field (which
22 /// is empty if `RUST_BACKTRACE` is not set, so it's simple to use).
23 /// 3. You still can place a failure based error into a `error_chain` if you
24 /// really want to. (Either through foreign_links or by making it a field
25 /// value of a `ErrorKind` variant).
26 ///
27 #[derive(Debug)]
28 pub enum Error {
29 /// Error during execution of `cargo metadata`
30 CargoMetadata {
31 /// stderr returned by the `cargo metadata` command
32 stderr: String,
33 },
34
35 /// IO Error during execution of `cargo metadata`
36 Io(io::Error),
37
38 /// Output of `cargo metadata` was not valid utf8
39 Utf8(Utf8Error),
40
41 /// Error output of `cargo metadata` was not valid utf8
42 ErrUtf8(FromUtf8Error),
43
44 /// Deserialization error (structure of json did not match expected structure)
45 Json(::serde_json::Error),
46
47 /// The output did not contain any json
48 NoJson,
49 }
50
51 impl From<io::Error> for Error {
52 fn from(v: io::Error) -> Self {
53 Error::Io(v)
54 }
55 }
56
57 impl From<Utf8Error> for Error {
58 fn from(v: Utf8Error) -> Self {
59 Error::Utf8(v)
60 }
61 }
62
63 impl From<FromUtf8Error> for Error {
64 fn from(v: FromUtf8Error) -> Self {
65 Error::ErrUtf8(v)
66 }
67 }
68
69 impl From<::serde_json::Error> for Error {
70 fn from(v: ::serde_json::Error) -> Self {
71 Error::Json(v)
72 }
73 }
74
75 impl fmt::Display for Error {
76 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77 match self {
78 Error::CargoMetadata { stderr } => {
79 write!(f, "Error during execution of `cargo metadata`: {}", stderr)
80 }
81 Error::Io(err) => write!(f, "{}", err),
82 Error::Utf8(err) => write!(f, "Cannot convert the stdout of `cargo metadata`: {}", err),
83 Error::ErrUtf8(err) => {
84 write!(f, "Cannot convert the stderr of `cargo metadata`: {}", err)
85 }
86 Error::Json(err) => write!(f, "Failed to interpret `cargo metadata`'s json: {}", err),
87 Error::NoJson => write!(
88 f,
89 "Could not find any json in the output of `cargo metadata`"
90 ),
91 }
92 }
93 }
94
95 impl ::std::error::Error for Error {
96 fn source(&self) -> Option<&(::std::error::Error + 'static)> {
97 match self {
98 Error::CargoMetadata { .. } => None,
99 Error::Io(err) => Some(err),
100 Error::Utf8(err) => Some(err),
101 Error::ErrUtf8(err) => Some(err),
102 Error::Json(err) => Some(err),
103 Error::NoJson => None,
104 }
105 }
106 }