1
1
use std:: collections:: BTreeMap ;
2
+ use std:: convert:: TryFrom ;
2
3
use std:: ffi:: OsStr ;
3
4
use std:: fmt;
4
5
use std:: path:: PathBuf ;
@@ -24,6 +25,33 @@ use crate::opts;
24
25
use crate :: passes:: { self , Condition , DefaultPassOption } ;
25
26
use crate :: theme;
26
27
28
+ #[ derive( Clone , Copy , PartialEq , Eq , Debug ) ]
29
+ pub enum OutputFormat {
30
+ Json ,
31
+ Html ,
32
+ }
33
+
34
+ impl OutputFormat {
35
+ pub fn is_json ( & self ) -> bool {
36
+ match self {
37
+ OutputFormat :: Json => true ,
38
+ _ => false ,
39
+ }
40
+ }
41
+ }
42
+
43
+ impl TryFrom < & str > for OutputFormat {
44
+ type Error = String ;
45
+
46
+ fn try_from ( value : & str ) -> Result < Self , Self :: Error > {
47
+ match value {
48
+ "json" => Ok ( OutputFormat :: Json ) ,
49
+ "html" => Ok ( OutputFormat :: Html ) ,
50
+ _ => Err ( format ! ( "unknown output format `{}`" , value) ) ,
51
+ }
52
+ }
53
+ }
54
+
27
55
/// Configuration options for rustdoc.
28
56
#[ derive( Clone ) ]
29
57
pub struct Options {
@@ -115,6 +143,8 @@ pub struct Options {
115
143
pub crate_version : Option < String > ,
116
144
/// Collected options specific to outputting final pages.
117
145
pub render_options : RenderOptions ,
146
+ /// Output format rendering (used only for "show-coverage" option for the moment)
147
+ pub output_format : Option < OutputFormat > ,
118
148
}
119
149
120
150
impl fmt:: Debug for Options {
@@ -425,14 +455,6 @@ impl Options {
425
455
}
426
456
}
427
457
428
- match matches. opt_str ( "w" ) . as_ref ( ) . map ( |s| & * * s) {
429
- Some ( "html" ) | None => { }
430
- Some ( s) => {
431
- diag. struct_err ( & format ! ( "unknown output format: {}" , s) ) . emit ( ) ;
432
- return Err ( 1 ) ;
433
- }
434
- }
435
-
436
458
let index_page = matches. opt_str ( "index-page" ) . map ( |s| PathBuf :: from ( & s) ) ;
437
459
if let Some ( ref index_page) = index_page {
438
460
if !index_page. is_file ( ) {
@@ -469,6 +491,29 @@ impl Options {
469
491
}
470
492
} ;
471
493
494
+ let output_format = match matches. opt_str ( "output-format" ) {
495
+ Some ( s) => match OutputFormat :: try_from ( s. as_str ( ) ) {
496
+ Ok ( o) => {
497
+ if o. is_json ( ) && !show_coverage {
498
+ diag. struct_err ( "json output format isn't supported for doc generation" )
499
+ . emit ( ) ;
500
+ return Err ( 1 ) ;
501
+ } else if !o. is_json ( ) && show_coverage {
502
+ diag. struct_err (
503
+ "html output format isn't supported for the --show-coverage option" ,
504
+ )
505
+ . emit ( ) ;
506
+ return Err ( 1 ) ;
507
+ }
508
+ Some ( o)
509
+ }
510
+ Err ( e) => {
511
+ diag. struct_err ( & e) . emit ( ) ;
512
+ return Err ( 1 ) ;
513
+ }
514
+ } ,
515
+ None => None ,
516
+ } ;
472
517
let crate_name = matches. opt_str ( "crate-name" ) ;
473
518
let proc_macro_crate = crate_types. contains ( & CrateType :: ProcMacro ) ;
474
519
let playground_url = matches. opt_str ( "playground-url" ) ;
@@ -553,6 +598,7 @@ impl Options {
553
598
generate_search_filter,
554
599
generate_redirect_pages,
555
600
} ,
601
+ output_format,
556
602
} )
557
603
}
558
604
@@ -568,6 +614,9 @@ fn check_deprecated_options(matches: &getopts::Matches, diag: &rustc_errors::Han
568
614
569
615
for flag in deprecated_flags. iter ( ) {
570
616
if matches. opt_present ( flag) {
617
+ if * flag == "output-format" && matches. opt_present ( "show-coverage" ) {
618
+ continue ;
619
+ }
571
620
let mut err =
572
621
diag. struct_warn ( & format ! ( "the '{}' flag is considered deprecated" , flag) ) ;
573
622
err. warn (
0 commit comments