1use std::error::Error;
4use std::fmt::Display;
5use wgpu::{PollError, RequestAdapterError, RequestDeviceError};
6
7#[derive(Debug)]
9pub enum WgpuContextError {
10 NoCompatibleDevice,
12 WgpuCreateSurfaceError(wgpu::CreateSurfaceError),
15 UnsupportedSurfaceFormat,
21 RequestAdapterError(RequestAdapterError),
23 RequestDeviceError(RequestDeviceError),
25 PollError(PollError),
27}
28
29impl Display for WgpuContextError {
30 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31 match self {
32 Self::NoCompatibleDevice => writeln!(f, "Couldn't find suitable device"),
33 Self::WgpuCreateSurfaceError(inner) => {
34 writeln!(f, "Couldn't create wgpu surface")?;
35 inner.fmt(f)
36 }
37 Self::UnsupportedSurfaceFormat => {
38 writeln!(
39 f,
40 "Couldn't find `Rgba8Unorm` or `Bgra8Unorm` texture formats for surface"
41 )
42 }
43 Self::RequestAdapterError(inner) => {
44 writeln!(f, "Couldn't request an adapter: {:#}", inner)
45 }
46 Self::RequestDeviceError(inner) => {
47 writeln!(f, "Couldn't request a device: {:#}", inner)
48 }
49 Self::PollError(inner) => {
50 writeln!(f, "Couldn't poll a device: {:#}", inner)
51 }
52 }
53 }
54}
55
56impl Error for WgpuContextError {}
57impl From<wgpu::CreateSurfaceError> for WgpuContextError {
58 fn from(value: wgpu::CreateSurfaceError) -> Self {
59 Self::WgpuCreateSurfaceError(value)
60 }
61}
62
63impl From<RequestAdapterError> for WgpuContextError {
64 fn from(value: RequestAdapterError) -> Self {
65 Self::RequestAdapterError(value)
66 }
67}
68
69impl From<RequestDeviceError> for WgpuContextError {
70 fn from(value: RequestDeviceError) -> Self {
71 Self::RequestDeviceError(value)
72 }
73}
74
75impl From<PollError> for WgpuContextError {
76 fn from(value: PollError) -> Self {
77 Self::PollError(value)
78 }
79}