@@ -9,7 +9,7 @@ mod import_finder;
99
1010use std:: cell:: RefCell ;
1111use std:: fs:: { create_dir_all, File } ;
12- use std:: io:: { BufWriter , Write } ;
12+ use std:: io:: { stdout , BufWriter , Write } ;
1313use std:: path:: PathBuf ;
1414use std:: rc:: Rc ;
1515
@@ -37,7 +37,7 @@ pub(crate) struct JsonRenderer<'tcx> {
3737 /// level field of the JSON blob.
3838 index : Rc < RefCell < FxHashMap < types:: Id , types:: Item > > > ,
3939 /// The directory where the blob will be written to.
40- out_path : PathBuf ,
40+ out_path : Option < PathBuf > ,
4141 cache : Rc < Cache > ,
4242 imported_items : DefIdSet ,
4343}
@@ -97,6 +97,20 @@ impl<'tcx> JsonRenderer<'tcx> {
9797 } )
9898 . unwrap_or_default ( )
9999 }
100+
101+ fn write < T : Write > (
102+ & self ,
103+ output : types:: Crate ,
104+ mut writer : BufWriter < T > ,
105+ path : & str ,
106+ ) -> Result < ( ) , Error > {
107+ self . tcx
108+ . sess
109+ . time ( "rustdoc_json_serialization" , || serde_json:: ser:: to_writer ( & mut writer, & output) )
110+ . unwrap ( ) ;
111+ try_err ! ( writer. flush( ) , path) ;
112+ Ok ( ( ) )
113+ }
100114}
101115
102116impl < ' tcx > FormatRenderer < ' tcx > for JsonRenderer < ' tcx > {
@@ -120,7 +134,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
120134 JsonRenderer {
121135 tcx,
122136 index : Rc :: new ( RefCell :: new ( FxHashMap :: default ( ) ) ) ,
123- out_path : options. output ,
137+ out_path : if options. output_to_stdout { None } else { Some ( options . output ) } ,
124138 cache : Rc :: new ( cache) ,
125139 imported_items,
126140 } ,
@@ -264,20 +278,21 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
264278 . collect ( ) ,
265279 format_version : types:: FORMAT_VERSION ,
266280 } ;
267- let out_dir = self . out_path . clone ( ) ;
268- try_err ! ( create_dir_all( & out_dir) , out_dir) ;
281+ if let Some ( ref out_path) = self . out_path {
282+ let out_dir = out_path. clone ( ) ;
283+ try_err ! ( create_dir_all( & out_dir) , out_dir) ;
269284
270- let mut p = out_dir;
271- p. push ( output. index . get ( & output. root ) . unwrap ( ) . name . clone ( ) . unwrap ( ) ) ;
272- p. set_extension ( "json" ) ;
273- let mut file = BufWriter :: new ( try_err ! ( File :: create ( & p ) , p ) ) ;
274- self . tcx
275- . sess
276- . time ( "rustdoc_json_serialization" , || serde_json :: ser :: to_writer ( & mut file , & output ) )
277- . unwrap ( ) ;
278- try_err ! ( file . flush ( ) , p ) ;
279-
280- Ok ( ( ) )
285+ let mut p = out_dir;
286+ p. push ( output. index . get ( & output. root ) . unwrap ( ) . name . clone ( ) . unwrap ( ) ) ;
287+ p. set_extension ( "json" ) ;
288+ self . write (
289+ output ,
290+ BufWriter :: new ( try_err ! ( File :: create ( & p ) , p ) ) ,
291+ & p . display ( ) . to_string ( ) ,
292+ )
293+ } else {
294+ self . write ( output , BufWriter :: new ( stdout ( ) ) , "<stdout>" )
295+ }
281296 }
282297
283298 fn cache ( & self ) -> & Cache {
0 commit comments