context-attribute

Crates.iocontext-attribute
lib.rscontext-attribute
version1.0.0
sourcesrc
created_at2019-05-09 21:38:19.237855
updated_at2019-05-09 21:38:19.237855
descriptionSet the error context using doc comments.
homepage
repositoryhttps://github.com/yoshuawuyts/context-attribute
max_upload_size
id133156
size30,430
Yosh (yoshuawuyts)

documentation

https://docs.rs/context-attribute

README

context-attribute

crates.io version build status downloads docs.rs docs

Set the error context using doc comments.

This is useful because instead of writing manual error messages to provide context to an error, it automatically derives it from doc comments. This works especially well for async contexts, where stack traces may not be persisted past yield points and thread boundaries. But contexts do.

Examples

use context_attribute::context;
use failure::{ensure, ResultExt};

/// Square a number if it's less than 10.
#[context]
fn square(num: usize) -> Result<usize, failure::Error> {
    ensure!(num < 10, "Number was too large");
    Ok(num * num)
}

fn main() -> Result<(), failure::Error> {
    let args = std::env::args();
    ensure!(args.len() == 2, "usage: square <num>");
    let input = args.skip(1).next().unwrap().parse()?;

    println!("result is {}", square(input)?);

    Ok(())
}
$ cargo run --example square 12
Error: ErrorMessage { msg: "Number was too large" }
Square a number if it's less than 10.

Installation

$ cargo add context-attribute

Safety

This crate uses #![deny(unsafe_code)] to ensure everything is implemented in 100% Safe Rust.

Contributing

Want to join us? Check out our "Contributing" guide and take a look at some of these issues:

References

None.

License

MIT OR Apache-2.0

Commit count: 8

cargo fmt