2 stable releases

1.0.23 Dec 4, 2020

#63 in #io-error

Download history 73/week @ 2024-03-31 6/week @ 2024-04-07 9/week @ 2024-04-14 24/week @ 2024-04-21 36/week @ 2024-04-28 15/week @ 2024-05-05 38/week @ 2024-05-12 16/week @ 2024-05-19 20/week @ 2024-05-26 37/week @ 2024-06-02 25/week @ 2024-06-09 30/week @ 2024-06-16 34/week @ 2024-06-23 2/week @ 2024-06-30 13/week @ 2024-07-07 27/week @ 2024-07-14

79 downloads per month
Used in 2 crates (via displaythis)

MIT/Apache

22KB
590 lines

derive(Display)

This library provides a convenient derive macro for the standard library's std::fmt::Display trait. displaythis is a fork of thiserror, modified for types that are not errors.


Example

# use std::io;
use displaythis::Display;

#[derive(Display, Debug)]
pub enum DataStoreError {
    #[display("data store disconnected")]
    Disconnect(io::Error),
    #[display("the data for key `{0}` is not available")]
    Redaction(String),
    #[display("invalid header (expected {expected:?}, found {found:?})")]
    InvalidHeader {
        expected: String,
        found: String,
    },
    #[display("unknown data store error")]
    Unknown,
}

Details

  • displaythis deliberately does not appear in your public API. You get the same thing as if you had written an implementation of std::fmt::Display by hand, and switching from handwritten impls to displaythis or vice versa is not a breaking change.

  • Types may be enums, structs with named fields, tuple structs, or unit structs.

  • You should provide #[display("...")] messages on the struct or each variant of your enum, as shown above in the example.

    The messages support a shorthand for interpolating fields from the error.

    • #[display("{var}")] ⟶ write!("{}", self.var)
    • #[display("{0}")] ⟶ write!("{}", self.0)
    • #[display("{var:?}")] ⟶ write!("{:?}", self.var)
    • #[display("{0:?}")] ⟶ write!("{:?}", self.0)

    These shorthands can be used together with any additional format args, which may be arbitrary expressions. For example:

    #[derive(Display, Debug)]
    pub enum Error {
        #[display("invalid rdo_lookahead_frames {0} (expected < {})", i32::MAX)]
        InvalidLookahead(u32),
    }
    

    If one of the additional expression arguments needs to refer to a field of the struct or enum, then refer to named fields as .var and tuple fields as .0.

    #[derive(Display, Debug)]
    pub enum Error {
        #[display("first letter must be lowercase but was {:?}", first_char(.0))]
        WrongCase(String),
        #[display("invalid index {idx}, expected at least {} and at most {}", .limits.lo, .limits.hi)]
        OutOfBounds { idx: usize, limits: Limits },
    }
    

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1.5MB
~35K SLoC