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
DisplayandDebugproduce an empty string for theOkcase. This is intentional — in theTerminationuse case, success should be silent. Debugdelegates toDisplay(same output). This intentionally deviates from the C-DEBUG guideline becauseTerminationcallsDebug::fmtto produce the error output. MakingDebugstructural (e.g.,StackReport(Err(...))) would render the terminal output useless. SinceStackReportis a display boundary type (not a general-purpose data carrier), the human-readable format is appropriate for both traits.Displayoutput does not include a trailing newline. This matches the convention forDisplayimplementations and avoids double newlines witheprintln!("{report}"). TheTerminationimpl adds a trailing newline when writing to stderr.
Trait Implementations§
Source§impl<E: StackError> Debug for StackReport<E>
impl<E: StackError> Debug for StackReport<E>
Source§impl<E: StackError> Display for StackReport<E>
impl<E: StackError> Display for StackReport<E>
Source§impl<E: StackError> From<E> for StackReport<E>
impl<E: StackError> From<E> for StackReport<E>
Source§impl<E: StackError> From<Result<(), E>> for StackReport<E>
impl<E: StackError> From<Result<(), E>> for StackReport<E>
Source§impl<E: StackError> Termination for StackReport<E>
Available on crate feature std only.
impl<E: StackError> Termination for StackReport<E>
Available on crate feature
std only.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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more