1
1
use proc_macro:: TokenStream ;
2
2
use quote:: { quote, ToTokens } ;
3
+ use syn:: meta:: ParseNestedMeta ;
4
+ use syn:: parse:: Result ;
3
5
use syn:: { parse_quote, Visibility } ;
4
6
5
- #[ derive( Debug ) ]
7
+ #[ derive( Debug , Default ) ]
6
8
pub ( crate ) struct UnstableAttribute {
7
9
feature : Option < String > ,
8
- issue : Option < String >
10
+ issue : Option < String > ,
9
11
}
10
12
11
13
impl UnstableAttribute {
14
+ pub ( crate ) fn parse ( & mut self , meta : ParseNestedMeta ) -> Result < ( ) > {
15
+ if meta. path . is_ident ( "feature" ) {
16
+ match meta. value ( ) ?. parse ( ) ? {
17
+ syn:: Lit :: Str ( s) => self . feature = Some ( s. value ( ) ) ,
18
+ _ => panic ! ( ) ,
19
+ }
20
+ } else if meta. path . is_ident ( "issue" ) {
21
+ match meta. value ( ) ?. parse ( ) ? {
22
+ syn:: Lit :: Str ( s) => self . issue = Some ( s. value ( ) ) ,
23
+ _ => panic ! ( ) ,
24
+ }
25
+ }
26
+ Ok ( ( ) )
27
+ }
28
+
12
29
fn crate_feature_name ( & self ) -> String {
13
30
if let Some ( name) = self . feature . as_deref ( ) {
14
31
format ! ( "unstable-{}" , name)
@@ -22,9 +39,9 @@ impl UnstableAttribute {
22
39
if item. is_public ( ) {
23
40
let feature_name = self . crate_feature_name ( ) ;
24
41
25
- if let Some ( issue) = & self . issue {
26
- let doc_addendum = format ! (
27
- "\n \
42
+ if let Some ( issue) = & self . issue {
43
+ let doc_addendum = format ! (
44
+ "\n \
28
45
# Availability\n \
29
46
\n \
30
47
**This API is marked as unstable** and is only available when \
@@ -33,27 +50,26 @@ impl UnstableAttribute {
33
50
34
51
The tracking issue is: `{}`
35
52
" ,
36
- feature_name,
37
- issue
38
- ) ;
39
- item. push_attr ( parse_quote ! {
40
- #[ doc = #doc_addendum]
41
- } ) ;
42
- } else {
43
- let doc_addendum = format ! (
44
- "\n \
53
+ feature_name, issue
54
+ ) ;
55
+ item. push_attr ( parse_quote ! {
56
+ #[ doc = #doc_addendum]
57
+ } ) ;
58
+ } else {
59
+ let doc_addendum = format ! (
60
+ "\n \
45
61
# Availability\n \
46
62
\n \
47
63
**This API is marked as unstable** and is only available when \
48
64
the `{}` crate feature is enabled. This comes with no stability \
49
65
guarantees, and could be changed or removed at any time.\
50
66
",
51
- feature_name
52
- ) ;
53
- item. push_attr ( parse_quote ! {
54
- #[ doc = #doc_addendum]
55
- } ) ;
56
- }
67
+ feature_name
68
+ ) ;
69
+ item. push_attr ( parse_quote ! {
70
+ #[ doc = #doc_addendum]
71
+ } ) ;
72
+ }
57
73
58
74
let mut hidden_item = item. clone ( ) ;
59
75
* hidden_item. visibility_mut ( ) = parse_quote ! {
@@ -74,34 +90,6 @@ The tracking issue is: `{}`
74
90
}
75
91
}
76
92
77
- impl From < syn:: AttributeArgs > for UnstableAttribute {
78
- fn from ( args : syn:: AttributeArgs ) -> Self {
79
- let mut feature = None ;
80
- let mut issue = None ;
81
-
82
- for arg in args {
83
- match arg {
84
- syn:: NestedMeta :: Meta ( syn:: Meta :: NameValue ( name_value) ) => {
85
- if name_value. path . is_ident ( "feature" ) {
86
- match name_value. lit {
87
- syn:: Lit :: Str ( s) => feature = Some ( s. value ( ) ) ,
88
- _ => panic ! ( ) ,
89
- }
90
- } else if name_value. path . is_ident ( "issue" ) {
91
- match name_value. lit {
92
- syn:: Lit :: Str ( s) => issue = Some ( s. value ( ) ) ,
93
- _ => panic ! ( )
94
- }
95
- }
96
- }
97
- _ => { }
98
- }
99
- }
100
-
101
- Self { feature, issue }
102
- }
103
- }
104
-
105
93
pub ( crate ) trait ItemLike {
106
94
fn attrs ( & self ) -> & [ syn:: Attribute ] ;
107
95
0 commit comments