2022-12-20 08:43:26 -07:00
|
|
|
use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
|
2022-12-22 12:35:18 -08:00
|
|
|
use std::error::Error;
|
2023-04-06 11:09:47 -07:00
|
|
|
use std::fmt;
|
2023-04-06 00:30:59 -07:00
|
|
|
use std::num::NonZeroU32;
|
2022-01-16 08:59:29 -06:00
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
2023-04-06 00:30:59 -07:00
|
|
|
#[allow(missing_docs)] // TODO
|
2022-12-21 17:26:09 -08:00
|
|
|
#[non_exhaustive]
|
2022-12-27 12:23:27 -08:00
|
|
|
pub enum SoftBufferError {
|
2023-01-06 13:08:17 -08:00
|
|
|
#[error(
|
|
|
|
|
"The provided display returned an unsupported platform: {human_readable_display_platform_name}."
|
|
|
|
|
)]
|
|
|
|
|
UnsupportedDisplayPlatform {
|
|
|
|
|
human_readable_display_platform_name: &'static str,
|
|
|
|
|
display_handle: RawDisplayHandle,
|
|
|
|
|
},
|
2022-01-16 08:59:29 -06:00
|
|
|
#[error(
|
2022-08-24 00:16:20 -05:00
|
|
|
"The provided window returned an unsupported platform: {human_readable_window_platform_name}, {human_readable_display_platform_name}."
|
2022-01-16 08:59:29 -06:00
|
|
|
)]
|
2023-01-06 13:08:17 -08:00
|
|
|
UnsupportedWindowPlatform {
|
2022-08-24 00:16:20 -05:00
|
|
|
human_readable_window_platform_name: &'static str,
|
|
|
|
|
human_readable_display_platform_name: &'static str,
|
|
|
|
|
window_handle: RawWindowHandle,
|
2022-01-16 08:59:29 -06:00
|
|
|
},
|
2022-12-21 17:26:09 -08:00
|
|
|
|
|
|
|
|
#[error("The provided window handle is null.")]
|
|
|
|
|
IncompleteWindowHandle,
|
|
|
|
|
|
|
|
|
|
#[error("The provided display handle is null.")]
|
|
|
|
|
IncompleteDisplayHandle,
|
|
|
|
|
|
2023-04-06 00:30:59 -07:00
|
|
|
#[error("Surface size {width}x{height} out of range for backend.")]
|
|
|
|
|
SizeOutOfRange {
|
|
|
|
|
width: NonZeroU32,
|
|
|
|
|
height: NonZeroU32,
|
|
|
|
|
},
|
|
|
|
|
|
2022-01-16 08:59:29 -06:00
|
|
|
#[error("Platform error")]
|
2022-12-22 12:35:18 -08:00
|
|
|
PlatformError(Option<String>, Option<Box<dyn Error>>),
|
2022-01-16 08:59:29 -06:00
|
|
|
}
|
2022-01-19 21:11:20 -06:00
|
|
|
|
2023-04-06 11:09:47 -07:00
|
|
|
/// Convenient wrapper to cast errors into SoftBufferError.
|
|
|
|
|
pub(crate) trait SwResultExt<T> {
|
|
|
|
|
fn swbuf_err(self, msg: impl Into<String>) -> Result<T, SoftBufferError>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T, E: std::error::Error + 'static> SwResultExt<T> for Result<T, E> {
|
|
|
|
|
fn swbuf_err(self, msg: impl Into<String>) -> Result<T, SoftBufferError> {
|
|
|
|
|
self.map_err(|e| {
|
|
|
|
|
SoftBufferError::PlatformError(Some(msg.into()), Some(Box::new(LibraryError(e))))
|
|
|
|
|
})
|
2022-01-19 21:11:20 -06:00
|
|
|
}
|
2022-12-20 08:43:26 -07:00
|
|
|
}
|
2023-04-06 11:09:47 -07:00
|
|
|
|
|
|
|
|
impl<T> SwResultExt<T> for Option<T> {
|
|
|
|
|
fn swbuf_err(self, msg: impl Into<String>) -> Result<T, SoftBufferError> {
|
|
|
|
|
self.ok_or_else(|| SoftBufferError::PlatformError(Some(msg.into()), None))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A wrapper around a library error.
|
|
|
|
|
///
|
|
|
|
|
/// This prevents `x11-dl` and `x11rb` from becoming public dependencies, since users cannot downcast
|
|
|
|
|
/// to this type.
|
|
|
|
|
struct LibraryError<E>(E);
|
|
|
|
|
|
|
|
|
|
impl<E: fmt::Debug> fmt::Debug for LibraryError<E> {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
fmt::Debug::fmt(&self.0, f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<E: fmt::Display> fmt::Display for LibraryError<E> {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
fmt::Display::fmt(&self.0, f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<E: fmt::Debug + fmt::Display> std::error::Error for LibraryError<E> {}
|