playdate_bindgen/
error.rs

1use std::env::VarError;
2use std::io::Error as IoError;
3
4use bindgen::BindgenError;
5use semver::Error as SemverError;
6use utils::toolchain::gcc::err::Error as GccError;
7
8
9#[derive(Debug)]
10pub enum Error {
11	Bindgen(BindgenError),
12	Io(IoError),
13	Env {
14		err: VarError,
15		ctx: &'static str,
16	},
17	Semver(SemverError),
18
19	Gcc(GccError),
20
21	#[cfg(feature = "extra-codegen")]
22	Syn(syn::Error),
23}
24
25
26impl From<BindgenError> for Error {
27	fn from(err: BindgenError) -> Self { Self::Bindgen(err) }
28}
29
30impl From<IoError> for Error {
31	fn from(err: IoError) -> Self { Self::Io(err) }
32}
33
34impl From<SemverError> for Error {
35	fn from(err: SemverError) -> Self { Self::Semver(err) }
36}
37
38impl From<GccError> for Error {
39	fn from(err: GccError) -> Self { Self::Gcc(err) }
40}
41
42impl From<VarError> for Error {
43	fn from(err: VarError) -> Self {
44		Self::Env { err,
45		            ctx: Default::default() }
46	}
47}
48
49#[cfg(feature = "extra-codegen")]
50impl From<syn::Error> for Error {
51	fn from(err: syn::Error) -> Self { Self::Syn(err) }
52}
53
54
55impl std::fmt::Display for Error {
56	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57		match self {
58			Error::Bindgen(err) => err.fmt(f),
59			Error::Io(err) => err.fmt(f),
60			Error::Env { err, ctx } => write!(f, "{err}: {ctx}"),
61			Error::Semver(err) => err.fmt(f),
62			Error::Gcc(err) => err.fmt(f),
63			#[cfg(feature = "extra-codegen")]
64			Error::Syn(err) => err.fmt(f),
65		}
66	}
67}
68
69
70impl std::error::Error for Error {
71	fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
72		match self {
73			Error::Bindgen(err) => Some(err),
74			Error::Io(err) => Some(err),
75			Error::Env { err, .. } => Some(err),
76			Error::Semver(err) => Some(err),
77			Error::Gcc(err) => Some(err),
78			#[cfg(feature = "extra-codegen")]
79			Error::Syn(err) => Some(err),
80		}
81	}
82}