scoped-error 0.1.4

Structured error handling with semantic context trees.
Documentation
// Copyright (C) 2026 Kan-Ru Chen <kanru@kanru.info>
//
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

//! Extension traits for error handling.
//!
//! This module provides [`ErrorExt`], which adds convenient methods to
/// all types implementing [`std::error::Error`].
use std::error::Error;

use crate::ErrorReport;

/// Extension trait for [`std::error::Error`].
///
/// This trait is implemented for all types that implement `Error`,
/// providing additional methods for working with errors.
///
/// # Example
///
/// ```
/// use std::error::Error;
/// use scoped_error::ErrorExt;
///
/// fn handle_error(e: impl Error + 'static) {
///     let report = e.report();
///     println!("{}", report);
/// }
/// ```
pub trait ErrorExt {
    /// Generate a formatted error report for this error.
    ///
    /// The report includes the error message and all causes in the chain,
    /// formatted for human readability.
    ///
    /// # Example
    ///
    /// ```
    /// use scoped_error::{Error, expect_error, ErrorExt};
    ///
    /// let result: Result<(), Error> = expect_error("Outer", || {
    ///     Err("Inner")?;
    ///     Ok(())
    /// });
    ///
    /// if let Err(e) = result {
    ///     println!("{}", e.report());
    /// }
    /// ```
    fn report(&self) -> ErrorReport<'_>;
}

impl<T> ErrorExt for T
where
    T: Error + 'static,
{
    fn report(&self) -> ErrorReport<'_> {
        ErrorReport(self)
    }
}