@@ -9,7 +9,7 @@ mod import_finder;
9
9
10
10
use std:: cell:: RefCell ;
11
11
use std:: fs:: { create_dir_all, File } ;
12
- use std:: io:: { BufWriter , Write } ;
12
+ use std:: io:: { stdout , BufWriter , Write } ;
13
13
use std:: path:: PathBuf ;
14
14
use std:: rc:: Rc ;
15
15
@@ -37,7 +37,7 @@ pub(crate) struct JsonRenderer<'tcx> {
37
37
/// level field of the JSON blob.
38
38
index : Rc < RefCell < FxHashMap < types:: Id , types:: Item > > > ,
39
39
/// The directory where the blob will be written to.
40
- out_path : PathBuf ,
40
+ out_path : Option < PathBuf > ,
41
41
cache : Rc < Cache > ,
42
42
imported_items : DefIdSet ,
43
43
}
@@ -97,6 +97,20 @@ impl<'tcx> JsonRenderer<'tcx> {
97
97
} )
98
98
. unwrap_or_default ( )
99
99
}
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
+ }
100
114
}
101
115
102
116
impl < ' tcx > FormatRenderer < ' tcx > for JsonRenderer < ' tcx > {
@@ -120,7 +134,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
120
134
JsonRenderer {
121
135
tcx,
122
136
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 ) } ,
124
138
cache : Rc :: new ( cache) ,
125
139
imported_items,
126
140
} ,
@@ -264,20 +278,21 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
264
278
. collect ( ) ,
265
279
format_version : types:: FORMAT_VERSION ,
266
280
} ;
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) ;
269
284
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
+ }
281
296
}
282
297
283
298
fn cache ( & self ) -> & Cache {
0 commit comments