1
1
use crate :: clean;
2
+ use crate :: config:: OutputFormat ;
2
3
use crate :: core:: DocContext ;
3
4
use crate :: fold:: { self , DocFolder } ;
4
5
use crate :: passes:: Pass ;
5
6
6
7
use rustc_ast:: attr;
7
8
use rustc_span:: symbol:: sym;
8
9
use rustc_span:: FileName ;
10
+ use serialize:: json:: { ToJson , Json } ;
9
11
10
12
use std:: collections:: BTreeMap ;
11
13
use std:: ops;
@@ -16,8 +18,8 @@ pub const CALCULATE_DOC_COVERAGE: Pass = Pass {
16
18
description : "counts the number of items with and without documentation" ,
17
19
} ;
18
20
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 ) ;
21
23
let krate = calc. fold_crate ( krate) ;
22
24
23
25
calc. print_results ( ) ;
@@ -64,13 +66,72 @@ impl ops::AddAssign for ItemCount {
64
66
}
65
67
}
66
68
67
- #[ derive( Default ) ]
68
69
struct CoverageCalculator {
69
70
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
+ }
70
120
}
71
121
72
122
impl CoverageCalculator {
123
+ fn new ( output_format : Option < OutputFormat > ) -> CoverageCalculator {
124
+ CoverageCalculator {
125
+ items : Default :: default ( ) ,
126
+ output_format,
127
+ }
128
+ }
129
+
73
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( ) ) ;
133
+ return ;
134
+ }
74
135
let mut total = ItemCount :: default ( ) ;
75
136
76
137
fn print_table_line ( ) {
0 commit comments