playdate_graphics/
error.rs

1use core::fmt;
2use sys::ffi::{CString, CStr};
3use crate::alloc::borrow::ToOwned;
4
5
6pub type ApiError = sys::error::Error<self::Error>;
7
8
9#[derive(Debug)]
10pub enum Error {
11	/// Causes when loading graphics from path fails.
12	/// This occurs when file does not exist or invalid format.
13	Fs(fs::error::Error),
14
15	/// Causes when allocation failed and/or null-ptr returned.
16	Alloc,
17
18	/// Mask must be the same size as the target bitmap.
19	InvalidMask,
20
21	/// Font error.
22	/// This occurs when char or page not found.
23	Font,
24
25	/// Video error.
26	Video(CString),
27
28	/// Unknown error.
29	Unknown,
30}
31
32impl fmt::Display for Error {
33	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34		match &self {
35			Error::Fs(err) => err.fmt(f),
36			Error::Alloc => write!(f, "Allocation failed"),
37			Error::Font => write!(f, "Font error"),
38			Error::InvalidMask => write!(f, "Mask must be the same size as the target bitmap"),
39			Error::Video(cs) => {
40				match cs.to_str() {
41					Ok(err) => err.fmt(f),
42					Err(_) => f.write_fmt(format_args!("Video error: {cs:?}")),
43				}
44			},
45			Error::Unknown => write!(f, "Unknown error"),
46		}
47	}
48}
49
50impl From<fs::error::Error> for Error {
51	fn from(err: fs::error::Error) -> Self { Error::Fs(err) }
52}
53
54
55impl Into<ApiError> for Error {
56	fn into(self) -> ApiError { ApiError::Api(self) }
57}
58
59
60impl core::error::Error for Error {}
61
62
63impl Error {
64	pub(crate) fn video_from(c: &CStr) -> Self { Self::Video(c.to_owned()) }
65}