@@ -39,8 +39,10 @@ pub(crate) struct JsonRenderer<'tcx> {
39
39
/// A mapping of IDs that contains all local items for this crate which gets output as a top
40
40
/// level field of the JSON blob.
41
41
index : Rc < RefCell < FxHashMap < types:: Id , types:: Item > > > ,
42
- /// The directory where the blob will be written to.
43
- out_path : Option < PathBuf > ,
42
+ /// The directory where the JSON blob should be written to.
43
+ ///
44
+ /// If this is `None`, the blob will be printed to `stdout` instead.
45
+ out_dir : Option < PathBuf > ,
44
46
cache : Rc < Cache > ,
45
47
imported_items : DefIdSet ,
46
48
}
@@ -101,18 +103,21 @@ impl<'tcx> JsonRenderer<'tcx> {
101
103
. unwrap_or_default ( )
102
104
}
103
105
104
- fn write < T : Write > (
106
+ fn serialize_and_write < T : Write > (
105
107
& self ,
106
- output : types:: Crate ,
108
+ output_document : types:: Crate ,
107
109
mut writer : BufWriter < T > ,
108
110
path : & str ,
109
111
) -> Result < ( ) , Error > {
110
- self . tcx
111
- . sess
112
- . time ( "rustdoc_json_serialization" , || serde_json:: ser:: to_writer ( & mut writer, & output) )
113
- . unwrap ( ) ;
114
- try_err ! ( writer. flush( ) , path) ;
115
- Ok ( ( ) )
112
+ self . sess ( ) . time ( "rustdoc_json_serialize_and_write" , || {
113
+ try_err ! (
114
+ serde_json:: ser:: to_writer( & mut writer, & output_document)
115
+ . map_err( |e| e. to_string( ) ) ,
116
+ path
117
+ ) ;
118
+ try_err ! ( writer. flush( ) , path) ;
119
+ Ok ( ( ) )
120
+ } )
116
121
}
117
122
}
118
123
@@ -137,7 +142,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
137
142
JsonRenderer {
138
143
tcx,
139
144
index : Rc :: new ( RefCell :: new ( FxHashMap :: default ( ) ) ) ,
140
- out_path : if options. output_to_stdout { None } else { Some ( options. output ) } ,
145
+ out_dir : if options. output_to_stdout { None } else { Some ( options. output ) } ,
141
146
cache : Rc :: new ( cache) ,
142
147
imported_items,
143
148
} ,
@@ -237,7 +242,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
237
242
let index = ( * self . index ) . clone ( ) . into_inner ( ) ;
238
243
239
244
debug ! ( "Constructing Output" ) ;
240
- let output = types:: Crate {
245
+ let output_crate = types:: Crate {
241
246
root : types:: Id ( format ! ( "0:0:{}" , e. name( self . tcx) . as_u32( ) ) ) ,
242
247
crate_version : self . cache . crate_version . clone ( ) ,
243
248
includes_private : self . cache . document_private ,
@@ -278,20 +283,20 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
278
283
. collect ( ) ,
279
284
format_version : types:: FORMAT_VERSION ,
280
285
} ;
281
- if let Some ( ref out_path) = self . out_path {
282
- let out_dir = out_path. clone ( ) ;
286
+ if let Some ( ref out_dir) = self . out_dir {
283
287
try_err ! ( create_dir_all( & out_dir) , out_dir) ;
284
288
285
- let mut p = out_dir;
286
- p. push ( output . index . get ( & output . root ) . unwrap ( ) . name . clone ( ) . unwrap ( ) ) ;
289
+ let mut p = out_dir. clone ( ) ;
290
+ p. push ( output_crate . index . get ( & output_crate . root ) . unwrap ( ) . name . clone ( ) . unwrap ( ) ) ;
287
291
p. set_extension ( "json" ) ;
288
- self . write (
289
- output,
292
+
293
+ self . serialize_and_write (
294
+ output_crate,
290
295
BufWriter :: new ( try_err ! ( File :: create( & p) , p) ) ,
291
296
& p. display ( ) . to_string ( ) ,
292
297
)
293
298
} else {
294
- self . write ( output , BufWriter :: new ( stdout ( ) ) , "<stdout>" )
299
+ self . serialize_and_write ( output_crate , BufWriter :: new ( stdout ( ) . lock ( ) ) , "<stdout>" )
295
300
}
296
301
}
297
302
0 commit comments