16 releases

0.2.1 Nov 20, 2024
0.2.0 Apr 26, 2024
0.1.2 Apr 18, 2024
0.1.1 Mar 6, 2024
0.0.9 Nov 29, 2023

#622 in Rust patterns

Download history 727/week @ 2024-08-24 1064/week @ 2024-08-31 690/week @ 2024-09-07 857/week @ 2024-09-14 1311/week @ 2024-09-21 889/week @ 2024-09-28 917/week @ 2024-10-05 1394/week @ 2024-10-12 888/week @ 2024-10-19 1230/week @ 2024-10-26 988/week @ 2024-11-02 794/week @ 2024-11-09 1379/week @ 2024-11-16 1228/week @ 2024-11-23 1134/week @ 2024-11-30 881/week @ 2024-12-07

4,727 downloads per month
Used in 7 crates (5 directly)

Apache-2.0

26KB
438 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

~0.3–0.8MB
~19K SLoC