easy_error/
terminator.rs

1//! Types that are useful in combination with the `Termination` trait.
2use std::{
3	error,
4	fmt::{self, Debug, Formatter},
5};
6
7use crate::ErrorExt;
8
9/// An error that wraps all other error types for a nicer debug output.
10///
11/// Given the current implementation of the `Termination` trait, and the
12/// implementation for every type that implements `Debug`, having a `main`
13/// function that returns a `Result` requires either using a type that
14/// implements the `Debug` trait poorly or dealing with an output that isn't
15/// very user friendly.
16///
17/// The types here help alleviate those issues. To begin with, we have an
18/// `Error` type that simply wraps any possible error and implements `Debug` in
19/// such a way as to make the output look nice. Additionally, there is a
20/// `Result` specialization in order to make the `main` function a little
21/// cleaner.
22pub struct Terminator
23{
24	inner: Box<dyn error::Error + 'static>,
25}
26
27impl Debug for Terminator
28{
29	fn fmt(&self, f: &mut Formatter) -> fmt::Result
30	{
31		writeln!(f, "{}", self.inner)?;
32		for cause in self.inner.iter_causes() {
33			writeln!(f, "Caused by: {}", cause)?;
34		}
35
36		Ok(())
37	}
38}
39
40impl<E: error::Error + 'static> From<E> for Terminator
41{
42	fn from(err: E) -> Terminator { Terminator { inner: Box::new(err) } }
43}