playdate_bindgen/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use std::env::VarError;
use std::io::Error as IoError;

use bindgen::BindgenError;
use semver::Error as SemverError;
use utils::toolchain::gcc::err::Error as GccError;


#[derive(Debug)]
pub enum Error {
	Bindgen(BindgenError),
	Io(IoError),
	Env {
		err: VarError,
		ctx: &'static str,
	},
	Semver(SemverError),

	Gcc(GccError),

	#[cfg(feature = "extra-codegen")]
	Syn(syn::Error),
}


impl From<BindgenError> for Error {
	fn from(err: BindgenError) -> Self { Self::Bindgen(err) }
}

impl From<IoError> for Error {
	fn from(err: IoError) -> Self { Self::Io(err) }
}

impl From<SemverError> for Error {
	fn from(err: SemverError) -> Self { Self::Semver(err) }
}

impl From<GccError> for Error {
	fn from(err: GccError) -> Self { Self::Gcc(err) }
}

impl From<VarError> for Error {
	fn from(err: VarError) -> Self {
		Self::Env { err,
		            ctx: Default::default() }
	}
}

#[cfg(feature = "extra-codegen")]
impl From<syn::Error> for Error {
	fn from(err: syn::Error) -> Self { Self::Syn(err) }
}


impl std::fmt::Display for Error {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Error::Bindgen(err) => err.fmt(f),
			Error::Io(err) => err.fmt(f),
			Error::Env { err, ctx } => write!(f, "{err}: {ctx}"),
			Error::Semver(err) => err.fmt(f),
			Error::Gcc(err) => err.fmt(f),
			#[cfg(feature = "extra-codegen")]
			Error::Syn(err) => err.fmt(f),
		}
	}
}


impl std::error::Error for Error {
	fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
		match self {
			Error::Bindgen(err) => Some(err),
			Error::Io(err) => Some(err),
			Error::Env { err, .. } => Some(err),
			Error::Semver(err) => Some(err),
			Error::Gcc(err) => Some(err),
			#[cfg(feature = "extra-codegen")]
			Error::Syn(err) => Some(err),
		}
	}
}