@@ -2,19 +2,18 @@ extern crate failure;
2
2
extern crate rustfix;
3
3
4
4
use failure:: Error ;
5
- use std:: { collections:: HashMap , collections:: HashSet , env, fs, process} ;
5
+ use std:: io:: { stdin, BufReader , Read } ;
6
+ use std:: { collections:: HashMap , collections:: HashSet , env, fs} ;
6
7
7
8
fn main ( ) -> Result < ( ) , Error > {
8
- let args : Vec < String > = env:: args ( ) . collect ( ) ;
9
- let suggestions_file = match args . as_slice ( ) {
10
- [ _ , suggestions_file ] => suggestions_file ,
11
- _ => {
12
- println ! ( "USAGE: fix-json <suggestions-file>" ) ;
13
- process :: exit ( 1 ) ;
14
- }
9
+ let suggestions_file = env:: args ( ) . nth ( 1 ) . expect ( "USAGE: fix-json <file or -->" ) ;
10
+ let suggestions = if suggestions_file == "--" {
11
+ let mut buffer = String :: new ( ) ;
12
+ BufReader :: new ( stdin ( ) ) . read_to_string ( & mut buffer ) ? ;
13
+ buffer
14
+ } else {
15
+ fs :: read_to_string ( & suggestions_file ) ?
15
16
} ;
16
-
17
- let suggestions = fs:: read_to_string ( & suggestions_file) ?;
18
17
let suggestions = rustfix:: get_suggestions_from_json (
19
18
& suggestions,
20
19
& HashSet :: new ( ) ,
@@ -27,14 +26,13 @@ fn main() -> Result<(), Error> {
27
26
. snippet
28
27
. file_name
29
28
. clone ( ) ;
30
- let entry = files. entry ( file) . or_insert ( Vec :: new ( ) ) ;
31
- entry. push ( suggestion) ;
29
+ files. entry ( file) . or_insert_with ( Vec :: new) . push ( suggestion) ;
32
30
}
33
31
34
32
for ( source_file, suggestions) in & files {
35
- let source = fs:: read_to_string ( & source_file) ?;
33
+ let source = fs:: read_to_string ( source_file) ?;
36
34
let fixes = rustfix:: apply_suggestions ( & source, suggestions) ?;
37
- fs:: write ( & source_file, fixes) ?;
35
+ fs:: write ( source_file, fixes) ?;
38
36
}
39
37
40
38
Ok ( ( ) )
0 commit comments