softbuffer/src/error.rs

81 lines
2.5 KiB
Rust
Raw Normal View History

use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
2022-12-22 12:35:18 -08:00
use std::error::Error;
use std::fmt;
use std::num::NonZeroU32;
2022-01-16 08:59:29 -06:00
use thiserror::Error;
#[derive(Error, Debug)]
#[allow(missing_docs)] // TODO
2022-12-21 17:26:09 -08:00
#[non_exhaustive]
pub enum SoftBufferError {
#[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(
"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
)]
UnsupportedWindowPlatform {
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,
#[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
/// 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
}
}
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> {}