14
14
extern crate log;
15
15
extern crate regex;
16
16
extern crate rustfmt_nightly as rustfmt;
17
- extern crate tempfile;
18
17
extern crate term;
19
18
20
19
use std:: collections:: HashMap ;
@@ -24,8 +23,6 @@ use std::iter::Peekable;
24
23
use std:: path:: { Path , PathBuf } ;
25
24
use std:: str:: Chars ;
26
25
27
- use tempfile:: NamedTempFileOptions ;
28
-
29
26
use rustfmt:: * ;
30
27
use rustfmt:: config:: { Color , Config , ReportTactic } ;
31
28
use rustfmt:: filemap:: { write_system_newlines, FileMap } ;
@@ -529,8 +526,8 @@ fn get_code_blocks() -> Vec<CodeBlock> {
529
526
530
527
let config_name_regex =
531
528
regex:: Regex :: new ( r"^## `([^`]+)`" ) . expect ( "Failed creating configuration pattern" ) ;
532
- let config_value_regex =
533
- regex :: Regex :: new ( r"^#### `([^`]+)`" ) . expect ( "Failed creating configuration value pattern" ) ;
529
+ let config_value_regex = regex :: Regex :: new ( r#"^#### `"?([^`"]+)"?`"# )
530
+ . expect ( "Failed creating configuration value pattern" ) ;
534
531
535
532
let mut code_blocks = Vec :: new ( ) ;
536
533
let mut extracting_block = false ;
@@ -593,34 +590,33 @@ fn check_blocks_idempotency(blocks: &Vec<CodeBlock>) {
593
590
let mut failures = 0 ;
594
591
595
592
for block in blocks {
596
- let mut temp_file = NamedTempFileOptions :: new ( ) . suffix ( ".rs" ) . create ( ) . unwrap ( ) ;
597
- let mut file_name_list = Vec :: new ( ) ;
598
-
599
- // Write the configuration name and value at the top of the temp file.
600
- writeln ! (
601
- temp_file,
602
- "// {}: {}" ,
603
- block. config. name, block. config. value
604
- ) . unwrap ( ) ;
605
-
606
- // Write the code block to the temp file.
607
- write ! ( temp_file, "{}" , block. code_block) . unwrap ( ) ;
608
-
609
- // Format the temp file.
610
- file_name_list. push ( temp_file. path ( ) . to_path_buf ( ) ) ;
611
- let files = file_name_list. iter ( ) . map ( |x| x. to_owned ( ) ) ;
612
- let ( _reports, _count, fails) = check_files ( files) ;
613
-
614
- if fails != 0 {
615
- // If formatting failed, print a message with the *actual* line
616
- // number of the code block in Configurations.md. The message
617
- // printed by `check_files` will use the name and line number from
618
- // the temp file.
619
- write_message ( format ! (
593
+ let input = Input :: Text ( block. code_block . clone ( ) ) ;
594
+ let mut config = Config :: default ( ) ;
595
+ config. override_value ( & block. config . name , & block. config . value ) ;
596
+
597
+ let ( error_summary, file_map, _report) =
598
+ format_input :: < io:: Stdout > ( input, & config, None ) . unwrap ( ) ;
599
+
600
+ if error_summary. has_parsing_errors ( ) {
601
+ write_message ( String :: from ( format ! (
620
602
"\u{261d} \u{1f3fd} Failed to format block starting at Line {} in Configurations.md" ,
621
603
block. code_block_start_line
622
- ) ) ;
623
- failures += fails;
604
+ ) ) ) ;
605
+ failures += 1 ;
606
+ } else {
607
+ for & ( ref file_name, ref text) in & file_map {
608
+ if let FileName :: Custom ( ref file_name) = * file_name {
609
+ if file_name == "stdin" {
610
+ let compare = make_diff ( & block. code_block , text, DIFF_CONTEXT_SIZE ) ;
611
+ if !compare. is_empty ( ) {
612
+ let mut mismatches = HashMap :: new ( ) ;
613
+ mismatches. insert ( PathBuf :: from ( "Configurations.md" ) , compare) ;
614
+ print_mismatches ( mismatches) ;
615
+ failures += 1 ;
616
+ }
617
+ }
618
+ }
619
+ }
624
620
}
625
621
}
626
622
0 commit comments