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
// SPDX-License-Identifier: MIT OR Apache-2.0
//! # pretty-error-debug
//!
//! [](https://github.com/Kijewski/pretty-error-debug/actions/workflows/ci.yml)
//! [](https://crates.io/crates/pretty-error-debug)
//! 
//! [](/LICENSE-MIT "License: MIT OR Apache-2.0")
//!
//! Display a the chain of an error. Most useful as [`Result<(), E>`](std::result::Result) for your `fn main()`,
//! and in conjunction with [`thiserror`](https://crates.io/crates/thiserror).
//!
//! This crate simply <del>plagiarized</del> <ins>extracted</ins> all the relevant formatting code from
//! [`anyhow`](https://crates.io/crates/anyhow).
//!
//! ## Example message
//!
//! ```text
//! Error: Got a 'middle' error
//!
//! Caused by:
//! 1: A nested error occured
//! 2: 'inner' failed
//! 3: Caught an error: Not implemented, yet.
//! ```
//!
//! ## With `thiserror`
//!
//! ```rust,ignore
//! #[derive(pretty_error_debug::Debug, thiserror::Error)]
//! pub enum MyError {
//! #[error("Error variant 1 happened")]
//! Variant1(#[from] Error1),
//! #[error("Error variant 2 happened")]
//! Variant2(#[from] Error2),
//! }
//!
//! fn main() -> Result<(), MyError> {
//! # /*
//! ...
//! # */ Ok(())
//! }
//! ```
//!
//! ## With `thiserror`, but without a new type
//!
//! ```rust,ignore
//! #[derive(Debug, thiserror::Error)]
//! pub enum MyError {
//! #[error("Error variant 1 happened")]
//! Variant1(#[from] Error1),
//! #[error("Error variant 2 happened")]
//! Variant2(#[from] Error2),
//! }
//!
//! fn main() -> Result<(), pretty_error_debug::Wrapper<MyError>> {
//! # /*
//! ...
//! # */ Ok(())
//! }
//! ```
//!
//! ## Without `thiserror`
//!
//! ```rust
//! use std::error::Error;
//! use std::fmt::{self, Write};
//! #
//! # #[derive(Debug)] pub struct Error1;
//! # impl Error for Error1 {}
//! # impl fmt::Display for Error1 {
//! # fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
//! # write!(f, "Error1")
//! # }
//! # }
//! #
//! # #[derive(Debug)] pub struct Error2;
//! # impl Error for Error2 {}
//! # impl fmt::Display for Error2 {
//! # fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
//! # write!(f, "Error2")
//! # }
//! # }
//!
//! #[derive(pretty_error_debug::Debug)]
//! pub enum MyError {
//! Variant1(Error1),
//! Variant2(Error2),
//! }
//!
//! impl fmt::Display for MyError {
//! fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
//! match self {
//! MyError::Variant1(_) => write!(f, "Error variant 1 happened"),
//! MyError::Variant2(_) => write!(f, "Error variant 2 happened"),
//! }
//! }
//! }
//!
//! impl Error for MyError {
//! fn source(&self) -> Option<&(dyn 'static + Error)> {
//! match self {
//! MyError::Variant1(source) => Some(source),
//! MyError::Variant2(source) => Some(source),
//! }
//! }
//! }
//!
//! fn main() -> Result<(), MyError> {
//! # /*
//! ...
//! # */ Ok(())
//! }
//! ```
//!
#![cfg_attr(docsrs, feature(doc_cfg))]
#![forbid(unsafe_code)]
#![warn(absolute_paths_not_starting_with_crate)]
#![warn(elided_lifetimes_in_paths)]
#![warn(meta_variable_misuse)]
#![warn(missing_copy_implementations)]
#![warn(missing_debug_implementations)]
#![warn(missing_docs)]
#![warn(non_ascii_idents)]
#![warn(unused_extern_crates)]
#![warn(unused_lifetimes)]
#![warn(unused_results)]
mod implementation;
#[cfg(test)]
mod test;
#[cfg(feature = "derive")]
#[doc(hidden)]
pub use core;
use std::error::Error;
use std::fmt;
#[cfg(feature = "derive")]
#[cfg_attr(docsrs, doc(inline, cfg(feature = "derive")))]
pub use pretty_error_debug_derive::PrettyDebug as Debug;
pub use self::implementation::pretty_error_debug;
/// Wrap an [`Error`] to display its error chain in debug messages ([`format!("{:?}")`][fmt::Debug]).
///
/// ## Example
///
/// ```rust
/// use some_external_mod::{SomeError, some_test};
///
/// fn main() -> Result<(), pretty_error_debug::Wrapper<SomeError>> {
/// some_test()?;
/// Ok(())
/// }
///
/// mod some_external_mod {
/// #[derive(Debug)]
/// pub struct SomeError;
///
/// impl std::error::Error for SomeError {}
///
/// impl std::fmt::Display for SomeError {
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// f.write_str("Something went wrong")
/// }
/// }
///
/// pub fn some_test() -> Result<(), SomeError> {
/// # /*
/// Err(SomeError)
/// # */ Ok(())
/// }
/// }
/// ```
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Wrapper<E: 'static + Error>(pub E);
impl<E: 'static + Error> Wrapper<E> {
/// Return the wrapped argument.
#[inline]
pub fn new(err: E) -> Self {
Self(err)
}
}
impl<E: 'static + Error> From<E> for Wrapper<E> {
#[inline]
fn from(value: E) -> Self {
Self(value)
}
}
impl<E: 'static + Error> Error for Wrapper<E> {
#[inline]
fn source(&self) -> Option<&(dyn 'static + Error)> {
Some(&self.0)
}
}
impl<E: 'static + Error> fmt::Display for Wrapper<E> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
impl<E: 'static + Error> fmt::Debug for Wrapper<E> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
pretty_error_debug(&self.0, f)
}
}
/// Wrap a reference to an [`Error`] to display its error chain with [`format!("{}")`][fmt::Display].
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Display<'a, E: ?Sized + Error>(pub &'a E);
impl<'a, E: ?Sized + Error> Display<'a, E> {
/// Return the wrapped reference.
#[inline]
pub fn new(err: &'a E) -> Self {
Self(err)
}
}
impl<'a, E: Error> From<&'a E> for Display<'a, E> {
#[inline]
fn from(value: &'a E) -> Self {
Self(value)
}
}
impl<E: ?Sized + Error> fmt::Debug for Display<'_, E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Display").field(&self.0).finish()
}
}
impl<E: ?Sized + Error> fmt::Display for Display<'_, E> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
pretty_error_debug(&self.0, f)
}
}