Struct snafu::Report

source ·
pub struct Report<E>(/* private fields */);
Expand description

Opinionated solution to format an error in a user-friendly way. Useful as the return type from main and test functions.

Most users will use the snafu::report procedural macro instead of directly using this type, but you can if you do not wish to use the macro.

§Rust 1.61 and up

Change the return type of the function to Report and wrap the body of your function with Report::capture.

§Rust before 1.61

Use Report as the error type inside of Result and then call either Report::capture_into_result or Report::from_error.

§Nightly Rust

Enabling the unstable-try-trait feature flag will allow you to use the ? operator directly:

use snafu::{prelude::*, Report};

fn main() -> Report<PlaceholderError> {
    let _v = may_fail_with_placeholder_error()?;

    Report::ok()
}

§Interaction with the Provider API

If you return a Report from your function and enable the unstable-provider-api feature flag, additional capabilities will be added:

  1. If provided, a Backtrace will be included in the output.
  2. If provided, a ExitCode will be used as the return value.

§Stability of the output

The exact content and format of a displayed Report are not stable, but this type strives to print the error and as much user-relevant information in an easily-consumable manner

Implementations§

source§

impl<E> Report<E>

source

pub fn from_error(error: E) -> Self

Convert an error into a Report.

Recommended if you support versions of Rust before 1.61.

use snafu::{prelude::*, Report};

#[derive(Debug, Snafu)]
struct PlaceholderError;

fn main() -> Result<(), Report<PlaceholderError>> {
    let _v = may_fail_with_placeholder_error().map_err(Report::from_error)?;
    Ok(())
}

fn may_fail_with_placeholder_error() -> Result<u8, PlaceholderError> {
    Ok(42)
}
source

pub fn capture_into_result<T>( body: impl FnOnce() -> Result<T, E>, ) -> Result<T, Self>

Executes a closure that returns a Result, converting the error variant into a Report.

Recommended if you support versions of Rust before 1.61.

use snafu::{prelude::*, Report};

#[derive(Debug, Snafu)]
struct PlaceholderError;

fn main() -> Result<(), Report<PlaceholderError>> {
    Report::capture_into_result(|| {
        let _v = may_fail_with_placeholder_error()?;

        Ok(())
    })
}

fn may_fail_with_placeholder_error() -> Result<u8, PlaceholderError> {
    Ok(42)
}
source

pub fn capture(body: impl FnOnce() -> Result<(), E>) -> Self

Executes a closure that returns a Result, converting any error to a Report.

Recommended if you only support Rust version 1.61 or above.

use snafu::{prelude::*, Report};

#[derive(Debug, Snafu)]
struct PlaceholderError;

fn main() -> Report<PlaceholderError> {
    Report::capture(|| {
        let _v = may_fail_with_placeholder_error()?;

        Ok(())
    })
}

fn may_fail_with_placeholder_error() -> Result<u8, PlaceholderError> {
    Ok(42)
}
source

pub const fn ok() -> Self

A Report that indicates no error occurred.

Trait Implementations§

source§

impl<E> Debug for Report<E>
where E: Error,

source§

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

Formats the value using the given formatter. Read more
source§

impl<E> Display for Report<E>
where E: Error,

source§

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

Formats the value using the given formatter. Read more
source§

impl<E> From<Result<(), E>> for Report<E>

source§

fn from(other: Result<(), E>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<E> Freeze for Report<E>
where E: Freeze,

§

impl<E> RefUnwindSafe for Report<E>
where E: RefUnwindSafe,

§

impl<E> Send for Report<E>
where E: Send,

§

impl<E> Sync for Report<E>
where E: Sync,

§

impl<E> Unpin for Report<E>
where E: Unpin,

§

impl<E> UnwindSafe for Report<E>
where E: UnwindSafe,

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§

default 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>,

§

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>,

§

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.