astral-error 0.0.5

Error handling library for the Astral Engine (WIP)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright (c) Astral Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Written by Tim Diekmann <tim.diekmann@3dvision.de>, November 2018

//! Traits and structures for working with Errors in the Astral Engine.

#![doc(
	html_no_source,
	html_logo_url = "https://astral-engine.github.io/docs/logo_astral.svg",
	html_favicon_url = "https://astral-engine.github.io/docs/logo.svg",
	test(attr(
		deny(
			future_incompatible,
			nonstandard_style,
			rust_2018_compatibility,
			rust_2018_idioms,
			unused,
			macro_use_extern_crate,
			trivial_casts,
			trivial_numeric_casts,
			unused_import_braces,
			unused_lifetimes,
			unused_qualifications,
			variant_size_differences,
		),
		allow(unused_extern_crates)
	))
)]
#![warn(
	future_incompatible,
	nonstandard_style,
	rust_2018_compatibility,
	rust_2018_idioms,
	unused,
	macro_use_extern_crate,
	missing_copy_implementations,
	missing_debug_implementations,
	missing_docs,
	// missing_doc_code_examples,
	single_use_lifetimes,
	trivial_casts,
	trivial_numeric_casts,
	unreachable_pub,
	unused_import_braces,
	unused_lifetimes,
	unused_qualifications,
	unused_results,
	variant_size_differences,
	clippy::pedantic
)]

mod chained;
mod custom;
mod option_ext;
mod repr;
mod result;
mod result_ext;

pub use self::{option_ext::OptionExt, result::Result, result_ext::ResultExt};

use std::{
	error,
	fmt::{self, Debug, Display, Formatter},
};

use self::{chained::Chained, custom::Custom, repr::Repr};

/// The generic error type for the Astral engine.
///
/// `Error` can be created with crafted error messages and a particular value of
/// `Kind` and optionally with a arbitrary error payload.
///
/// It is useful but not necessary, that `Kind` implements [`Debug`] and
/// [`Display`] so [`std::error::Error`] is implemented.
///
/// [`Debug`]: std::fmt::Debug
/// [`Display`]: std::fmt::Display
/// [`Error`]: std::error::Error
///
/// # Example
///
/// ```
/// use std::fmt::{self, Debug, Display, Formatter};
/// use std::error::Error as StdError;
///
/// use astral::error::Error;
///
/// #[derive(Debug, PartialEq)]
/// enum MyErrorKind {
///     Variant,
/// }
///
/// impl Display for MyErrorKind {
///     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
///         Debug::fmt(self, f)
///     }
/// }
///
/// let my_error = Error::new(MyErrorKind::Variant, "oh no!");
///
/// let my_error2 = Error::new(MyErrorKind::Variant, my_error);
///
/// assert_eq!(*my_error2.kind(), MyErrorKind::Variant);
/// assert!(my_error2.source().is_none());
/// ```
pub struct Error<Kind> {
	repr: Repr<Kind>,
}

impl<Kind> Error<Kind> {
	/// Creates a new error from a known kind of error as well as an arbitrary
	/// error payload. The `error` argument is an arbitrary payload which will
	/// be contained in this `Error`. The resulting error don't have a source
	/// error returned by [`Error::source`].
	///
	/// [`Error::source`]: std::error::Error::source
	///
	/// # Example
	///
	/// ```
	/// # use std::fmt::{self, Debug, Display, Formatter};
	/// # #[derive(Debug, PartialEq)] enum MyErrorKind { Variant }
	/// # impl Display for MyErrorKind {
	/// #     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
	/// #         Debug::fmt(self, f)
	/// #     }
	/// # }
	/// use std::error::Error as StdError;
	///
	/// use astral::error::Error;
	///
	/// let my_error = Error::new(MyErrorKind::Variant, "oh no!");
	///
	/// let my_error2 = Error::new(MyErrorKind::Variant, my_error);
	///
	/// assert!(my_error2.source().is_none());
	/// ```
	pub fn new<E>(kind: Kind, error: E) -> Self
	where
		E: Into<Box<dyn error::Error + Send + Sync>>,
	{
		Self {
			repr: Repr::Custom(Box::new(Custom {
				kind,
				error: error.into(),
			})),
		}
	}

	/// Creates a new error from a known kind of error as well as an arbitrary
	/// error payload and keeps another payload as source error.
	///
	/// The `error` argument is an arbitrary payload which will be contained in
	/// this `Error`. The `source` argument is an error, which will be returned
	/// by [`Error::source`]
	///
	/// [`Error::source`]: std::error::Error::source
	///
	/// # Example
	///
	/// ```
	/// # use std::fmt::{self, Debug, Display, Formatter};
	/// # #[derive(Debug)] enum MyErrorKind { Variant }
	/// # impl Display for MyErrorKind {
	/// #     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
	/// #         Debug::fmt(self, f)
	/// #     }
	/// # }
	/// # fn main() { test().unwrap() }
	/// # fn test() -> Option<()> {
	/// use std::error::Error as StdError;
	///
	/// use astral::error::Error;
	///
	/// let my_error = Error::new(MyErrorKind::Variant, "oh no!");
	///
	/// let my_error2 = Error::chained(MyErrorKind::Variant, "failed!", my_error);
	///
	/// assert_eq!(my_error2.source()?.to_string(), "oh no!");
	/// # Some(())
	/// # }
	/// ```
	pub fn chained<E, S>(kind: Kind, error: E, source: S) -> Self
	where
		E: Into<Box<dyn error::Error + Send + Sync>>,
		S: Into<Box<dyn error::Error + Send + Sync>>,
	{
		Self {
			repr: Repr::Chained(Box::new(Chained {
				kind,
				error: error.into(),
				source: source.into(),
			})),
		}
	}

