Skip to main content

StackReport

Struct StackReport 

Source
pub struct StackReport<E>(/* private fields */);
Expand description

Formats a StackError chain as a stack-trace-like report with type names and locations.

Wraps Result<(), E> and provides formatted output via Display (and Debug, which delegates to Display). Used at error display boundaries such as main().

Create via StackReport::from(error), Result::<(), E>::into(), or error.into().

§Output Format

Error: AppError::IoFailed: io failed, at src/main.rs:42:5
Caused by (recent first):
  1| InfraError::Read: read failed, at src/infra.rs:10:9
  2| No such file or directory (os error 2)

The first line shows the top-level error with type name and location. StackError sources (with location) are listed first with numbering, then plain Error::source() chain entries (without location) follow.

With the std feature, implements Termination for use as the return type of main(). The #[suzunari_error::report] macro can transform fn() -> Result<(), E> into fn() -> StackReport<E> automatically.

§Example

use suzunari_error::*;

#[suzunari_error]
#[suzu(display("app error"))]
struct AppError {
    source: std::io::Error,
}

fn run() -> Result<(), AppError> {
    std::fs::read("/nonexistent").context(AppSnafu)?;
    Ok(())
}

let err = run().unwrap_err();
let report = StackReport::from(err);

let output = format!("{report}");
assert!(output.contains("Error: AppError: app error"));
assert!(output.contains("Caused by"));

§Notes

  • Both Display and Debug produce an empty string for the Ok case. This is intentional — in the Termination use case, success should be silent.
  • Debug delegates to Display (same output). This intentionally deviates from the C-DEBUG guideline because Termination calls Debug::fmt to produce the error output. Making Debug structural (e.g., StackReport(Err(...))) would render the terminal output useless. Since StackReport is a display boundary type (not a general-purpose data carrier), the human-readable format is appropriate for both traits.
  • Display output does not include a trailing newline. This matches the convention for Display implementations and avoids double newlines with eprintln!("{report}"). The Termination impl adds a trailing newline when writing to stderr.

Trait Implementations§

Source§

impl<E: StackError> Debug for StackReport<E>

Source§

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

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

impl<E: StackError> Display for StackReport<E>

Source§

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

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

impl<E: StackError> From<E> for StackReport<E>

Source§

fn from(error: E) -> Self

Converts to this type from the input type.
Source§

impl<E: StackError> From<Result<(), E>> for StackReport<E>

Source§

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

Converts to this type from the input type.
Source§

impl<E: StackError> Termination for StackReport<E>

Available on crate feature std only.
Source§

fn report(self) -> ExitCode

Is called to get the representation of the value as status code. This status code is returned to the operating system.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<E> UnsafeUnpin for StackReport<E>
where E: UnsafeUnpin,

§

impl<E> UnwindSafe for StackReport<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§

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.