10
10
11
11
// TODO: add tests
12
12
13
- use std:: fs:: { self , File } ;
14
- use std:: io:: { self , BufWriter , Read , Write } ;
15
- use std:: path:: Path ;
13
+ use std:: fs;
14
+ use std:: io:: { self , Write } ;
16
15
17
16
use checkstyle:: output_checkstyle_file;
18
- use config:: { Config , EmitMode , FileName , NewlineStyle , Verbosity } ;
19
- use rustfmt_diff:: { make_diff, output_modified, print_diff, Mismatch } ;
17
+ use config:: { Config , EmitMode , FileName , Verbosity } ;
18
+ use rustfmt_diff:: { make_diff, output_modified, print_diff} ;
20
19
21
20
#[ cfg( test) ]
22
21
use FileRecord ;
@@ -48,72 +47,15 @@ where
48
47
Ok ( ( ) )
49
48
}
50
49
51
- // Prints all newlines either as `\n` or as `\r\n`.
52
- pub fn write_system_newlines < T > ( writer : T , text : & str , config : & Config ) -> Result < ( ) , io:: Error >
53
- where
54
- T : Write ,
55
- {
56
- // Buffer output, since we're writing a since char at a time.
57
- let mut writer = BufWriter :: new ( writer) ;
58
-
59
- let style = if config. newline_style ( ) == NewlineStyle :: Native {
60
- if cfg ! ( windows) {
61
- NewlineStyle :: Windows
62
- } else {
63
- NewlineStyle :: Unix
64
- }
65
- } else {
66
- config. newline_style ( )
67
- } ;
68
-
69
- match style {
70
- NewlineStyle :: Unix => write ! ( writer, "{}" , text) ,
71
- NewlineStyle :: Windows => {
72
- for c in text. chars ( ) {
73
- match c {
74
- '\n' => write ! ( writer, "\r \n " ) ?,
75
- '\r' => continue ,
76
- c => write ! ( writer, "{}" , c) ?,
77
- }
78
- }
79
- Ok ( ( ) )
80
- }
81
- NewlineStyle :: Native => unreachable ! ( ) ,
82
- }
83
- }
84
-
85
50
pub fn write_file < T > (
86
- text : & str ,
51
+ formatted_text : & str ,
87
52
filename : & FileName ,
88
53
out : & mut T ,
89
54
config : & Config ,
90
55
) -> Result < bool , io:: Error >
91
56
where
92
57
T : Write ,
93
58
{
94
- fn source_and_formatted_text (
95
- text : & str ,
96
- filename : & Path ,
97
- config : & Config ,
98
- ) -> Result < ( String , String ) , io:: Error > {
99
- let mut f = File :: open ( filename) ?;
100
- let mut ori_text = String :: new ( ) ;
101
- f. read_to_string ( & mut ori_text) ?;
102
- let mut v = Vec :: new ( ) ;
103
- write_system_newlines ( & mut v, text, config) ?;
104
- let fmt_text = String :: from_utf8 ( v) . unwrap ( ) ;
105
- Ok ( ( ori_text, fmt_text) )
106
- }
107
-
108
- fn create_diff (
109
- filename : & Path ,
110
- text : & str ,
111
- config : & Config ,
112
- ) -> Result < Vec < Mismatch > , io:: Error > {
113
- let ( ori, fmt) = source_and_formatted_text ( text, filename, config) ?;
114
- Ok ( make_diff ( & ori, & fmt, 3 ) )
115
- }
116
-
117
59
let filename_to_path = || match * filename {
118
60
FileName :: Real ( ref path) => path,
119
61
_ => panic ! ( "cannot format `{}` and emit to files" , filename) ,
@@ -122,65 +64,58 @@ where
122
64
match config. emit_mode ( ) {
123
65
EmitMode :: Files if config. make_backup ( ) => {
124
66
let filename = filename_to_path ( ) ;
125
- if let Ok ( ( ori, fmt) ) = source_and_formatted_text ( text, filename, config) {
126
- if fmt != ori {
127
- // Do a little dance to make writing safer - write to a temp file
128
- // rename the original to a .bk, then rename the temp file to the
129
- // original.
130
- let tmp_name = filename. with_extension ( "tmp" ) ;
131
- let bk_name = filename. with_extension ( "bk" ) ;
132
- {
133
- // Write text to temp file
134
- let tmp_file = File :: create ( & tmp_name) ?;
135
- write_system_newlines ( tmp_file, text, config) ?;
136
- }
137
-
138
- fs:: rename ( filename, bk_name) ?;
139
- fs:: rename ( tmp_name, filename) ?;
140
- }
67
+ let ori = fs:: read_to_string ( filename) ?;
68
+ if ori != formatted_text {
69
+ // Do a little dance to make writing safer - write to a temp file
70
+ // rename the original to a .bk, then rename the temp file to the
71
+ // original.
72
+ let tmp_name = filename. with_extension ( "tmp" ) ;
73
+ let bk_name = filename. with_extension ( "bk" ) ;
74
+
75
+ fs:: write ( & tmp_name, formatted_text) ?;
76
+ fs:: rename ( filename, bk_name) ?;
77
+ fs:: rename ( tmp_name, filename) ?;
141
78
}
142
79
}
143
80
EmitMode :: Files => {
144
81
// Write text directly over original file if there is a diff.
145
82
let filename = filename_to_path ( ) ;
146
- let ( source, formatted) = source_and_formatted_text ( text, filename, config) ?;
147
- if source != formatted {
148
- let file = File :: create ( filename) ?;
149
- write_system_newlines ( file, text, config) ?;
83
+ let ori = fs:: read_to_string ( filename) ?;
84
+ if ori != formatted_text {
85
+ fs:: write ( filename, formatted_text) ?;
150
86
}
151
87
}
152
88
EmitMode :: Stdout | EmitMode :: Coverage => {
153
89
if config. verbose ( ) != Verbosity :: Quiet {
154
90
println ! ( "{}:\n " , filename) ;
155
91
}
156
- write_system_newlines ( out, text , config ) ?;
92
+ write ! ( out, "{}" , formatted_text ) ?;
157
93
}
158
94
EmitMode :: ModifiedLines => {
159
95
let filename = filename_to_path ( ) ;
160
- if let Ok ( ( ori, fmt) ) = source_and_formatted_text ( text, filename, config) {
161
- let mismatch = make_diff ( & ori, & fmt, 0 ) ;
162
- let has_diff = !mismatch. is_empty ( ) ;
163
- output_modified ( out, mismatch) ;
164
- return Ok ( has_diff) ;
165
- }
96
+ let ori = fs:: read_to_string ( filename) ?;
97
+ let mismatch = make_diff ( & ori, formatted_text, 0 ) ;
98
+ let has_diff = !mismatch. is_empty ( ) ;
99
+ output_modified ( out, mismatch) ;
100
+ return Ok ( has_diff) ;
166
101
}
167
102
EmitMode :: Checkstyle => {
168
103
let filename = filename_to_path ( ) ;
169
- let diff = create_diff ( filename, text, config) ?;
104
+ let ori = fs:: read_to_string ( filename) ?;
105
+ let diff = make_diff ( & ori, formatted_text, 3 ) ;
170
106
output_checkstyle_file ( out, filename, diff) ?;
171
107
}
172
108
EmitMode :: Diff => {
173
109
let filename = filename_to_path ( ) ;
174
- if let Ok ( ( ori, fmt) ) = source_and_formatted_text ( text, filename, config) {
175
- let mismatch = make_diff ( & ori, & fmt, 3 ) ;
176
- let has_diff = !mismatch. is_empty ( ) ;
177
- print_diff (
178
- mismatch,
179
- |line_num| format ! ( "Diff in {} at line {}:" , filename. display( ) , line_num) ,
180
- config,
181
- ) ;
182
- return Ok ( has_diff) ;
183
- }
110
+ let ori = fs:: read_to_string ( filename) ?;
111
+ let mismatch = make_diff ( & ori, formatted_text, 3 ) ;
112
+ let has_diff = !mismatch. is_empty ( ) ;
113
+ print_diff (
114
+ mismatch,
115
+ |line_num| format ! ( "Diff in {} at line {}:" , filename. display( ) , line_num) ,
116
+ config,
117
+ ) ;
118
+ return Ok ( has_diff) ;
184
119
}
185
120
}
186
121
0 commit comments