Skip to content

Commit c59c084

Browse files
authored
Merge pull request #31 from Manishearth/displaydoc-attr
Add #[displaydoc] attribute
2 parents 194718c + 3d27f6b commit c59c084

File tree

7 files changed

+69
-13
lines changed

7 files changed

+69
-13
lines changed

CHANGELOG.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
<!-- next-header -->
88

9-
## [Unreleased] - ReleaseDate
9+
# [Unreleased] - ReleaseDate
10+
## Added
11+
- Added `#[displaydoc("..")]` attribute for overriding a doc comment
1012

11-
## [0.2.2] - 2021-07-01
12-
# Added
13+
# [0.2.2] - 2021-07-01
14+
## Added
1315
- Added prefix feature to use the doc comment from an enum and prepend it
1416
before the error message from each variant.
1517

16-
## [0.2.1] - 2021-03-26
17-
# Added
18+
# [0.2.1] - 2021-03-26
19+
## Added
1820
- Added opt in support for ignoring extra doc attributes
1921

20-
## [0.2.0] - 2021-03-16
21-
# Changed
22+
# [0.2.0] - 2021-03-16
23+
## Changed
2224

2325
- (BREAKING) disallow multiple `doc` attributes in display impl
2426
[https://github.com/yaahc/displaydoc/pull/22]. Allowing and ignoring extra

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ A derive macro for implementing the display Trait via a doc comment and string i
1515

1616
[lib]
1717
proc-macro = true
18+
path = "src/lib.rs"
1819

1920
[features]
2021
default = ["std"]

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ displaydoc = "0.2"
1818

1919
<br>
2020

21-
## Example
21+
### Example
2222

2323
```rust
2424
use std::io;
@@ -43,7 +43,7 @@ pub enum DataStoreError {
4343

4444
<br>
4545

46-
## Details
46+
### Details
4747

4848
- A `Display` impl is generated for your type if you provide doc comment
4949
messages on the struct or each variant of your enum, as shown above in the
@@ -68,17 +68,20 @@ pub enum DataStoreError {
6868
“enum: variant”. When added to an enum, the doc comment on the enum
6969
becomes mandatory. When added to any other type, it has no effect.
7070

71+
- In case you want to have an independent doc comment, the
72+
`#[displaydoc("...")` atrribute may be used on the variant or struct to
73+
override it.
74+
7175
<br>
7276

73-
## FAQ
77+
### FAQ
7478

7579
1. **Is this crate `no_std` compatible?**
7680
* 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`.
7781

7882
2. **Does this crate work with `Path` and `PathBuf` via the `Display` trait?**
7983
* 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!
8084

81-
<br>
8285

8386
#### License
8487

README.tpl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
derive(Display) /// `From<docs>`
2+
===============
3+
4+
[![Latest Version](https://img.shields.io/crates/v/displaydoc.svg)](https://crates.io/crates/displaydoc)
5+
[![Rust Documentation](https://img.shields.io/badge/api-rustdoc-blue.svg)](https://docs.rs/displaydoc)
6+
7+
{{readme}}
8+
9+
10+
#### License
11+
12+
<sup>
13+
Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
14+
2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
15+
</sup>
16+
17+
<br>
18+
19+
<sub>
20+
Unless you explicitly state otherwise, any contribution intentionally submitted
21+
for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
22+
be dual licensed as above, without any additional terms or conditions.
23+
</sub>

src/attr.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ impl AttrsHelper {
5454
}
5555

5656
pub(crate) fn display(&self, attrs: &[Attribute]) -> Result<Option<Display>> {
57+
let displaydoc_attr = attrs.iter().find(|attr| attr.path.is_ident("displaydoc"));
58+
59+
if let Some(displaydoc_attr) = displaydoc_attr {
60+
let lit = displaydoc_attr
61+
.parse_args()
62+
.expect("#[displaydoc(\"foo\")] must contain string arguments");
63+
let mut display = Display {
64+
fmt: lit,
65+
args: TokenStream::new(),
66+
};
67+
68+
display.expand_shorthand();
69+
return Ok(Some(display));
70+
}
71+
5772
let num_doc_attrs = attrs
5873
.iter()
5974
.filter(|attr| attr.path.is_ident("doc"))

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@
6262
//! “enum: variant”. When added to an enum, the doc comment on the enum
6363
//! becomes mandatory. When added to any other type, it has no effect.
6464
//!
65+
//! - In case you want to have an independent doc comment, the
66+
//! `#[displaydoc("...")` atrribute may be used on the variant or struct to
67+
//! override it.
68+
//!
6569
//! <br>
6670
//!
6771
//! ## FAQ
@@ -108,7 +112,7 @@ use syn::{parse_macro_input, DeriveInput};
108112
/// Derive macro for implementing `Display` via doc comment attributes
109113
#[proc_macro_derive(
110114
Display,
111-
attributes(ignore_extra_doc_attributes, prefix_enum_doc_attributes,)
115+
attributes(ignore_extra_doc_attributes, prefix_enum_doc_attributes, displaydoc)
112116
)]
113117
pub fn derive_error(input: TokenStream) -> TokenStream {
114118
let input = parse_macro_input!(input as DeriveInput);

tests/happy.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ enum Happy {
3838
/// The path {0}
3939
#[cfg(feature = "std")]
4040
Variant6(PathBuf),
41+
42+
/// These docs are ignored
43+
#[displaydoc("Variant7 has a parameter {0} and uses #[displaydoc]")]
44+
/// These docs are also ignored
45+
Variant7(u32),
4146
}
4247

4348
// Used for testing indented doc comments
@@ -98,7 +103,10 @@ fn does_it_print() {
98103
Happy::Variant5(2),
99104
"Variant5 has a parameter 2 and some regular comments",
100105
);
101-
106+
assert_display(
107+
Happy::Variant7(2),
108+
"Variant7 has a parameter 2 and uses #[displaydoc]",
109+
);
102110
assert_display(HappyStruct { thing: "hi" }, "Just a basic struct hi");
103111

104112
assert_display(HappyStruct2 { thing: "hi2" }, "Just a basic struct hi2");

0 commit comments

Comments
 (0)