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	Internal(&'static str),
25}
26
27
28impl From<BindgenError> for Error {
29	fn from(err: BindgenError) -> Self { Self::Bindgen(err) }
30}
31
32impl From<IoError> for Error {
33	fn from(err: IoError) -> Self { Self::Io(err) }
34}
35
36impl From<SemverError> for Error {
37	fn from(err: SemverError) -> Self { Self::Semver(err) }
38}
39
40impl From<GccError> for Error {
41	fn from(err: GccError) -> Self { Self::Gcc(err) }
42}
43
44impl From<VarError> for Error {
45	fn from(err: VarError) -> Self {
46		Self::Env { err,
47		            ctx: Default::default() }
48	}
49}
50
51#[cfg(feature = "extra-codegen")]
52impl From<syn::Error> for Error {
53	fn from(err: syn::Error) -> Self { Self::Syn(err) }
54}
55
56
57impl std::fmt::Display for Error {
58	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59		match self {
60			Error::Bindgen(err) => err.fmt(f),
61			Error::Io(err) => err.fmt(f),
62			Error::Env { err, ctx } => write!(f, "{err}: {ctx}"),
63			Error::Semver(err) => err.fmt(f),
64			Error::Gcc(err) => err.fmt(f),
65			#[cfg(feature = "extra-codegen")]
66			Error::Syn(err) => err.fmt(f),
67			Error::Internal(s) => s.fmt(f),
68		}
69	}
70}
71
72
73impl std::error::Error for Error {
74	fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
75		match self {
76			Error::Bindgen(err) => Some(err),
77			Error::Io(err) => Some(err),
78			Error::Env { err, .. } => Some(err),
79			Error::Semver(err) => Some(err),
80			Error::Gcc(err) => Some(err),
81			#[cfg(feature = "extra-codegen")]
82			Error::Syn(err) => Some(err),
83			Error::Internal(_) => None,
84		}
85	}
86}