@@ -7,7 +7,8 @@ use crate::passes::Pass;
7
7
use rustc_ast:: attr;
8
8
use rustc_span:: symbol:: sym;
9
9
use rustc_span:: FileName ;
10
- use serialize:: json:: { ToJson , Json } ;
10
+ use serde:: Serialize ;
11
+ use serde_json;
11
12
12
13
use std:: collections:: BTreeMap ;
13
14
use std:: ops;
@@ -18,16 +19,16 @@ pub const CALCULATE_DOC_COVERAGE: Pass = Pass {
18
19
description : "counts the number of items with and without documentation" ,
19
20
} ;
20
21
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 ( ) ;
23
24
let krate = calc. fold_crate ( krate) ;
24
25
25
- calc. print_results ( ) ;
26
+ calc. print_results ( ctx . renderinfo . borrow ( ) . output_format ) ;
26
27
27
28
krate
28
29
}
29
30
30
- #[ derive( Default , Copy , Clone ) ]
31
+ #[ derive( Default , Copy , Clone , Serialize ) ]
31
32
struct ItemCount {
32
33
total : u64 ,
33
34
with_docs : u64 ,
@@ -68,68 +69,37 @@ impl ops::AddAssign for ItemCount {
68
69
69
70
struct CoverageCalculator {
70
71
items : BTreeMap < FileName , ItemCount > ,
71
- output_format : Option < OutputFormat > ,
72
72
}
73
73
74
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 ..]
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 ) ..]
80
79
} else {
81
80
filename
82
81
}
83
82
}
84
83
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 ( ) }
119
87
}
120
- }
121
88
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" )
128
98
}
129
99
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( ) ) ;
133
103
return ;
134
104
}
135
105
let mut total = ItemCount :: default ( ) ;
@@ -154,15 +124,7 @@ impl CoverageCalculator {
154
124
155
125
for ( file, & count) in & self . items {
156
126
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) ;
166
128
167
129
total += count;
168
130
}
0 commit comments