@@ -22,9 +22,6 @@ use log::{log, debug};
22
22
use rustfmt_nightly:: { Config , Session , Input } ;
23
23
use serde_json;
24
24
25
- struct External < ' a > ( & ' a Path , & ' a Path ) ;
26
- struct Internal ;
27
-
28
25
/// Specified which `rustfmt` to use.
29
26
#[ derive( Clone ) ]
30
27
pub enum Rustfmt {
@@ -43,81 +40,68 @@ impl From<Option<(String, PathBuf)>> for Rustfmt {
43
40
}
44
41
}
45
42
46
-
47
- pub trait Formatter {
48
- fn format ( & self , input : String , cfg : Config ) -> Result < String , String > ;
49
- }
50
-
51
- impl Formatter for External < ' _ > {
52
- fn format ( & self , input : String , cfg : Config ) -> Result < String , String > {
53
- let External ( path, cwd) = self ;
54
-
55
- let ( _file_handle, config_path) = gen_config_file ( & cfg) ?;
56
- let args = rustfmt_args ( & cfg, & config_path) ;
57
-
58
- let mut rustfmt = Command :: new ( path)
59
- . args ( args)
60
- . current_dir ( cwd)
61
- . stdin ( Stdio :: piped ( ) )
62
- . stdout ( Stdio :: piped ( ) )
63
- . spawn ( )
64
- . map_err ( |_| format ! ( "Couldn't spawn `{}`" , path. display( ) ) ) ?;
65
-
66
- {
67
- let stdin = rustfmt. stdin . as_mut ( )
68
- . ok_or_else ( || "Failed to open rustfmt stdin" . to_string ( ) ) ?;
69
- stdin. write_all ( input. as_bytes ( ) )
70
- . map_err ( |_| "Failed to pass input to rustfmt" . to_string ( ) ) ?;
43
+ impl Rustfmt {
44
+ pub fn format ( & self , input : String , cfg : Config ) -> Result < String , String > {
45
+ match self {
46
+ Rustfmt :: Internal => format_internal ( input, cfg) ,
47
+ Rustfmt :: External ( path, cwd) => format_external ( path, cwd, input, cfg) ,
71
48
}
72
-
73
- rustfmt. wait_with_output ( )
74
- . map_err ( |err| format ! ( "Error running rustfmt: {}" , err) )
75
- . and_then ( |out| String :: from_utf8 ( out. stdout )
76
- . map_err ( |_| "Formatted code is not valid UTF-8" . to_string ( ) ) )
77
49
}
78
50
}
79
51
80
- impl Formatter for Internal {
81
- fn format ( & self , input : String , config : Config ) -> Result < String , String > {
82
- let mut buf = Vec :: < u8 > :: new ( ) ;
52
+ fn format_external ( path : & PathBuf , cwd : & PathBuf , input : String , cfg : Config ) -> Result < String , String > {
53
+ let ( _file_handle, config_path) = gen_config_file ( & cfg) ?;
54
+ let args = rustfmt_args ( & cfg, & config_path) ;
55
+
56
+ let mut rustfmt = Command :: new ( path)
57
+ . args ( args)
58
+ . current_dir ( cwd)
59
+ . stdin ( Stdio :: piped ( ) )
60
+ . stdout ( Stdio :: piped ( ) )
61
+ . spawn ( )
62
+ . map_err ( |_| format ! ( "Couldn't spawn `{}`" , path. display( ) ) ) ?;
63
+
64
+ {
65
+ let stdin = rustfmt. stdin . as_mut ( )
66
+ . ok_or_else ( || "Failed to open rustfmt stdin" . to_string ( ) ) ?;
67
+ stdin. write_all ( input. as_bytes ( ) )
68
+ . map_err ( |_| "Failed to pass input to rustfmt" . to_string ( ) ) ?;
69
+ }
70
+
71
+ rustfmt. wait_with_output ( )
72
+ . map_err ( |err| format ! ( "Error running rustfmt: {}" , err) )
73
+ . and_then ( |out| String :: from_utf8 ( out. stdout )
74
+ . map_err ( |_| "Formatted code is not valid UTF-8" . to_string ( ) ) )
75
+ }
83
76
84
- {
85
- let mut session = Session :: new ( config , Some ( & mut buf ) ) ;
77
+ fn format_internal ( input : String , config : Config ) -> Result < String , String > {
78
+ let mut buf = Vec :: < u8 > :: new ( ) ;
86
79
87
- match session. format ( Input :: Text ( input) ) {
88
- Ok ( report) => {
89
- // Session::format returns Ok even if there are any errors, i.e., parsing errors.
90
- if session. has_operational_errors ( ) || session. has_parsing_errors ( ) {
91
- debug ! (
92
- "reformat: format_input failed: has errors, report = {}" ,
93
- report
94
- ) ;
80
+ {
81
+ let mut session = Session :: new ( config, Some ( & mut buf) ) ;
95
82
96
- return Err ( "Reformat failed to complete successfully" . into ( ) ) ;
97
- }
98
- }
99
- Err ( e) => {
100
- debug ! ( "Reformat failed: {:?}" , e) ;
83
+ match session. format ( Input :: Text ( input) ) {
84
+ Ok ( report) => {
85
+ // Session::format returns Ok even if there are any errors, i.e., parsing errors.
86
+ if session. has_operational_errors ( ) || session. has_parsing_errors ( ) {
87
+ debug ! (
88
+ "reformat: format_input failed: has errors, report = {}" ,
89
+ report
90
+ ) ;
101
91
102
92
return Err ( "Reformat failed to complete successfully" . into ( ) ) ;
103
93
}
104
94
}
105
- }
106
-
107
- String :: from_utf8 ( buf)
108
- . map_err ( |_| "Reformat output is not a valid UTF-8" . into ( ) )
109
- }
110
- }
95
+ Err ( e) => {
96
+ debug ! ( "Reformat failed: {:?}" , e) ;
111
97
112
- impl Formatter for Rustfmt {
113
- fn format ( & self , input : String , cfg : Config ) -> Result < String , String > {
114
- match self {
115
- Rustfmt :: Internal => Internal . format ( input, cfg) ,
116
- Rustfmt :: External ( path, cwd) => {
117
- External ( path. as_path ( ) , cwd. as_path ( ) ) . format ( input, cfg)
98
+ return Err ( "Reformat failed to complete successfully" . into ( ) ) ;
118
99
}
119
100
}
120
101
}
102
+
103
+ String :: from_utf8 ( buf)
104
+ . map_err ( |_| "Reformat output is not a valid UTF-8" . into ( ) )
121
105
}
122
106
123
107
fn random_file ( ) -> Result < ( File , PathBuf ) , String > {
0 commit comments