playdate_build/metadata/
error.rs

1use std::io::Error as IoError;
2
3#[derive(Debug)]
4pub enum Error {
5	Io(IoError),
6	Err(&'static str),
7	#[cfg(feature = "serde_json")]
8	Json(serde_json::error::Error),
9	#[cfg(feature = "toml")]
10	Toml(toml::de::Error),
11}
12
13impl From<&'static str> for Error {
14	fn from(value: &'static str) -> Self { Self::Err(value) }
15}
16
17impl From<IoError> for Error {
18	fn from(err: IoError) -> Self { Self::Io(err) }
19}
20
21#[cfg(feature = "serde_json")]
22impl From<serde_json::error::Error> for Error {
23	fn from(err: serde_json::error::Error) -> Self { Self::Json(err) }
24}
25
26#[cfg(feature = "toml")]
27impl From<toml::de::Error> for Error {
28	fn from(err: toml::de::Error) -> Self { Self::Toml(err) }
29}
30
31impl std::fmt::Display for Error {
32	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33		match self {
34			Error::Io(err) => err.fmt(f),
35			Error::Err(err) => err.fmt(f),
36			#[cfg(feature = "serde_json")]
37			Error::Json(err) => err.fmt(f),
38			#[cfg(feature = "toml")]
39			Error::Toml(err) => err.fmt(f),
40		}
41	}
42}
43
44impl std::error::Error for Error {
45	fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
46		match self {
47			Error::Err(_) => None,
48			Error::Io(err) => Some(err),
49			#[cfg(feature = "serde_json")]
50			Error::Json(err) => Some(err),
51			#[cfg(feature = "toml")]
52			Error::Toml(err) => Some(err),
53		}
54	}
55}