	/// Returns a reference to the inner error wrapped by this error (if any).
	///
	/// If this `Error` was constructed via [`new`] or [`chained`] then this
	/// function will return [`Some`], otherwise it will return [`None`].
	///
	/// [`new`]: struct.Error.html#method.new
	/// [`chained`]: struct.Error.html#method.chained
	///
	/// # Examples
	///
	/// ```
	/// use astral::error::Error;
	///
	/// #[derive(Debug)]
	/// enum MyErrorKind {
	/// 	Variant,
	/// }
	///
	/// fn print_error<Kind>(err: &Error<Kind>) {
	///     if let Some(inner_err) = err.get_ref() {
	///         println!("Inner error: {:?}", inner_err);
	///     } else {
	///         println!("No inner error");
	///     }
	/// }
	///
	/// fn main() {
	///     // Will print "Inner error: Variant".
	///     print_error(&Error::new(MyErrorKind::Variant, "oh no!"));
	/// }
	/// ```
	pub fn get_ref(&self) -> Option<&(dyn error::Error + Send + Sync + 'static)> {
		self.repr.get_ref()
	}

	/// Returns a mutable reference to the inner error wrapped by this error
	/// (if any).
	///
	/// If this `Error` was constructed via [`new`] or [`chained`] then this
	/// function will return [`Some`], otherwise it will return [`None`].
	///
	/// [`new`]: struct.Error.html#method.new
	/// [`chained`]: struct.Error.html#method.chained
	///
	/// # Examples
	///
	/// ```
	/// use std::{error, fmt};
	/// use std::fmt::Display;
	///
	/// use astral::error::Error;
	///
	/// #[derive(Debug)]
	/// struct MyError {
	///     v: String,
	/// }
	///
	/// impl MyError {
	///     fn new() -> MyError {
	///         MyError {
	///             v: "oh no!".to_string()
	///         }
	///     }
	///
	///     fn change_message(&mut self, new_message: &str) {
	///         self.v = new_message.to_string();
	///     }
	/// }
	///
	/// impl error::Error for MyError {}
	///
	/// impl Display for MyError {
	///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
	///         write!(f, "MyError: {}", &self.v)
	///     }
	/// }
	///
	/// fn change_error<Kind>(mut err: Error<Kind>) -> Error<Kind> {
	///     if let Some(inner_err) = err.get_mut() {
	///         inner_err.downcast_mut::<MyError>().unwrap().change_message("I've been changed!");
	///     }
	///     err
	/// }
	///
	/// #[derive(Debug)]
	/// enum MyErrorKind {
	/// 	Variant,
	/// }
	///
	/// fn print_error<Kind>(err: &Error<Kind>) {
	///     if let Some(inner_err) = err.get_ref() {
	///         println!("Inner error: {}", inner_err);
	///     } else {
	///         println!("No inner error");
	///     }
	/// }
	///
	/// fn main() {
	///     // Will print "Inner error: ...".
	///     print_error(&change_error(Error::new(MyErrorKind::Variant, MyError::new())));
	/// }
	/// ```
	pub fn get_mut(&mut self) -> Option<&mut (dyn error::Error + Send + Sync + 'static)> {
		self.repr.get_mut()
	}

	/// Consumes the `Error`, returning its inner error (if any).
	///
	/// If this `Error` was constructed via [`new`] or [`chained`] then this
	/// function will return [`Some`], otherwise it will return [`None`].
	///
	/// [`new`]: struct.Error.html#method.new
	/// [`chained`]: struct.Error.html#method.chained
	///
	/// # Example
	///
	/// ```
	/// # use std::fmt::{self, Display, Formatter};
	/// # #[derive(Debug)] enum MyErrorKind { Variant }
	/// # impl Display for MyErrorKind {
	/// #     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
	/// #         Display::fmt(self, f)
	/// #     }
	/// # }
	/// # fn main() { test().unwrap() }
	/// # fn test() -> Option<()> {
	/// use astral::error::Error;
	///
	/// let my_error = Error::new(MyErrorKind::Variant, "oh no!");
	///
	/// let my_error2 = Error::new(MyErrorKind::Variant, my_error);
	///
	/// assert_eq!(my_error2.into_inner()?.to_string(), "oh no!");
	/// # Some(())
	/// # }
	/// ```
	#[inline]
	pub fn into_inner(self) -> Option<Box<dyn error::Error + Send + Sync>> {
		self.repr.into_inner()
	}

	/// Returns the corresponding `Kind` for this error.
	///
	/// # Example
	///
	/// ```
	/// use astral::error::Error;
	///
	/// #[derive(Debug, PartialEq)]
	/// enum MyErrorKind {
	///     Variant,
	/// }
	///
	/// let my_error = Error::new(MyErrorKind::Variant, "oh no!");
	/// assert_eq!(*my_error.kind(), MyErrorKind::Variant);
	/// ```
	#[inline]
	pub fn kind(&self) -> &Kind {
		self.repr.kind()
	}
}

impl<Kind> Debug for Error<Kind>
where
	Kind: Debug,
{
	fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
		Debug::fmt(&self.repr, fmt)
	}
}

impl<Kind> Display for Error<Kind>
where
	Kind: Display,
{
	#[inline]
	fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
		Display::fmt(&self.repr, fmt)
	}
}

impl<Kind> error::Error for Error<Kind>
where
	Kind: Debug + Display,
{
	#[inline]
	fn source(&self) -> Option<&(dyn error::Error + 'static)> {
		self.repr.source()
	}
}

impl<Kind> From<Kind> for Error<Kind> {
	#[inline]
	fn from(kind: Kind) -> Self {
		Self {
			repr: Repr::Simple(kind),
		}
	}
}