@@ -258,7 +258,7 @@ pub struct Item {
258
258
pub source : Span ,
259
259
/// Not everything has a name. E.g., impls
260
260
pub name : Option < String > ,
261
- pub attrs : Vec < Attribute > ,
261
+ pub attrs : Vec < Attribute > ,
262
262
pub inner : ItemEnum ,
263
263
pub visibility : Option < Visibility > ,
264
264
pub def_id : DefId ,
@@ -267,49 +267,10 @@ pub struct Item {
267
267
}
268
268
269
269
impl Item {
270
- /// Finds the `doc` attribute as a List and returns the list of attributes
271
- /// nested inside.
272
- pub fn doc_list < ' a > ( & ' a self ) -> Option < & ' a [ Attribute ] > {
273
- for attr in & self . attrs {
274
- match * attr {
275
- List ( ref x, ref list) if "doc" == * x => {
276
- return Some ( list) ;
277
- }
278
- _ => { }
279
- }
280
- }
281
- return None ;
282
- }
283
-
284
270
/// Finds the `doc` attribute as a NameValue and returns the corresponding
285
271
/// value found.
286
272
pub fn doc_value < ' a > ( & ' a self ) -> Option < & ' a str > {
287
- for attr in & self . attrs {
288
- match * attr {
289
- NameValue ( ref x, ref v) if "doc" == * x => {
290
- return Some ( v) ;
291
- }
292
- _ => { }
293
- }
294
- }
295
- return None ;
296
- }
297
-
298
- pub fn is_hidden_from_doc ( & self ) -> bool {
299
- match self . doc_list ( ) {
300
- Some ( l) => {
301
- for innerattr in l {
302
- match * innerattr {
303
- Word ( ref s) if "hidden" == * s => {
304
- return true
305
- }
306
- _ => ( ) ,
307
- }
308
- }
309
- } ,
310
- None => ( )
311
- }
312
- return false ;
273
+ self . attrs . value ( "doc" )
313
274
}
314
275
315
276
pub fn is_mod ( & self ) -> bool {
@@ -438,10 +399,54 @@ impl Clean<Item> for doctree::Module {
438
399
}
439
400
}
440
401
402
+ pub trait Attributes {
403
+ fn has_word ( & self , & str ) -> bool ;
404
+ fn value < ' a > ( & ' a self , & str ) -> Option < & ' a str > ;
405
+ fn list_def < ' a > ( & ' a self , & str ) -> & ' a [ Attribute ] ;
406
+ }
407
+
408
+ impl Attributes for [ Attribute ] {
409
+ /// Returns whether the attribute list contains a specific `Word`
410
+ fn has_word ( & self , word : & str ) -> bool {
411
+ for attr in self {
412
+ if let Word ( ref w) = * attr {
413
+ if word == * w {
414
+ return true ;
415
+ }
416
+ }
417
+ }
418
+ false
419
+ }
420
+
421
+ /// Finds an attribute as NameValue and returns the corresponding value found.
422
+ fn value < ' a > ( & ' a self , name : & str ) -> Option < & ' a str > {
423
+ for attr in self {
424
+ if let NameValue ( ref x, ref v) = * attr {
425
+ if name == * x {
426
+ return Some ( v) ;
427
+ }
428
+ }
429
+ }
430
+ None
431
+ }
432
+
433
+ /// Finds an attribute as List and returns the list of attributes nested inside.
434
+ fn list_def < ' a > ( & ' a self , name : & str ) -> & ' a [ Attribute ] {
435
+ for attr in self {
436
+ if let List ( ref x, ref list) = * attr {
437
+ if name == * x {
438
+ return & list[ ..] ;
439
+ }
440
+ }
441
+ }
442
+ & [ ]
443
+ }
444
+ }
445
+
441
446
#[ derive( Clone , RustcEncodable , RustcDecodable , PartialEq , Debug ) ]
442
447
pub enum Attribute {
443
448
Word ( String ) ,
444
- List ( String , Vec < Attribute > ) ,
449
+ List ( String , Vec < Attribute > ) ,
445
450
NameValue ( String , String )
446
451
}
447
452
@@ -1513,24 +1518,16 @@ impl PrimitiveType {
1513
1518
}
1514
1519
1515
1520
fn find ( attrs : & [ Attribute ] ) -> Option < PrimitiveType > {
1516
- for attr in attrs {
1517
- let list = match * attr {
1518
- List ( ref k, ref l) if * k == "doc" => l,
1519
- _ => continue ,
1520
- } ;
1521
- for sub_attr in list {
1522
- let value = match * sub_attr {
1523
- NameValue ( ref k, ref v)
1524
- if * k == "primitive" => v,
1525
- _ => continue ,
1526
- } ;
1527
- match PrimitiveType :: from_str ( value) {
1528
- Some ( p) => return Some ( p) ,
1529
- None => { }
1521
+ for attr in attrs. list_def ( "doc" ) {
1522
+ if let NameValue ( ref k, ref v) = * attr {
1523
+ if "primitive" == * k {
1524
+ if let ret@Some ( ..) = PrimitiveType :: from_str ( v) {
1525
+ return ret;
1526
+ }
1530
1527
}
1531
1528
}
1532
1529
}
1533
- return None
1530
+ None
1534
1531
}
1535
1532
1536
1533
pub fn to_string ( & self ) -> & ' static str {
0 commit comments