@@ -7,7 +7,8 @@ use crate::passes::Pass;
77use rustc_ast:: attr;
88use rustc_span:: symbol:: sym;
99use rustc_span:: FileName ;
10- use serialize:: json:: { ToJson , Json } ;
10+ use serde:: Serialize ;
11+ use serde_json;
1112
1213use std:: collections:: BTreeMap ;
1314use std:: ops;
@@ -18,16 +19,16 @@ pub const CALCULATE_DOC_COVERAGE: Pass = Pass {
1819 description : "counts the number of items with and without documentation" ,
1920} ;
2021
21- fn calculate_doc_coverage ( krate : clean:: Crate , ctx : & DocContext < ' _ > ) -> clean:: Crate {
22- let mut calc = CoverageCalculator :: new ( ctx . renderinfo . borrow ( ) . output_format ) ;
22+ fn calculate_doc_coverage ( krate : clean:: Crate , ctx : & DocContext < ' _ > ) -> clean:: Crate {
23+ let mut calc = CoverageCalculator :: new ( ) ;
2324 let krate = calc. fold_crate ( krate) ;
2425
25- calc. print_results ( ) ;
26+ calc. print_results ( ctx . renderinfo . borrow ( ) . output_format ) ;
2627
2728 krate
2829}
2930
30- #[ derive( Default , Copy , Clone ) ]
31+ #[ derive( Default , Copy , Clone , Serialize ) ]
3132struct ItemCount {
3233 total : u64 ,
3334 with_docs : u64 ,
@@ -68,68 +69,37 @@ impl ops::AddAssign for ItemCount {
6869
6970struct CoverageCalculator {
7071 items : BTreeMap < FileName , ItemCount > ,
71- output_format : Option < OutputFormat > ,
7272}
7373
7474fn limit_filename_len ( filename : String ) -> String {
75- // if a filename is too long, shorten it so we don't blow out the table
76- // FIXME(misdreavus): this needs to count graphemes, and probably also track
77- // double-wide characters...
78- if filename. len ( ) > 35 {
79- "..." . to_string ( ) + & filename[ filename. len ( ) - 32 ..]
75+ let nb_chars = filename. chars ( ) . count ( ) ;
76+ if nb_chars > 35 {
77+ "..." . to_string ( )
78+ + & filename[ filename. char_indices ( ) . nth ( nb_chars - 32 ) . map ( |x| x. 0 ) . unwrap_or ( 0 ) ..]
8079 } else {
8180 filename
8281 }
8382}
8483
85- impl ToJson for CoverageCalculator {
86- fn to_json ( & self ) -> Json {
87- let mut total = ItemCount :: default ( ) ;
88- let mut entries = BTreeMap :: default ( ) ;
89-
90- entries. insert ( "files" . to_owned ( ) , Json :: Array ( self . items
91- . iter ( )
92- . filter_map ( |( file, & count) | {
93- count. percentage ( ) . map ( |percent| {
94- ( limit_filename_len ( file. to_string ( ) ) , count, percent)
95- } )
96- } )
97- . map ( |( name, count, percentage) | {
98- let mut fields = BTreeMap :: default ( ) ;
99-
100- fields. insert ( "documented" . to_owned ( ) , Json :: U64 ( count. with_docs ) ) ;
101- fields. insert ( "total" . to_owned ( ) , Json :: U64 ( count. total ) ) ;
102- fields. insert ( "percentage" . to_owned ( ) , Json :: F64 ( percentage) ) ;
103-
104- total += count;
105-
106- let mut obj = BTreeMap :: default ( ) ;
107- obj. insert ( name, Json :: Object ( fields) ) ;
108-
109- Json :: Object ( obj)
110- } )
111- . collect :: < Vec < _ > > ( ) ) ) ;
112- let mut fields = BTreeMap :: default ( ) ;
113- fields. insert ( "documented" . to_owned ( ) , Json :: U64 ( total. with_docs ) ) ;
114- fields. insert ( "total" . to_owned ( ) , Json :: U64 ( total. total ) ) ;
115- fields. insert ( "percentage" . to_owned ( ) , Json :: F64 ( total. percentage ( ) . unwrap_or ( 0.0 ) ) ) ;
116-
117- entries. insert ( "total" . to_owned ( ) , Json :: Object ( fields) ) ;
118- Json :: Object ( entries)
84+ impl CoverageCalculator {
85+ fn new ( ) -> CoverageCalculator {
86+ CoverageCalculator { items : Default :: default ( ) }
11987 }
120- }
12188
122- impl CoverageCalculator {
123- fn new ( output_format : Option < OutputFormat > ) -> CoverageCalculator {
124- CoverageCalculator {
125- items : Default :: default ( ) ,
126- output_format,
127- }
89+ fn to_json ( & self ) -> String {
90+ serde_json:: to_string (
91+ & self
92+ . items
93+ . iter ( )
94+ . map ( |( k, v) | ( k. to_string ( ) , v) )
95+ . collect :: < BTreeMap < String , & ItemCount > > ( ) ,
96+ )
97+ . expect ( "failed to convert JSON data to string" )
12898 }
12999
130- fn print_results ( & self ) {
131- if self . output_format . map ( |o| o. is_json ( ) ) . unwrap_or_else ( || false ) {
132- println ! ( "{}" , self . to_json( ) . pretty ( ) ) ;
100+ fn print_results ( & self , output_format : Option < OutputFormat > ) {
101+ if output_format. map ( |o| o. is_json ( ) ) . unwrap_or_else ( || false ) {
102+ println ! ( "{}" , self . to_json( ) ) ;
133103 return ;
134104 }
135105 let mut total = ItemCount :: default ( ) ;
@@ -154,15 +124,7 @@ impl CoverageCalculator {
154124
155125 for ( file, & count) in & self . items {
156126 if let Some ( percentage) = count. percentage ( ) {
157- let mut name = file. to_string ( ) ;
158- // if a filename is too long, shorten it so we don't blow out the table
159- // FIXME(misdreavus): this needs to count graphemes, and probably also track
160- // double-wide characters...
161- if name. len ( ) > 35 {
162- name = "..." . to_string ( ) + & name[ name. len ( ) - 32 ..] ;
163- }
164-
165- print_table_record ( & name, count, percentage) ;
127+ print_table_record ( & limit_filename_len ( file. to_string ( ) ) , count, percentage) ;
166128
167129 total += count;
168130 }
0 commit comments