@@ -46,6 +46,7 @@ use std::rc::Rc;
46
46
use std:: str;
47
47
use std:: string:: ToString ;
48
48
49
+ use askama:: Template ;
49
50
use rustc_ast_pretty:: pprust;
50
51
use rustc_attr:: { ConstStability , Deprecation , StabilityLevel } ;
51
52
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
@@ -417,7 +418,7 @@ fn document(
417
418
if let Some ( ref name) = item. name {
418
419
info ! ( "Documenting {}" , name) ;
419
420
}
420
- document_item_info ( w , cx, item, parent) ;
421
+ document_item_info ( cx, item, parent) . render_into ( w ) . unwrap ( ) ;
421
422
if parent. is_none ( ) {
422
423
document_full_collapsible ( w, item, cx, heading_offset) ;
423
424
} else {
@@ -459,7 +460,7 @@ fn document_short(
459
460
parent : & clean:: Item ,
460
461
show_def_docs : bool ,
461
462
) {
462
- document_item_info ( w , cx, item, Some ( parent) ) ;
463
+ document_item_info ( cx, item, Some ( parent) ) . render_into ( w ) . unwrap ( ) ;
463
464
if !show_def_docs {
464
465
return ;
465
466
}
@@ -531,25 +532,23 @@ fn document_full_inner(
531
532
}
532
533
}
533
534
535
+ #[ derive( Template ) ]
536
+ #[ template( path = "item_info.html" ) ]
537
+ struct ItemInfo {
538
+ items : Vec < ShortItemInfo > ,
539
+ }
534
540
/// Add extra information about an item such as:
535
541
///
536
542
/// * Stability
537
543
/// * Deprecated
538
544
/// * Required features (through the `doc_cfg` feature)
539
545
fn document_item_info (
540
- w : & mut Buffer ,
541
546
cx : & mut Context < ' _ > ,
542
547
item : & clean:: Item ,
543
548
parent : Option < & clean:: Item > ,
544
- ) {
545
- let item_infos = short_item_info ( item, cx, parent) ;
546
- if !item_infos. is_empty ( ) {
547
- w. write_str ( "<span class=\" item-info\" >" ) ;
548
- for info in item_infos {
549
- w. write_str ( & info) ;
550
- }
551
- w. write_str ( "</span>" ) ;
552
- }
549
+ ) -> ItemInfo {
550
+ let items = short_item_info ( item, cx, parent) ;
551
+ ItemInfo { items }
553
552
}
554
553
555
554
fn portability ( item : & clean:: Item , parent : Option < & clean:: Item > ) -> Option < String > {
@@ -567,7 +566,25 @@ fn portability(item: &clean::Item, parent: Option<&clean::Item>) -> Option<Strin
567
566
cfg
568
567
) ;
569
568
570
- Some ( format ! ( "<div class=\" stab portability\" >{}</div>" , cfg?. render_long_html( ) ) )
569
+ Some ( cfg?. render_long_html ( ) )
570
+ }
571
+
572
+ #[ derive( Template ) ]
573
+ #[ template( path = "short_item_info.html" ) ]
574
+ enum ShortItemInfo {
575
+ /// A message describing the deprecation of this item
576
+ Deprecation {
577
+ message : String ,
578
+ } ,
579
+ /// The feature corresponding to an unstable item, and optionally
580
+ /// a tracking issue URL and number.
581
+ Unstable {
582
+ feature : String ,
583
+ tracking : Option < ( String , u32 ) > ,
584
+ } ,
585
+ Portability {
586
+ message : String ,
587
+ } ,
571
588
}
572
589
573
590
/// Render the stability, deprecation and portability information that is displayed at the top of
@@ -576,7 +593,7 @@ fn short_item_info(
576
593
item : & clean:: Item ,
577
594
cx : & mut Context < ' _ > ,
578
595
parent : Option < & clean:: Item > ,
579
- ) -> Vec < String > {
596
+ ) -> Vec < ShortItemInfo > {
580
597
let mut extra_info = vec ! [ ] ;
581
598
582
599
if let Some ( depr @ Deprecation { note, since, is_since_rustc_version : _, suggestion : _ } ) =
@@ -602,15 +619,10 @@ fn short_item_info(
602
619
if let Some ( note) = note {
603
620
let note = note. as_str ( ) ;
604
621
let html = MarkdownItemInfo ( note, & mut cx. id_map ) ;
605
- message. push_str ( & format ! ( ": {}" , html. into_string( ) ) ) ;
622
+ message. push_str ( ": " ) ;
623
+ message. push_str ( & html. into_string ( ) ) ;
606
624
}
607
- extra_info. push ( format ! (
608
- "<div class=\" stab deprecated\" >\
609
- <span class=\" emoji\" >👎</span>\
610
- <span>{}</span>\
611
- </div>",
612
- message,
613
- ) ) ;
625
+ extra_info. push ( ShortItemInfo :: Deprecation { message } ) ;
614
626
}
615
627
616
628
// Render unstable items. But don't render "rustc_private" crates (internal compiler crates).
@@ -621,26 +633,17 @@ fn short_item_info(
621
633
. filter ( |stab| stab. feature != sym:: rustc_private)
622
634
. map ( |stab| ( stab. level , stab. feature ) )
623
635
{
624
- let mut message = "<span class=\" emoji\" >🔬</span>\
625
- <span>This is a nightly-only experimental API."
626
- . to_owned ( ) ;
627
-
628
- let mut feature = format ! ( "<code>{}</code>" , Escape ( feature. as_str( ) ) ) ;
629
- if let ( Some ( url) , Some ( issue) ) = ( & cx. shared . issue_tracker_base_url , issue) {
630
- feature. push_str ( & format ! (
631
- " <a href=\" {url}{issue}\" >#{issue}</a>" ,
632
- url = url,
633
- issue = issue
634
- ) ) ;
635
- }
636
-
637
- message. push_str ( & format ! ( " ({})</span>" , feature) ) ;
638
-
639
- extra_info. push ( format ! ( "<div class=\" stab unstable\" >{}</div>" , message) ) ;
636
+ let tracking = if let ( Some ( url) , Some ( issue) ) = ( & cx. shared . issue_tracker_base_url , issue)
637
+ {
638
+ Some ( ( url. clone ( ) , issue. get ( ) ) )
639
+ } else {
640
+ None
641
+ } ;
642
+ extra_info. push ( ShortItemInfo :: Unstable { feature : feature. to_string ( ) , tracking } ) ;
640
643
}
641
644
642
- if let Some ( portability ) = portability ( item, parent) {
643
- extra_info. push ( portability ) ;
645
+ if let Some ( message ) = portability ( item, parent) {
646
+ extra_info. push ( ShortItemInfo :: Portability { message } ) ;
644
647
}
645
648
646
649
extra_info
@@ -1472,7 +1475,9 @@ fn render_impl(
1472
1475
// We need the stability of the item from the trait
1473
1476
// because impls can't have a stability.
1474
1477
if item. doc_value ( ) . is_some ( ) {
1475
- document_item_info ( & mut info_buffer, cx, it, Some ( parent) ) ;
1478
+ document_item_info ( cx, it, Some ( parent) )
1479
+ . render_into ( & mut info_buffer)
1480
+ . unwrap ( ) ;
1476
1481
document_full ( & mut doc_buffer, item, cx, HeadingOffset :: H5 ) ;
1477
1482
short_documented = false ;
1478
1483
} else {
@@ -1489,7 +1494,9 @@ fn render_impl(
1489
1494
}
1490
1495
}
1491
1496
} else {
1492
- document_item_info ( & mut info_buffer, cx, item, Some ( parent) ) ;
1497
+ document_item_info ( cx, item, Some ( parent) )
1498
+ . render_into ( & mut info_buffer)
1499
+ . unwrap ( ) ;
1493
1500
if rendering_params. show_def_docs {
1494
1501
document_full ( & mut doc_buffer, item, cx, HeadingOffset :: H5 ) ;
1495
1502
short_documented = false ;
@@ -1862,7 +1869,11 @@ pub(crate) fn render_impl_summary(
1862
1869
let is_trait = inner_impl. trait_ . is_some ( ) ;
1863
1870
if is_trait {
1864
1871
if let Some ( portability) = portability ( & i. impl_item , Some ( parent) ) {
1865
- write ! ( w, "<span class=\" item-info\" >{}</span>" , portability) ;
1872
+ write ! (
1873
+ w,
1874
+ "<span class=\" item-info\" ><div class=\" stab portability\" >{}</div></span>" ,
1875
+ portability
1876
+ ) ;
1866
1877
}
1867
1878
}
1868
1879
0 commit comments