15 releases

0.2.0 Apr 26, 2024
0.1.2 Apr 18, 2024
0.1.1 Mar 6, 2024
0.1.0 Jan 15, 2024
0.0.9 Nov 29, 2023

#946 in Rust patterns

Download history 1505/week @ 2024-07-01 999/week @ 2024-07-08 1045/week @ 2024-07-15 652/week @ 2024-07-22 1006/week @ 2024-07-29 612/week @ 2024-08-05 1089/week @ 2024-08-12 947/week @ 2024-08-19 884/week @ 2024-08-26 926/week @ 2024-09-02 677/week @ 2024-09-09 847/week @ 2024-09-16 1436/week @ 2024-09-23 752/week @ 2024-09-30 1000/week @ 2024-10-07 1310/week @ 2024-10-14

4,534 downloads per month
Used in 6 crates (4 directly)

Apache-2.0

26KB
437 lines

thiserror-ext

Crate Docs

Useful extension utilities for thiserror. See the documentation for more details.

#[derive(
    Debug,
    thiserror::Error,
    thiserror_ext::Box,
    thiserror_ext::Construct,
    thiserror_ext::ContextInto,
    thiserror_ext::Macro,
)]
#[thiserror_ext(
    newtype(name = Error, backtrace),
    macro(path = "crate::foo"),
)]
enum ErrorKind {
    #[error("cannot parse int from `{from}`")]
    Parse {
        source: std::num::ParseIntError,
        from: String,
    },

    #[error("not yet implemented: {msg}")]
    NotImplemented {
        issue: Option<i32>,
        #[message] msg: String,
    },

    #[error("internal error: {0}")]
    Internal(String),
}

// `thiserror_ext::Construct`
let error: Error = Error::internal("oops");

// `thiserror_ext::Box`
assert_eq!(std::mem::size_of::<Error>(), std::mem::size_of::<usize>());
let bt: &Backtrace = std::error::request_ref(&error).unwrap();

// `thiserror_ext::ContextInto`
let result: Result<i32, Error> = "foo".parse().into_parse("foo");

// `thiserror_ext::AsReport`
//
// "cannot parse int from `foo`: invalid digit found in string"
println!("{}", result.unwrap_err().as_report());

// `thiserror_ext::Macro`
bail_not_implemented!(issue = 42, "an {} feature", "awesome");

Dependencies

~290–760KB
~18K SLoC