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
use core::fmt;
use sys::ffi::{CString, CStr};
use crate::alloc::borrow::ToOwned;


pub type ApiError = sys::error::Error<self::Error>;


#[derive(Debug)]
pub enum Error {
	/// Causes when loading graphics from path fails.
	/// This occurs when file does not exist or invalid format.
	Fs(fs::error::Error),

	/// Causes when allocation failed and/or null-ptr returned.
	Alloc,

	/// Mask must be the same size as the target bitmap.
	InvalidMask,

	/// Font error.
	/// This occurs when char or page not found.
	Font,

	/// Video error.
	Video(CString),

	/// Unknown error.
	Unknown,
}

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match &self {
			Error::Fs(err) => err.fmt(f),
			Error::Alloc => write!(f, "Allocation failed"),
			Error::Font => write!(f, "Font error"),
			Error::InvalidMask => write!(f, "Mask must be the same size as the target bitmap"),
			Error::Video(cs) => {
				match cs.to_str() {
					Ok(err) => err.fmt(f),
					Err(_) => f.write_fmt(format_args!("Video error: {cs:?}")),
				}
			},
			Error::Unknown => write!(f, "Unknown error"),
		}
	}
}

impl From<fs::error::Error> for Error {
	fn from(err: fs::error::Error) -> Self { Error::Fs(err) }
}


impl Into<ApiError> for Error {
	fn into(self) -> ApiError { ApiError::Api(self) }
}


impl core::error::Error for Error {}


impl Error {
	pub(crate) fn video_from(c: &CStr) -> Self { Self::Video(c.to_owned()) }
}