@@ -5,6 +5,9 @@ use crate::passes::Pass;
55
66use syntax:: attr;
77
8+ use std:: ops:: Sub ;
9+ use std:: fmt;
10+
811pub const CALCULATE_DOC_COVERAGE : Pass = Pass {
912 name : "calculate-doc-coverage" ,
1013 pass : calculate_doc_coverage,
@@ -15,29 +18,66 @@ fn calculate_doc_coverage(krate: clean::Crate, _: &DocContext<'_, '_, '_>) -> cl
1518 let mut calc = CoverageCalculator :: default ( ) ;
1619 let krate = calc. fold_crate ( krate) ;
1720
18- let total_minus_traits = calc. total - calc. total_trait_impls ;
19- let docs_minus_traits = calc. with_docs - calc. trait_impls_with_docs ;
21+ let non_traits = calc. items - calc. trait_impl_items ;
2022
21- print ! ( "Rustdoc found {}/{} items with documentation" , calc. with_docs , calc . total ) ;
22- println ! ( " ({}/{} not counting trait impls)" , docs_minus_traits , total_minus_traits ) ;
23+ print ! ( "Rustdoc found {} items with documentation" , calc. items ) ;
24+ println ! ( " ({} not counting trait impls)" , non_traits ) ;
2325
24- if calc. total > 0 {
25- let percentage = ( calc. with_docs as f64 * 100.0 ) / calc. total as f64 ;
26- let percentage_minus_traits =
27- ( docs_minus_traits as f64 * 100.0 ) / total_minus_traits as f64 ;
26+ if let ( Some ( percentage) , Some ( percentage_non_traits) ) =
27+ ( calc. items . percentage ( ) , non_traits. percentage ( ) )
28+ {
2829 println ! ( " Score: {:.1}% ({:.1}% not counting trait impls)" ,
29- percentage, percentage_minus_traits ) ;
30+ percentage, percentage_non_traits ) ;
3031 }
3132
3233 krate
3334}
3435
36+ #[ derive( Default , Copy , Clone ) ]
37+ struct ItemCount {
38+ total : u64 ,
39+ with_docs : u64 ,
40+ }
41+
42+ impl ItemCount {
43+ fn count_item ( & mut self , has_docs : bool ) {
44+ self . total += 1 ;
45+
46+ if has_docs {
47+ self . with_docs += 1 ;
48+ }
49+ }
50+
51+ fn percentage ( & self ) -> Option < f64 > {
52+ if self . total > 0 {
53+ Some ( ( self . with_docs as f64 * 100.0 ) / self . total as f64 )
54+ } else {
55+ None
56+ }
57+ }
58+ }
59+
60+ impl Sub for ItemCount {
61+ type Output = Self ;
62+
63+ fn sub ( self , rhs : Self ) -> Self {
64+ ItemCount {
65+ total : self . total - rhs. total ,
66+ with_docs : self . with_docs - rhs. with_docs ,
67+ }
68+ }
69+ }
70+
71+ impl fmt:: Display for ItemCount {
72+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
73+ write ! ( f, "{}/{}" , self . with_docs, self . total)
74+ }
75+ }
76+
3577#[ derive( Default ) ]
3678struct CoverageCalculator {
37- total : usize ,
38- with_docs : usize ,
39- total_trait_impls : usize ,
40- trait_impls_with_docs : usize ,
79+ items : ItemCount ,
80+ trait_impl_items : ItemCount ,
4181}
4282
4383impl fold:: DocFolder for CoverageCalculator {
@@ -65,24 +105,15 @@ impl fold::DocFolder for CoverageCalculator {
65105 if let Some ( ref tr) = i. trait_ {
66106 debug ! ( "counting impl {:#} for {:#}" , tr, i. for_) ;
67107
68- self . total += 1 ;
69- if has_docs {
70- self . with_docs += 1 ;
71- }
108+ self . items . count_item ( has_docs) ;
72109
73110 // trait impls inherit their docs from the trait definition, so documenting
74111 // them can be considered optional
75112
76- self . total_trait_impls += 1 ;
77- if has_docs {
78- self . trait_impls_with_docs += 1 ;
79- }
113+ self . trait_impl_items . count_item ( has_docs) ;
80114
81115 for it in & i. items {
82- self . total_trait_impls += 1 ;
83- if !it. attrs . doc_strings . is_empty ( ) {
84- self . trait_impls_with_docs += 1 ;
85- }
116+ self . trait_impl_items . count_item ( !it. attrs . doc_strings . is_empty ( ) ) ;
86117 }
87118 } else {
88119 // inherent impls *can* be documented, and those docs show up, but in most
@@ -92,10 +123,7 @@ impl fold::DocFolder for CoverageCalculator {
92123 }
93124 } else {
94125 debug ! ( "counting {} {:?}" , i. type_( ) , i. name) ;
95- self . total += 1 ;
96- if has_docs {
97- self . with_docs += 1 ;
98- }
126+ self . items . count_item ( has_docs) ;
99127 }
100128 }
101129 }
0 commit comments