#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(all(feature = "strum"), derive(strum::EnumIter))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
#[cfg_attr(feature = "c_compatible", repr(C))]
pub enum WindowCreationError {
BackendFailedToLoad,
TransparencyRequiresBorderlessProperty,
OsFailed,
Misc(String),
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "c_compatible", repr(C))]
pub enum WindowUpdateError {
BufferInvalidSize {
width: usize,
stride: usize,
height: usize,
expected: usize,
gotten: usize,
},
Misc(String),
}
impl From<crate::platform::windowing::errors::WindowUpdateError>
for crate::platform::windowing::errors::WindowError
{
fn from(
value: crate::platform::windowing::errors::WindowUpdateError,
) -> Self {
Self::FailedToUpdateWindow(value)
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "c_compatible", repr(C))]
pub enum WindowError {
IncorrectSize((usize, usize)),
OsNotSupported,
FailedToOpenWindow(WindowCreationError),
FailedToUpdateWindow(WindowUpdateError),
NotImplemented,
DuplicateWindow,
FileAccessNotPossible {
path: String,
},
UnableToLoadIcon,
UnableToLoadCursor,
Misc(String),
}
impl std::fmt::Display for WindowCreationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::BackendFailedToLoad => {
write!(f, "Backend failed to load before opening the window")
}
Self::TransparencyRequiresBorderlessProperty => {
write!(f, "Transparency feature requires the borderless property on Windows")
}
Self::OsFailed => {
write!(f, "Operating system failed to create a window")
}
Self::Misc(msg) => {
write!(f, "Window creation error: {msg}")
}
}
}
}
impl std::error::Error for WindowCreationError {}
impl std::fmt::Display for WindowUpdateError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::BufferInvalidSize {
width,
stride,
height,
expected,
gotten,
} => {
write!(
f,
"Buffer has invalid size: expected {expected} bytes for {width}x{height} (stride: {stride}), got {gotten} bytes"
)
}
Self::Misc(msg) => {
write!(f, "Window update error: {msg}")
}
}
}
}
impl std::error::Error for WindowUpdateError {}
impl std::fmt::Display for WindowError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::IncorrectSize((width, height)) => {
write!(f, "Incorrect buffer size: expected {width}x{height}")
}
Self::OsNotSupported => {
write!(
f,
"Window creation is not supported on this operating system"
)
}
Self::FailedToOpenWindow(err) => {
write!(f, "Failed to open window: {err}")
}
Self::FailedToUpdateWindow(err) => {
write!(f, "Failed to update window: {err}")
}
Self::NotImplemented => {
write!(f, "Feature is not yet implemented")
}
Self::DuplicateWindow => {
write!(f, "A window already exists")
}
Self::FileAccessNotPossible {
path,
} => {
write!(f, "Unable to access file: {path}")
}
Self::UnableToLoadIcon => {
write!(f, "Unable to load application icon")
}
Self::UnableToLoadCursor => {
write!(f, "Unable to load cursor")
}
Self::Misc(msg) => {
write!(f, "Window error: {msg}")
}
}
}
}
impl std::error::Error for WindowError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::FailedToOpenWindow(err) => Some(err),
Self::FailedToUpdateWindow(err) => Some(err),
_ => None,
}
}
}