@@ -68,6 +68,7 @@ mod error;
6868mod fold;
6969crate mod formats;
7070pub mod html;
71+ mod json;
7172mod markdown;
7273mod passes;
7374mod test;
@@ -450,6 +451,28 @@ fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> i32 {
450451 }
451452}
452453
454+ fn run_renderer < T : formats:: FormatRenderer > (
455+ krate : clean:: Crate ,
456+ renderopts : config:: RenderOptions ,
457+ render_info : config:: RenderInfo ,
458+ diag : & rustc_errors:: Handler ,
459+ edition : rustc_span:: edition:: Edition ,
460+ ) -> i32 {
461+ match formats:: run_format :: < T > ( krate, renderopts, render_info, & diag, edition) {
462+ Ok ( _) => rustc_driver:: EXIT_SUCCESS ,
463+ Err ( e) => {
464+ let mut msg = diag. struct_err ( & format ! ( "couldn't generate documentation: {}" , e. error) ) ;
465+ let file = e. file . display ( ) . to_string ( ) ;
466+ if file. is_empty ( ) {
467+ msg. emit ( )
468+ } else {
469+ msg. note ( & format ! ( "failed to create or modify \" {}\" " , file) ) . emit ( )
470+ }
471+ rustc_driver:: EXIT_FAILURE
472+ }
473+ }
474+ }
475+
453476fn main_options ( options : config:: Options ) -> i32 {
454477 let diag = core:: new_handler ( options. error_format , None , & options. debugging_options ) ;
455478
@@ -480,6 +503,7 @@ fn main_options(options: config::Options) -> i32 {
480503 let result = rustc_driver:: catch_fatal_errors ( move || {
481504 let crate_name = options. crate_name . clone ( ) ;
482505 let crate_version = options. crate_version . clone ( ) ;
506+ let output_format = options. output_format ;
483507 let ( mut krate, renderinfo, renderopts) = core:: run_core ( options) ;
484508
485509 info ! ( "finished with rustc" ) ;
@@ -502,20 +526,12 @@ fn main_options(options: config::Options) -> i32 {
502526 info ! ( "going to format" ) ;
503527 let ( error_format, edition, debugging_options) = diag_opts;
504528 let diag = core:: new_handler ( error_format, None , & debugging_options) ;
505- match formats:: run_format :: < html:: render:: Context > (
506- krate, renderopts, renderinfo, & diag, edition,
507- ) {
508- Ok ( _) => rustc_driver:: EXIT_SUCCESS ,
509- Err ( e) => {
510- let mut msg =
511- diag. struct_err ( & format ! ( "couldn't generate documentation: {}" , e. error) ) ;
512- let file = e. file . display ( ) . to_string ( ) ;
513- if file. is_empty ( ) {
514- msg. emit ( )
515- } else {
516- msg. note ( & format ! ( "failed to create or modify \" {}\" " , file) ) . emit ( )
517- }
518- rustc_driver:: EXIT_FAILURE
529+ match output_format {
530+ None | Some ( config:: OutputFormat :: Html ) => {
531+ run_renderer :: < html:: render:: Context > ( krate, renderopts, renderinfo, & diag, edition)
532+ }
533+ Some ( config:: OutputFormat :: Json ) => {
534+ run_renderer :: < json:: JsonRenderer > ( krate, renderopts, renderinfo, & diag, edition)
519535 }
520536 }
521537 } ) ;
0 commit comments