11use crate :: clean;
2+ use crate :: config:: OutputFormat ;
23use crate :: core:: DocContext ;
34use crate :: fold:: { self , DocFolder } ;
45use crate :: passes:: Pass ;
56
67use rustc_ast:: attr;
78use rustc_span:: symbol:: sym;
89use rustc_span:: FileName ;
10+ use serialize:: json:: { ToJson , Json } ;
911
1012use std:: collections:: BTreeMap ;
1113use std:: ops;
@@ -16,8 +18,8 @@ pub const CALCULATE_DOC_COVERAGE: Pass = Pass {
1618 description : "counts the number of items with and without documentation" ,
1719} ;
1820
19- fn calculate_doc_coverage ( krate : clean:: Crate , _ : & DocContext < ' _ > ) -> clean:: Crate {
20- let mut calc = CoverageCalculator :: default ( ) ;
21+ fn calculate_doc_coverage ( krate : clean:: Crate , ctx : & DocContext < ' _ > ) -> clean:: Crate {
22+ let mut calc = CoverageCalculator :: new ( ctx . renderinfo . borrow ( ) . output_format ) ;
2123 let krate = calc. fold_crate ( krate) ;
2224
2325 calc. print_results ( ) ;
@@ -64,13 +66,72 @@ impl ops::AddAssign for ItemCount {
6466 }
6567}
6668
67- #[ derive( Default ) ]
6869struct CoverageCalculator {
6970 items : BTreeMap < FileName , ItemCount > ,
71+ output_format : Option < OutputFormat > ,
72+ }
73+
74+ fn 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 ..]
80+ } else {
81+ filename
82+ }
83+ }
84+
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)
119+ }
70120}
71121
72122impl CoverageCalculator {
123+ fn new ( output_format : Option < OutputFormat > ) -> CoverageCalculator {
124+ CoverageCalculator {
125+ items : Default :: default ( ) ,
126+ output_format,
127+ }
128+ }
129+
73130 fn print_results ( & self ) {
131+ if self . output_format . map ( |o| o. is_json ( ) ) . unwrap_or_else ( || false ) {
132+ println ! ( "{}" , self . to_json( ) . pretty( ) ) ;
133+ return ;
134+ }
74135 let mut total = ItemCount :: default ( ) ;
75136
76137 fn print_table_line ( ) {
0 commit comments