1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | |
5 | use std::error; |
6 | use std::fmt; |
7 | |
8 | pub use crate::bindgen::cargo::cargo_expand::Error as CargoExpandError; |
9 | pub use crate::bindgen::cargo::cargo_metadata::Error as CargoMetadataError; |
10 | pub use crate::bindgen::cargo::cargo_toml::Error as CargoTomlError; |
11 | pub use syn::parse::Error as ParseError; |
12 | |
13 | #[derive (Debug)] |
14 | #[allow (clippy::enum_variant_names)] |
15 | pub enum Error { |
16 | CargoMetadata(String, CargoMetadataError), |
17 | CargoToml(String, CargoTomlError), |
18 | CargoExpand(String, CargoExpandError), |
19 | ParseSyntaxError { |
20 | crate_name: String, |
21 | src_path: String, |
22 | error: ParseError, |
23 | }, |
24 | ParseCannotOpenFile { |
25 | crate_name: String, |
26 | src_path: String, |
27 | }, |
28 | } |
29 | |
30 | impl fmt::Display for Error { |
31 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
32 | match *self { |
33 | Error::CargoMetadata(ref path, ref error) => write!( |
34 | f, |
35 | "Couldn't execute `cargo metadata` with manifest {:?}: {:?}" , |
36 | path, error |
37 | ), |
38 | Error::CargoToml(ref path, ref error) => { |
39 | write!(f, "Couldn't load manifest file {:?}: {:?}" , path, error) |
40 | } |
41 | Error::CargoExpand(ref crate_name, ref error) => write!( |
42 | f, |
43 | "Parsing crate ` {}`: couldn't run `cargo rustc -Zunpretty=expanded`: {:?}" , |
44 | crate_name, error |
45 | ), |
46 | Error::ParseSyntaxError { |
47 | ref crate_name, |
48 | ref src_path, |
49 | ref error, |
50 | } => { |
51 | write!( |
52 | f, |
53 | "Parsing crate ` {}`:` {}`: \n{:?}" , |
54 | crate_name, src_path, error |
55 | )?; |
56 | |
57 | if !src_path.is_empty() { |
58 | write!( |
59 | f, |
60 | " \nTry running `rustc -Z parse-only {}` to see a nicer error message" , |
61 | src_path, |
62 | )? |
63 | } |
64 | Ok(()) |
65 | } |
66 | Error::ParseCannotOpenFile { |
67 | ref crate_name, |
68 | ref src_path, |
69 | } => write!( |
70 | f, |
71 | "Parsing crate ` {}`: cannot open file ` {}`." , |
72 | crate_name, src_path |
73 | ), |
74 | } |
75 | } |
76 | } |
77 | |
78 | impl error::Error for Error { |
79 | fn source(&self) -> Option<&(dyn error::Error + 'static)> { |
80 | match self { |
81 | Error::CargoMetadata(_, ref error: &Error) => Some(error), |
82 | Error::CargoToml(_, ref error: &Error) => Some(error), |
83 | Error::CargoExpand(_, ref error: &Error) => Some(error), |
84 | Error::ParseSyntaxError { ref error: &Error, .. } => Some(error), |
85 | Error::ParseCannotOpenFile { .. } => None, |
86 | } |
87 | } |
88 | } |
89 | |