show_image/
error.rs

1//! Error types for the crate.
2
3use crate::WindowId;
4
5/// An error that can occur while creating a new window.
6#[derive(Debug)]
7pub enum CreateWindowError {
8	/// The underlying call to `winit` reported an error.
9	Winit(winit::error::OsError),
10
11	/// Failed to get a suitable GPU device.
12	GetDevice(GetDeviceError),
13
14	/// Failed to create a surface for drawing.
15	CreateSurface(wgpu::CreateSurfaceError),
16}
17
18/// An error that can occur while interpreting image data.
19#[derive(Debug, Clone, Eq, PartialEq)]
20pub enum ImageDataError {
21	/// The image data is not in a supported format.
22	UnsupportedImageFormat(UnsupportedImageFormat),
23
24	/// An other error occured.
25	Other(String),
26}
27
28/// An error indicating that the image data is not in a supported format.
29#[derive(Debug, Clone, Eq, PartialEq)]
30pub struct UnsupportedImageFormat {
31	/// The unsupported format.
32	pub format: String,
33}
34
35/// The window ID is not valid.
36#[derive(Debug, Clone, Eq, PartialEq)]
37pub struct InvalidWindowId {
38	/// The invalid window ID.
39	pub window_id: WindowId,
40}
41
42/// An error that can occur when setting the image of a window.
43#[derive(Debug, Clone, Eq, PartialEq)]
44pub enum SetImageError {
45	/// The window ID is invalid.
46	InvalidWindowId(InvalidWindowId),
47
48	/// The image data is not supported.
49	ImageDataError(ImageDataError),
50}
51
52/// The specified overlay was not found on the window.
53#[derive(Debug, Clone, Eq, PartialEq)]
54pub struct UnknownOverlay {
55	/// The name of the overlay.
56	pub name: String,
57}
58
59/// An error occured trying to find a usable graphics device.
60#[derive(Debug, Clone, Eq, PartialEq)]
61pub enum GetDeviceError {
62	/// No suitable video adapter was found.
63	NoSuitableAdapterFound(NoSuitableAdapterFound),
64
65	/// No suitable graphics device was found.
66	NoSuitableDeviceFound(wgpu::RequestDeviceError),
67}
68
69/// No suitable video adapter was found.
70#[derive(Debug, Clone, Eq, PartialEq)]
71pub struct NoSuitableAdapterFound;
72
73/// An error occured trying to save an image.
74#[derive(Debug)]
75pub enum SaveImageError {
76	/// An I/O error occured.
77	IoError(std::io::Error),
78
79	/// An error occured encoding the PNG image.
80	#[cfg(feature = "png")]
81	PngError(png::EncodingError),
82}
83
84impl From<winit::error::OsError> for CreateWindowError {
85	fn from(other: winit::error::OsError) -> Self {
86		Self::Winit(other)
87	}
88}
89
90impl From<GetDeviceError> for CreateWindowError {
91	fn from(other: GetDeviceError) -> Self {
92		Self::GetDevice(other)
93	}
94}
95
96impl From<wgpu::CreateSurfaceError> for CreateWindowError {
97	fn from(other: wgpu::CreateSurfaceError) -> Self {
98		Self::CreateSurface(other)
99	}
100}
101
102impl From<ImageDataError> for SetImageError {
103	fn from(other: ImageDataError) -> Self {
104		Self::ImageDataError(other)
105	}
106}
107
108impl From<InvalidWindowId> for SetImageError {
109	fn from(other: InvalidWindowId) -> Self {
110		Self::InvalidWindowId(other)
111	}
112}
113
114impl From<UnsupportedImageFormat> for ImageDataError {
115	fn from(other: UnsupportedImageFormat) -> Self {
116		Self::UnsupportedImageFormat(other)
117	}
118}
119
120impl From<String> for ImageDataError {
121	fn from(other: String) -> Self {
122		Self::Other(other)
123	}
124}
125
126impl<'a> From<&'a str> for ImageDataError {
127	fn from(other: &'a str) -> Self {
128		Self::Other(other.to_string())
129	}
130}
131
132impl From<NoSuitableAdapterFound> for GetDeviceError {
133	fn from(other: NoSuitableAdapterFound) -> Self {
134		Self::NoSuitableAdapterFound(other)
135	}
136}
137
138impl From<wgpu::RequestDeviceError> for GetDeviceError {
139	fn from(other: wgpu::RequestDeviceError) -> Self {
140		Self::NoSuitableDeviceFound(other)
141	}
142}
143
144impl From<std::io::Error> for SaveImageError {
145	fn from(other: std::io::Error) -> Self {
146		Self::IoError(other)
147	}
148}
149
150#[cfg(feature = "png")]
151impl From<png::EncodingError> for SaveImageError {
152	fn from(other: png::EncodingError) -> Self {
153		match other {
154			png::EncodingError::IoError(e) => Self::IoError(e),
155			e => Self::PngError(e),
156		}
157	}
158}
159
160impl std::error::Error for CreateWindowError {}
161impl std::error::Error for ImageDataError {}
162impl std::error::Error for UnsupportedImageFormat {}
163impl std::error::Error for InvalidWindowId {}
164impl std::error::Error for SetImageError {}
165impl std::error::Error for UnknownOverlay {}
166impl std::error::Error for GetDeviceError {}
167impl std::error::Error for NoSuitableAdapterFound {}
168impl std::error::Error for SaveImageError {}
169
170impl std::fmt::Display for CreateWindowError {
171	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
172		match self {
173			Self::Winit(e) => write!(f, "{}", e),
174			Self::GetDevice(e) => write!(f, "{}", e),
175			Self::CreateSurface(e) => write!(f, "{}", e),
176		}
177	}
178}
179
180impl std::fmt::Display for ImageDataError {
181	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
182		match self {
183			Self::UnsupportedImageFormat(e) => write!(f, "{}", e),
184			Self::Other(e) => write!(f, "{}", e),
185		}
186	}
187}
188
189impl std::fmt::Display for UnsupportedImageFormat {
190	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
191		write!(f, "unsupported image format: {}", self.format)
192	}
193}
194
195impl std::fmt::Display for InvalidWindowId {
196	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
197		write!(f, "invalid window ID: {:?}", self.window_id)
198	}
199}
200
201impl std::fmt::Display for SetImageError {
202	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
203		match self {
204			Self::InvalidWindowId(e) => write!(f, "{}", e),
205			Self::ImageDataError(e) => write!(f, "{}", e),
206		}
207	}
208}
209
210impl std::fmt::Display for UnknownOverlay {
211	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
212		write!(f, "unknown overlay: {}", self.name)
213	}
214}
215
216impl std::fmt::Display for GetDeviceError {
217	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
218		match self {
219			Self::NoSuitableAdapterFound(e) => write!(f, "{}", e),
220			Self::NoSuitableDeviceFound(e) => write!(f, "{}", e),
221		}
222	}
223}
224
225impl std::fmt::Display for NoSuitableAdapterFound {
226	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
227		write!(f, "no suitable graphics adapter found")
228	}
229}
230
231impl std::fmt::Display for SaveImageError {
232	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
233		match self {
234			Self::IoError(e) => write!(f, "{}", e),
235			#[cfg(feature = "png")]
236			Self::PngError(e) => write!(f, "{}", e),
237		}
238	}
239}