@@ -27,20 +27,29 @@ fn calculate_doc_coverage(krate: clean::Crate, ctx: &DocContext<'_>) -> clean::C
27
27
krate
28
28
}
29
29
30
- #[ derive( Default , Copy , Clone , Serialize ) ]
30
+ #[ derive( Default , Copy , Clone , Serialize , Debug ) ]
31
31
struct ItemCount {
32
32
total : u64 ,
33
33
with_docs : u64 ,
34
+ total_examples : u64 ,
34
35
with_examples : u64 ,
35
36
}
36
37
37
38
impl ItemCount {
38
- fn count_item ( & mut self , has_docs : bool , has_doc_example : bool ) {
39
+ fn count_item (
40
+ & mut self ,
41
+ has_docs : bool ,
42
+ has_doc_example : bool ,
43
+ should_have_doc_examples : bool ,
44
+ ) {
39
45
self . total += 1 ;
40
46
41
47
if has_docs {
42
48
self . with_docs += 1 ;
43
49
}
50
+ if should_have_doc_examples || has_doc_example {
51
+ self . total_examples += 1 ;
52
+ }
44
53
if has_doc_example {
45
54
self . with_examples += 1 ;
46
55
}
@@ -55,8 +64,8 @@ impl ItemCount {
55
64
}
56
65
57
66
fn examples_percentage ( & self ) -> Option < f64 > {
58
- if self . total > 0 {
59
- Some ( ( self . with_examples as f64 * 100.0 ) / self . total as f64 )
67
+ if self . total_examples > 0 {
68
+ Some ( ( self . with_examples as f64 * 100.0 ) / self . total_examples as f64 )
60
69
} else {
61
70
None
62
71
}
@@ -70,6 +79,7 @@ impl ops::Sub for ItemCount {
70
79
ItemCount {
71
80
total : self . total - rhs. total ,
72
81
with_docs : self . with_docs - rhs. with_docs ,
82
+ total_examples : self . total_examples - rhs. total_examples ,
73
83
with_examples : self . with_examples - rhs. with_examples ,
74
84
}
75
85
}
@@ -79,6 +89,7 @@ impl ops::AddAssign for ItemCount {
79
89
fn add_assign ( & mut self , rhs : Self ) {
80
90
self . total += rhs. total ;
81
91
self . with_docs += rhs. with_docs ;
92
+ self . total_examples += rhs. total_examples ;
82
93
self . with_examples += rhs. with_examples ;
83
94
}
84
95
}
@@ -121,7 +132,7 @@ impl CoverageCalculator {
121
132
let mut total = ItemCount :: default ( ) ;
122
133
123
134
fn print_table_line ( ) {
124
- println ! ( "+-{0:->35}-+-{0:->10}-+-{0:->10}-+-{0:->10}-+-{0:->10}-+-{0:->10}-+ " , "" ) ;
135
+ println ! ( "+-{0:->35}-+-{0:->10}-+-{0:->10}-+-{0:->10}-+-{0:->10}-+" , "" ) ;
125
136
}
126
137
127
138
fn print_table_record (
@@ -131,32 +142,25 @@ impl CoverageCalculator {
131
142
examples_percentage : f64 ,
132
143
) {
133
144
println ! (
134
- "| {:<35} | {:>10} | {:>10} | {:>9.1}% | {:>10} | {:>9.1}% |" ,
135
- name,
136
- count. with_docs,
137
- count. total,
138
- percentage,
139
- count. with_examples,
140
- examples_percentage,
145
+ "| {:<35} | {:>10} | {:>9.1}% | {:>10} | {:>9.1}% |" ,
146
+ name, count. with_docs, percentage, count. with_examples, examples_percentage,
141
147
) ;
142
148
}
143
149
144
150
print_table_line ( ) ;
145
151
println ! (
146
- "| {:<35} | {:>10} | {:>10} | {:>10} | {:>10} | {:>10} | " ,
147
- "File" , "Documented" , "Total" , " Percentage", "Examples" , "Percentage" ,
152
+ "| {:<35} | {:>10} | {:>10} | {:>10} | {:>10} |" ,
153
+ "File" , "Documented" , "Percentage" , "Examples" , "Percentage" ,
148
154
) ;
149
155
print_table_line ( ) ;
150
156
151
157
for ( file, & count) in & self . items {
152
- if let ( Some ( percentage) , Some ( examples_percentage) ) =
153
- ( count. percentage ( ) , count. examples_percentage ( ) )
154
- {
158
+ if let Some ( percentage) = count. percentage ( ) {
155
159
print_table_record (
156
160
& limit_filename_len ( file. to_string ( ) ) ,
157
161
count,
158
162
percentage,
159
- examples_percentage,
163
+ count . examples_percentage ( ) . unwrap_or ( 0. ) ,
160
164
) ;
161
165
162
166
total += count;
@@ -176,19 +180,6 @@ impl CoverageCalculator {
176
180
177
181
impl fold:: DocFolder for CoverageCalculator {
178
182
fn fold_item ( & mut self , i : clean:: Item ) -> Option < clean:: Item > {
179
- let has_docs = !i. attrs . doc_strings . is_empty ( ) ;
180
- let mut tests = Tests { found_tests : 0 } ;
181
-
182
- find_testable_code (
183
- & i. attrs . doc_strings . iter ( ) . map ( |d| d. as_str ( ) ) . collect :: < Vec < _ > > ( ) . join ( "\n " ) ,
184
- & mut tests,
185
- ErrorCodes :: No ,
186
- false ,
187
- None ,
188
- ) ;
189
-
190
- let has_doc_example = tests. found_tests != 0 ;
191
-
192
183
match i. inner {
193
184
_ if !i. def_id . is_local ( ) => {
194
185
// non-local items are skipped because they can be out of the users control,
@@ -237,11 +228,37 @@ impl fold::DocFolder for CoverageCalculator {
237
228
}
238
229
}
239
230
_ => {
231
+ let has_docs = !i. attrs . doc_strings . is_empty ( ) ;
232
+ let mut tests = Tests { found_tests : 0 } ;
233
+
234
+ let should_have_doc_examples = !matches ! ( i. inner,
235
+ clean:: StructFieldItem ( _)
236
+ | clean:: VariantItem ( _)
237
+ | clean:: AssocConstItem ( _, _)
238
+ | clean:: AssocTypeItem ( _, _)
239
+ | clean:: TypedefItem ( _, _)
240
+ | clean:: StaticItem ( _)
241
+ | clean:: ConstantItem ( _)
242
+ | clean:: ExternCrateItem ( _, _)
243
+ | clean:: ImportItem ( _)
244
+ | clean:: PrimitiveItem ( _)
245
+ | clean:: KeywordItem ( _)
246
+ ) ;
247
+ find_testable_code (
248
+ & i. attrs . doc_strings . iter ( ) . map ( |d| d. as_str ( ) ) . collect :: < Vec < _ > > ( ) . join ( "\n " ) ,
249
+ & mut tests,
250
+ ErrorCodes :: No ,
251
+ false ,
252
+ None ,
253
+ ) ;
254
+
255
+ let has_doc_example = tests. found_tests != 0 ;
240
256
debug ! ( "counting {:?} {:?} in {}" , i. type_( ) , i. name, i. source. filename) ;
241
- self . items
242
- . entry ( i. source . filename . clone ( ) )
243
- . or_default ( )
244
- . count_item ( has_docs, has_doc_example) ;
257
+ self . items . entry ( i. source . filename . clone ( ) ) . or_default ( ) . count_item (
258
+ has_docs,
259
+ has_doc_example,
260
+ should_have_doc_examples,
261
+ ) ;
245
262
}
246
263
}
247
264
0 commit comments