@@ -39,8 +39,10 @@ pub(crate) struct JsonRenderer<'tcx> {
3939 /// A mapping of IDs that contains all local items for this crate which gets output as a top
4040 /// level field of the JSON blob.
4141 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 > ,
4446 cache : Rc < Cache > ,
4547 imported_items : DefIdSet ,
4648}
@@ -101,18 +103,20 @@ impl<'tcx> JsonRenderer<'tcx> {
101103 . unwrap_or_default ( )
102104 }
103105
104- fn write < T : Write > (
106+ fn serialize_and_write < T : Write > (
105107 & self ,
106- output : types:: Crate ,
108+ output_crate : types:: Crate ,
107109 mut writer : BufWriter < T > ,
108110 path : & str ,
109111 ) -> 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_crate) . map_err( |e| e. to_string( ) ) ,
115+ path
116+ ) ;
117+ try_err ! ( writer. flush( ) , path) ;
118+ Ok ( ( ) )
119+ } )
116120 }
117121}
118122
@@ -137,7 +141,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
137141 JsonRenderer {
138142 tcx,
139143 index : Rc :: new ( RefCell :: new ( FxHashMap :: default ( ) ) ) ,
140- out_path : if options. output_to_stdout { None } else { Some ( options. output ) } ,
144+ out_dir : if options. output_to_stdout { None } else { Some ( options. output ) } ,
141145 cache : Rc :: new ( cache) ,
142146 imported_items,
143147 } ,
@@ -237,7 +241,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
237241 let index = ( * self . index ) . clone ( ) . into_inner ( ) ;
238242
239243 debug ! ( "Constructing Output" ) ;
240- let output = types:: Crate {
244+ let output_crate = types:: Crate {
241245 root : types:: Id ( format ! ( "0:0:{}" , e. name( self . tcx) . as_u32( ) ) ) ,
242246 crate_version : self . cache . crate_version . clone ( ) ,
243247 includes_private : self . cache . document_private ,
@@ -278,20 +282,20 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
278282 . collect ( ) ,
279283 format_version : types:: FORMAT_VERSION ,
280284 } ;
281- if let Some ( ref out_path) = self . out_path {
282- let out_dir = out_path. clone ( ) ;
285+ if let Some ( ref out_dir) = self . out_dir {
283286 try_err ! ( create_dir_all( & out_dir) , out_dir) ;
284287
285- let mut p = out_dir;
286- p. push ( output . index . get ( & output . root ) . unwrap ( ) . name . clone ( ) . unwrap ( ) ) ;
288+ let mut p = out_dir. clone ( ) ;
289+ p. push ( output_crate . index . get ( & output_crate . root ) . unwrap ( ) . name . clone ( ) . unwrap ( ) ) ;
287290 p. set_extension ( "json" ) ;
288- self . write (
289- output,
291+
292+ self . serialize_and_write (
293+ output_crate,
290294 BufWriter :: new ( try_err ! ( File :: create( & p) , p) ) ,
291295 & p. display ( ) . to_string ( ) ,
292296 )
293297 } else {
294- self . write ( output , BufWriter :: new ( stdout ( ) ) , "<stdout>" )
298+ self . serialize_and_write ( output_crate , BufWriter :: new ( stdout ( ) . lock ( ) ) , "<stdout>" )
295299 }
296300 }
297301
0 commit comments