@@ -140,17 +140,14 @@ impl Attribute {
140
140
141
141
pub fn value_str ( & self ) -> Option < Symbol > {
142
142
match & self . kind {
143
- AttrKind :: Normal ( normal) => normal. item . meta_kind ( ) . and_then ( |kind| kind . value_str ( ) ) ,
143
+ AttrKind :: Normal ( normal) => normal. item . value_str ( ) ,
144
144
AttrKind :: DocComment ( ..) => None ,
145
145
}
146
146
}
147
147
148
148
pub fn meta_item_list ( & self ) -> Option < Vec < NestedMetaItem > > {
149
149
match & self . kind {
150
- AttrKind :: Normal ( normal) => match normal. item . meta_kind ( ) {
151
- Some ( MetaItemKind :: List ( list) ) => Some ( list) ,
152
- _ => None ,
153
- } ,
150
+ AttrKind :: Normal ( normal) => normal. item . meta_item_list ( ) ,
154
151
AttrKind :: DocComment ( ..) => None ,
155
152
}
156
153
}
@@ -216,6 +213,20 @@ impl MetaItem {
216
213
}
217
214
}
218
215
216
+ impl AttrArgsEq {
217
+ fn value_str ( & self ) -> Option < Symbol > {
218
+ match self {
219
+ AttrArgsEq :: Ast ( expr) => match expr. kind {
220
+ ExprKind :: Lit ( token_lit) => {
221
+ LitKind :: from_token_lit ( token_lit) . ok ( ) . and_then ( |lit| lit. str ( ) )
222
+ }
223
+ _ => None ,
224
+ } ,
225
+ AttrArgsEq :: Hir ( lit) => lit. kind . str ( ) ,
226
+ }
227
+ }
228
+ }
229
+
219
230
impl AttrItem {
220
231
pub fn span ( & self ) -> Span {
221
232
self . args . span ( ) . map_or ( self . path . span , |args_span| self . path . span . to ( args_span) )
@@ -228,6 +239,22 @@ impl AttrItem {
228
239
pub fn meta_kind ( & self ) -> Option < MetaItemKind > {
229
240
MetaItemKind :: from_attr_args ( & self . args )
230
241
}
242
+
243
+ fn meta_item_list ( & self ) -> Option < Vec < NestedMetaItem > > {
244
+ match & self . args {
245
+ AttrArgs :: Delimited ( args) if args. delim == MacDelimiter :: Parenthesis => {
246
+ MetaItemKind :: list_from_tokens ( args. tokens . clone ( ) )
247
+ }
248
+ AttrArgs :: Delimited ( _) | AttrArgs :: Eq ( ..) | AttrArgs :: Empty => None ,
249
+ }
250
+ }
251
+
252
+ fn value_str ( & self ) -> Option < Symbol > {
253
+ match & self . args {
254
+ AttrArgs :: Eq ( _, args) => args. value_str ( ) ,
255
+ AttrArgs :: Delimited ( _) | AttrArgs :: Empty => None ,
256
+ }
257
+ }
231
258
}
232
259
233
260
impl Attribute {
@@ -247,13 +274,11 @@ impl Attribute {
247
274
/// * `#[doc = "doc"]` returns `Some(("doc", CommentKind::Line))`.
248
275
/// * `#[doc(...)]` returns `None`.
249
276
pub fn doc_str_and_comment_kind ( & self ) -> Option < ( Symbol , CommentKind ) > {
250
- match self . kind {
251
- AttrKind :: DocComment ( kind, data) => Some ( ( data, kind) ) ,
252
- AttrKind :: Normal ( ref normal) if normal. item . path == sym:: doc => normal
253
- . item
254
- . meta_kind ( )
255
- . and_then ( |kind| kind. value_str ( ) )
256
- . map ( |data| ( data, CommentKind :: Line ) ) ,
277
+ match & self . kind {
278
+ AttrKind :: DocComment ( kind, data) => Some ( ( * data, * kind) ) ,
279
+ AttrKind :: Normal ( normal) if normal. item . path == sym:: doc => {
280
+ normal. item . value_str ( ) . map ( |s| ( s, CommentKind :: Line ) )
281
+ }
257
282
_ => None ,
258
283
}
259
284
}
@@ -265,9 +290,7 @@ impl Attribute {
265
290
pub fn doc_str ( & self ) -> Option < Symbol > {
266
291
match & self . kind {
267
292
AttrKind :: DocComment ( .., data) => Some ( * data) ,
268
- AttrKind :: Normal ( normal) if normal. item . path == sym:: doc => {
269
- normal. item . meta_kind ( ) . and_then ( |kind| kind. value_str ( ) )
270
- }
293
+ AttrKind :: Normal ( normal) if normal. item . path == sym:: doc => normal. item . value_str ( ) ,
271
294
_ => None ,
272
295
}
273
296
}
@@ -508,15 +531,12 @@ impl MetaItem {
508
531
impl MetaItemKind {
509
532
pub fn value_str ( & self ) -> Option < Symbol > {
510
533
match self {
511
- MetaItemKind :: NameValue ( v) => match v. kind {
512
- LitKind :: Str ( s, _) => Some ( s) ,
513
- _ => None ,
514
- } ,
534
+ MetaItemKind :: NameValue ( v) => v. kind . str ( ) ,
515
535
_ => None ,
516
536
}
517
537
}
518
538
519
- fn list_from_tokens ( tokens : TokenStream ) -> Option < MetaItemKind > {
539
+ fn list_from_tokens ( tokens : TokenStream ) -> Option < Vec < NestedMetaItem > > {
520
540
let mut tokens = tokens. into_trees ( ) . peekable ( ) ;
521
541
let mut result = Vec :: new ( ) ;
522
542
while tokens. peek ( ) . is_some ( ) {
@@ -527,7 +547,7 @@ impl MetaItemKind {
527
547
_ => return None ,
528
548
}
529
549
}
530
- Some ( MetaItemKind :: List ( result) )
550
+ Some ( result)
531
551
}
532
552
533
553
fn name_value_from_tokens (
@@ -551,7 +571,7 @@ impl MetaItemKind {
551
571
dspan : _,
552
572
delim : MacDelimiter :: Parenthesis ,
553
573
tokens,
554
- } ) => MetaItemKind :: list_from_tokens ( tokens. clone ( ) ) ,
574
+ } ) => MetaItemKind :: list_from_tokens ( tokens. clone ( ) ) . map ( MetaItemKind :: List ) ,
555
575
AttrArgs :: Delimited ( ..) => None ,
556
576
AttrArgs :: Eq ( _, AttrArgsEq :: Ast ( expr) ) => match expr. kind {
557
577
ExprKind :: Lit ( token_lit) => {
@@ -573,7 +593,7 @@ impl MetaItemKind {
573
593
Some ( TokenTree :: Delimited ( _, Delimiter :: Parenthesis , inner_tokens) ) => {
574
594
let inner_tokens = inner_tokens. clone ( ) ;
575
595
tokens. next ( ) ;
576
- MetaItemKind :: list_from_tokens ( inner_tokens)
596
+ MetaItemKind :: list_from_tokens ( inner_tokens) . map ( MetaItemKind :: List )
577
597
}
578
598
Some ( TokenTree :: Delimited ( ..) ) => None ,
579
599
Some ( TokenTree :: Token ( Token { kind : token:: Eq , .. } , _) ) => {
0 commit comments