@@ -5,6 +5,9 @@ use crate::passes::Pass;
5
5
6
6
use syntax:: attr;
7
7
8
+ use std:: ops:: Sub ;
9
+ use std:: fmt;
10
+
8
11
pub const CALCULATE_DOC_COVERAGE : Pass = Pass {
9
12
name : "calculate-doc-coverage" ,
10
13
pass : calculate_doc_coverage,
@@ -15,29 +18,66 @@ fn calculate_doc_coverage(krate: clean::Crate, _: &DocContext<'_, '_, '_>) -> cl
15
18
let mut calc = CoverageCalculator :: default ( ) ;
16
19
let krate = calc. fold_crate ( krate) ;
17
20
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 ;
20
22
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 ) ;
23
25
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
+ {
28
29
println ! ( " Score: {:.1}% ({:.1}% not counting trait impls)" ,
29
- percentage, percentage_minus_traits ) ;
30
+ percentage, percentage_non_traits ) ;
30
31
}
31
32
32
33
krate
33
34
}
34
35
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
+
35
77
#[ derive( Default ) ]
36
78
struct 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 ,
41
81
}
42
82
43
83
impl fold:: DocFolder for CoverageCalculator {
@@ -65,24 +105,15 @@ impl fold::DocFolder for CoverageCalculator {
65
105
if let Some ( ref tr) = i. trait_ {
66
106
debug ! ( "counting impl {:#} for {:#}" , tr, i. for_) ;
67
107
68
- self . total += 1 ;
69
- if has_docs {
70
- self . with_docs += 1 ;
71
- }
108
+ self . items . count_item ( has_docs) ;
72
109
73
110
// trait impls inherit their docs from the trait definition, so documenting
74
111
// them can be considered optional
75
112
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) ;
80
114
81
115
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 ( ) ) ;
86
117
}
87
118
} else {
88
119
// inherent impls *can* be documented, and those docs show up, but in most
@@ -92,10 +123,7 @@ impl fold::DocFolder for CoverageCalculator {
92
123
}
93
124
} else {
94
125
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) ;
99
127
}
100
128
}
101
129
}
0 commit comments