1derive(Display) /// `From<docs>` 2=============== 3 4[](https://crates.io/crates/displaydoc) 5[](https://docs.rs/displaydoc) 6 7This library provides a convenient derive macro for the standard library's 8[`core::fmt::Display`] trait. 9 10[`core::fmt::Display`]: https://doc.rust-lang.org/std/fmt/trait.Display.html 11 12```toml 13[dependencies] 14displaydoc = "0.2" 15``` 16 17*Compiler support: requires rustc 1.56+* 18 19<br> 20 21### Example 22 23*Demonstration alongside the [`Error`][std::error::Error] derive macro from [`thiserror`](https://docs.rs/thiserror/1.0.25/thiserror/index.html), 24to propagate source locations from [`io::Error`][std::io::Error] with the `#[source]` attribute:* 25```rust 26use std::io; 27use displaydoc::Display; 28use thiserror::Error; 29 30#[derive(Display, Error, Debug)] 31pub enum DataStoreError { 32 /// data store disconnected 33 Disconnect(#[source] io::Error), 34 /// the data for key `{0}` is not available 35 Redaction(String), 36 /// invalid header (expected {expected:?}, found {found:?}) 37 InvalidHeader { 38 expected: String, 39 found: String, 40 }, 41 /// unknown data store error 42 Unknown, 43} 44 45let error = DataStoreError::Redaction("CLASSIFIED CONTENT".to_string()); 46assert!("the data for key `CLASSIFIED CONTENT` is not available" == &format!("{}", error)); 47``` 48*Note that although [`io::Error`][std::io::Error] implements `Display`, we do not add it to the 49generated message for `DataStoreError::Disconnect`, since it is already made available via 50`#[source]`. See further context on avoiding duplication in error reports at the rust blog 51[here](https://github.com/yaahc/blog.rust-lang.org/blob/master/posts/inside-rust/2021-05-15-What-the-error-handling-project-group-is-working-towards.md#duplicate-information-issue).* 52 53<br> 54 55### Details 56 57- A `fmt::Display` impl is generated for your enum if you provide 58 a docstring comment on each variant as shown above in the example. The 59 `Display` derive macro supports a shorthand for interpolating fields from 60 the error: 61 - `/// {var}` ⟶ `write!("{}", self.var)` 62 - `/// {0}` ⟶ `write!("{}", self.0)` 63 - `/// {var:?}` ⟶ `write!("{:?}", self.var)` 64 - `/// {0:?}` ⟶ `write!("{:?}", self.0)` 65- This also works with structs and [generic types][crate::Display#generic-type-parameters]: 66```rust 67/// oh no, an error: {0} 68#[derive(Display)] 69pub struct Error<E>(pub E); 70 71let error: Error<&str> = Error("muahaha i am an error"); 72assert!("oh no, an error: muahaha i am an error" == &format!("{}", error)); 73``` 74 75- Two optional attributes can be added to your types next to the derive: 76 77 - `#[ignore_extra_doc_attributes]` makes the macro ignore any doc 78 comment attributes (or `///` lines) after the first. Multi-line 79 comments using `///` are otherwise treated as an error, so use this 80 attribute or consider switching to block doc comments (`/** */`). 81 82 - `#[prefix_enum_doc_attributes]` combines the doc comment message on 83 your enum itself with the messages for each variant, in the format 84 “enum: variant”. When added to an enum, the doc comment on the enum 85 becomes mandatory. When added to any other type, it has no effect. 86 87- In case you want to have an independent doc comment, the 88 `#[displaydoc("...")` atrribute may be used on the variant or struct to 89 override it. 90 91<br> 92 93### FAQ 94 951. **Is this crate `no_std` compatible?** 96 * Yes! This crate implements the [`core::fmt::Display`] trait, not the [`std::fmt::Display`] trait, so it should work in `std` and `no_std` environments. Just add `default-features = false`. 97 982. **Does this crate work with `Path` and `PathBuf` via the `Display` trait?** 99 * Yuuup. This crate uses @dtolnay's [autoref specialization technique](https://github.com/dtolnay/case-studies/blob/master/autoref-specialization/README.md) to add a special trait for types to get the display impl. It then specializes for `Path` and `PathBuf`, and when either of these types are found, it calls `self.display()` to get a `std::path::Display<'_>` type which can be used with the `Display` format specifier! 100 101 102#### License 103 104<sup> 105Licensed under either of <a href="LICENSE-APACHE">Apache License, Version 1062.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option. 107</sup> 108 109<br> 110 111<sub> 112Unless you explicitly state otherwise, any contribution intentionally submitted 113for inclusion in this crate by you, as defined in the Apache-2.0 license, shall 114be dual licensed as above, without any additional terms or conditions. 115</sub> 116