@@ -22,9 +22,6 @@ use log::{log, debug};
2222use rustfmt_nightly:: { Config , Session , Input } ;
2323use serde_json;
2424
25- struct External < ' a > ( & ' a Path , & ' a Path ) ;
26- struct Internal ;
27-
2825/// Specified which `rustfmt` to use.
2926#[ derive( Clone ) ]
3027pub enum Rustfmt {
@@ -43,81 +40,68 @@ impl From<Option<(String, PathBuf)>> for Rustfmt {
4340 }
4441}
4542
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) ,
7148 }
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 ( ) ) )
7749 }
7850}
7951
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+ }
8376
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 ( ) ;
8679
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) ) ;
9582
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+ ) ;
10191
10292 return Err ( "Reformat failed to complete successfully" . into ( ) ) ;
10393 }
10494 }
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) ;
11197
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 ( ) ) ;
11899 }
119100 }
120101 }
102+
103+ String :: from_utf8 ( buf)
104+ . map_err ( |_| "Reformat output is not a valid UTF-8" . into ( ) )
121105}
122106
123107fn random_file ( ) -> Result < ( File , PathBuf ) , String > {
0 commit comments