1 | use std::error; |
2 | use std::fmt; |
3 | |
4 | /// Errors that can occur during code generation. |
5 | #[derive (Clone, Debug, PartialEq, Eq)] |
6 | pub(crate) enum Error { |
7 | /// Tried to generate an opaque blob for a type that did not have a layout. |
8 | NoLayoutForOpaqueBlob, |
9 | |
10 | /// Tried to instantiate an opaque template definition, or a template |
11 | /// definition that is too difficult for us to understand (like a partial |
12 | /// template specialization). |
13 | InstantiationOfOpaqueType, |
14 | |
15 | /// Function ABI is not supported. |
16 | UnsupportedAbi(&'static str), |
17 | |
18 | /// The pointer type size does not match the target's pointer size. |
19 | InvalidPointerSize { |
20 | ty_name: String, |
21 | ty_size: usize, |
22 | ptr_size: usize, |
23 | }, |
24 | } |
25 | |
26 | impl fmt::Display for Error { |
27 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
28 | match self { |
29 | Error::NoLayoutForOpaqueBlob => { |
30 | "Tried to generate an opaque blob, but had no layout." .fmt(f) |
31 | } |
32 | Error::InstantiationOfOpaqueType => { |
33 | "Instantiation of opaque template type or partial template specialization." &str |
34 | .fmt(f) |
35 | } |
36 | Error::UnsupportedAbi(abi: &&str) => { |
37 | write!( |
38 | f, |
39 | " {} ABI is not supported by the configured Rust target." , |
40 | abi |
41 | ) |
42 | } |
43 | Error::InvalidPointerSize { ty_name: &String, ty_size: &usize, ptr_size: &usize } => { |
44 | write!(f, "The {} pointer type has size {} but the current target's pointer size is {}." , ty_name, ty_size, ptr_size) |
45 | } |
46 | } |
47 | } |
48 | } |
49 | |
50 | impl error::Error for Error {} |
51 | |
52 | /// A `Result` of `T` or an error of `bindgen::codegen::error::Error`. |
53 | pub(crate) type Result<T> = ::std::result::Result<T, Error>; |
54 | |