pushrod/error.rs
1// Errors
2// Pushrod
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! # Errors
17//!
18//! Library code reports failures as [`Error`] values instead of panicking, so an application can
19//! recover or report the problem. Fallible public functions return [`Result`]. A failure that
20//! happens inside the run loop (a font that will not load while text is drawn, a texture the
21//! driver cannot create) has no caller to return to, so the engine delivers it as
22//! [`EngineEvent::Error`](crate::event::EngineEvent::Error) and keeps running.
23//!
24//! ```
25//! use pushrod::error::Error;
26//!
27//! let error = Error::font("missing.ttf", "No such file");
28//!
29//! assert_eq!(error.to_string(), "font \"missing.ttf\": No such file");
30//! ```
31
32use crate::widget::WidgetId;
33use std::fmt;
34
35/// A failure reported by Pushrod or a backend.
36///
37/// Every variant carries text, not a library-specific error type, so the value is `Clone` and
38/// `Eq` and can travel in an [`EngineEvent`](crate::event::EngineEvent).
39#[derive(Debug, Clone, PartialEq, Eq, Hash)]
40#[non_exhaustive]
41pub enum Error {
42 /// A backend could not start or lost a resource it needs (window, renderer, text library).
43 Backend {
44 /// What went wrong, as the backend reported it.
45 message: String,
46 },
47 /// A font file could not be loaded or used.
48 Font {
49 /// The path of the font file.
50 path: String,
51 /// What went wrong.
52 message: String,
53 },
54 /// An image could not be read, decoded or written.
55 Image {
56 /// The path of the image file.
57 path: String,
58 /// What went wrong.
59 message: String,
60 },
61 /// A texture could not be created, updated or drawn.
62 Texture {
63 /// What went wrong.
64 message: String,
65 },
66 /// A `Widget` ID does not exist in the widget tree.
67 UnknownWidget {
68 /// The ID that was not found.
69 id: WidgetId,
70 },
71 /// A form file could not be read or is not valid.
72 FormFile {
73 /// The path of the form file.
74 path: String,
75 /// What went wrong.
76 message: String,
77 },
78}
79
80/// The result of a fallible Pushrod function.
81pub type Result<T> = std::result::Result<T, Error>;
82
83impl Error {
84 /// Creates a [`Error::Backend`].
85 ///
86 /// # Arguments
87 ///
88 /// * `message` - What went wrong.
89 ///
90 /// # Returns
91 ///
92 /// The error.
93 pub fn backend(message: impl Into<String>) -> Self {
94 Error::Backend {
95 message: message.into(),
96 }
97 }
98
99 /// Creates a [`Error::Font`].
100 ///
101 /// # Arguments
102 ///
103 /// * `path` - The font file's path.
104 /// * `message` - What went wrong.
105 ///
106 /// # Returns
107 ///
108 /// The error.
109 pub fn font(path: impl Into<String>, message: impl Into<String>) -> Self {
110 Error::Font {
111 path: path.into(),
112 message: message.into(),
113 }
114 }
115
116 /// Creates a [`Error::Image`].
117 ///
118 /// # Arguments
119 ///
120 /// * `path` - The image file's path.
121 /// * `message` - What went wrong.
122 ///
123 /// # Returns
124 ///
125 /// The error.
126 pub fn image(path: impl Into<String>, message: impl Into<String>) -> Self {
127 Error::Image {
128 path: path.into(),
129 message: message.into(),
130 }
131 }
132
133 /// Creates a [`Error::Texture`].
134 ///
135 /// # Arguments
136 ///
137 /// * `message` - What went wrong.
138 ///
139 /// # Returns
140 ///
141 /// The error.
142 pub fn texture(message: impl Into<String>) -> Self {
143 Error::Texture {
144 message: message.into(),
145 }
146 }
147
148 /// Creates a [`Error::FormFile`].
149 ///
150 /// # Arguments
151 ///
152 /// * `path` - The form file's path.
153 /// * `message` - What went wrong.
154 ///
155 /// # Returns
156 ///
157 /// The error.
158 pub fn form_file(path: impl Into<String>, message: impl Into<String>) -> Self {
159 Error::FormFile {
160 path: path.into(),
161 message: message.into(),
162 }
163 }
164}
165
166impl fmt::Display for Error {
167 /// Writes a one-line description, without a trailing period.
168 ///
169 /// # Arguments
170 ///
171 /// * `f` - The formatter.
172 ///
173 /// # Returns
174 ///
175 /// The formatting result.
176 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
177 match self {
178 Error::Backend { message } => write!(f, "backend: {message}"),
179 Error::Font { path, message } => write!(f, "font \"{path}\": {message}"),
180 Error::Image { path, message } => write!(f, "image \"{path}\": {message}"),
181 Error::Texture { message } => write!(f, "texture: {message}"),
182 Error::UnknownWidget { id } => write!(f, "widget {id} does not exist"),
183 Error::FormFile { path, message } => write!(f, "form file \"{path}\": {message}"),
184 }
185 }
186}
187
188impl std::error::Error for Error {}
189
190#[cfg(test)]
191mod tests {
192 use super::*;
193
194 #[test]
195 fn display_names_the_failing_thing() {
196 assert_eq!(
197 Error::backend("no driver").to_string(),
198 "backend: no driver"
199 );
200 assert_eq!(
201 Error::font("a.ttf", "missing").to_string(),
202 "font \"a.ttf\": missing"
203 );
204 assert_eq!(
205 Error::image("a.png", "bad").to_string(),
206 "image \"a.png\": bad"
207 );
208 assert_eq!(Error::texture("full").to_string(), "texture: full");
209 assert_eq!(
210 Error::UnknownWidget { id: 7 }.to_string(),
211 "widget 7 does not exist"
212 );
213 assert_eq!(
214 Error::form_file("f.form", "syntax").to_string(),
215 "form file \"f.form\": syntax"
216 );
217 }
218
219 #[test]
220 fn is_a_std_error() {
221 let boxed: Box<dyn std::error::Error> = Box::new(Error::texture("x"));
222
223 assert_eq!(boxed.to_string(), "texture: x");
224 }
225}