Crate tamanegi_error

Crate tamanegi_error 

Source
Expand description

§What is this library

Tamanegi-Error is a library that helps you create error types that can track the propagation history.

See README.md for more details. (Only the Japanese version is available. Please wait for English version or send PR)

§How to use

Please add TamanegiError to the error struct made by Snafu as follows:

use snafu::{GenerateImplicitData, Snafu};
use tamanegi_error::{TamanegiError, location::StaticLocationRef};

#[derive(Snafu, TamanegiError)]
pub struct ErrorSubA {
    #[snafu(implicit)]
    location: StaticLocationRef,
}

impl ErrorSubA {
    pub fn new() -> Self {
        Self {
            location: StaticLocationRef::generate(),
        }
    }
}

#[derive(Snafu, TamanegiError)]
#[snafu(context(false))]
struct MyError {
    #[snafu(source)]
    source: ErrorSubA,
    #[snafu(implicit)]
    location: StaticLocationRef,
}

fn err() -> Result<(), MyError> {
    let err: Result<(), ErrorSubA> = Err(ErrorSubA::new());
    let _ = err?;
    Ok(())
}

fn main() {
    if let Err(e) = err() {
        println!("{:?}", e);
    }
}

Then the following propagation history can be seen:

1: MyError, at examples/basic_struct.rs:29:13
0: ErrorSubA, at examples/basic_struct.rs:13:23

§Relation to Stack Error

This library is based on Stack Error’s idea. I initially intended to use StackError as is because it is a great idea, but it was not suitable for no_std. So I write this crate from scratch suitable for no_std, using only the idea as a reference.

Modules§

location

Traits§

TamanegiTrait
This is a marker trait that indicates the implementation of Tamanegi-Error. It is implemented by the tamanegi_error_impl::TamanegiError derive macro.

Derive Macros§

TamanegiError