ErrorTraceColorFormatter

Struct ErrorTraceColorFormatter 

Source
pub struct ErrorTraceColorFormatter<'s, 'e1, 'e2> { /* private fields */ }
Available on crate feature colors only.
Expand description

Formats an error and all its dependencies using neat ANSI-colors if the formatter to which we’re writing supports it.

Whether colors are enabled or not can be checked by console’s colors_enabled_stderr() function, and controlled by set_colors_enabled_stderr().

See ErrorTraceFormatter to do the same but without ANSI colors at all.

§Example

use error_trace::{ErrorTraceColorFormatter, ErrorTrace as _};

#[derive(Debug)]
struct ExampleError {
    msg : String,
}
impl Display for ExampleError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FResult {
        write!(f, "{}", self.msg)
    }
}
impl Error for ExampleError {}

let err = ExampleError { msg: "Hello, world!".into() };
let fmt: ErrorTraceColorFormatter = err.trace_colored();

// Colours aren't visible here, because we're writing to a string; but try writing to stdout/stderr!
assert_eq!(format!("{fmt}"), "Hello, world!");

Implementations§

Source§

impl<'s, 'e1, 'e2> ErrorTraceColorFormatter<'s, 'e1, 'e2>

Source

pub fn new( msg: impl Into<Cow<'s, str>>, err: Option<&'e1 (dyn Error + 'e2)>, ) -> Self

Builds a formatter for a given “anonymous error”.

This is useful for creating one-time error traces where you don’t want to create the root type.

For even more convenience, see the toplevel!-macro.

§Arguments
  • msg: A message that is printed as “current error”.
  • err: An optional error that, if any, will cause this formatter to start printing a trace based on the error’s Error::source()-implementation.
§Returns

A new ErrorTraceColourFormatter ready to rock-n-roll.

Trait Implementations§

Source§

impl<'s, 'e1, 'e2> Display for ErrorTraceColorFormatter<'s, 'e1, 'e2>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> FResult

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'s, 'e1, 'e2> Freeze for ErrorTraceColorFormatter<'s, 'e1, 'e2>

§

impl<'s, 'e1, 'e2> !RefUnwindSafe for ErrorTraceColorFormatter<'s, 'e1, 'e2>

§

impl<'s, 'e1, 'e2> !Send for ErrorTraceColorFormatter<'s, 'e1, 'e2>

§

impl<'s, 'e1, 'e2> !Sync for ErrorTraceColorFormatter<'s, 'e1, 'e2>

§

impl<'s, 'e1, 'e2> Unpin for ErrorTraceColorFormatter<'s, 'e1, 'e2>

§

impl<'s, 'e1, 'e2> !UnwindSafe for ErrorTraceColorFormatter<'s, 'e1, 'e2>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.