@@ -27,20 +27,29 @@ fn calculate_doc_coverage(krate: clean::Crate, ctx: &DocContext<'_>) -> clean::C
2727 krate
2828}
2929
30- #[ derive( Default , Copy , Clone , Serialize ) ]
30+ #[ derive( Default , Copy , Clone , Serialize , Debug ) ]
3131struct ItemCount {
3232 total : u64 ,
3333 with_docs : u64 ,
34+ total_examples : u64 ,
3435 with_examples : u64 ,
3536}
3637
3738impl 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+ ) {
3945 self . total += 1 ;
4046
4147 if has_docs {
4248 self . with_docs += 1 ;
4349 }
50+ if should_have_doc_examples || has_doc_example {
51+ self . total_examples += 1 ;
52+ }
4453 if has_doc_example {
4554 self . with_examples += 1 ;
4655 }
@@ -55,8 +64,8 @@ impl ItemCount {
5564 }
5665
5766 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 )
6069 } else {
6170 None
6271 }
@@ -70,6 +79,7 @@ impl ops::Sub for ItemCount {
7079 ItemCount {
7180 total : self . total - rhs. total ,
7281 with_docs : self . with_docs - rhs. with_docs ,
82+ total_examples : self . total_examples - rhs. total_examples ,
7383 with_examples : self . with_examples - rhs. with_examples ,
7484 }
7585 }
@@ -79,6 +89,7 @@ impl ops::AddAssign for ItemCount {
7989 fn add_assign ( & mut self , rhs : Self ) {
8090 self . total += rhs. total ;
8191 self . with_docs += rhs. with_docs ;
92+ self . total_examples += rhs. total_examples ;
8293 self . with_examples += rhs. with_examples ;
8394 }
8495}
@@ -121,7 +132,7 @@ impl CoverageCalculator {
121132 let mut total = ItemCount :: default ( ) ;
122133
123134 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}-+" , "" ) ;
125136 }
126137
127138 fn print_table_record (
@@ -131,32 +142,25 @@ impl CoverageCalculator {
131142 examples_percentage : f64 ,
132143 ) {
133144 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,
141147 ) ;
142148 }
143149
144150 print_table_line ( ) ;
145151 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" ,
148154 ) ;
149155 print_table_line ( ) ;
150156
151157 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 ( ) {
155159 print_table_record (
156160 & limit_filename_len ( file. to_string ( ) ) ,
157161 count,
158162 percentage,
159- examples_percentage,
163+ count . examples_percentage ( ) . unwrap_or ( 0. ) ,
160164 ) ;
161165
162166 total += count;
@@ -176,19 +180,6 @@ impl CoverageCalculator {
176180
177181impl fold:: DocFolder for CoverageCalculator {
178182 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-
192183 match i. inner {
193184 _ if !i. def_id . is_local ( ) => {
194185 // non-local items are skipped because they can be out of the users control,
@@ -237,11 +228,37 @@ impl fold::DocFolder for CoverageCalculator {
237228 }
238229 }
239230 _ => {
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 ;
240256 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+ ) ;
245262 }
246263 }
247264
0 commit comments