1
1
// High level formatting functions.
2
2
3
3
use std:: collections:: HashMap ;
4
+ use std:: ffi:: OsStr ;
4
5
use std:: io:: { self , Write } ;
6
+ use std:: path:: PathBuf ;
5
7
use std:: rc:: Rc ;
6
8
use std:: time:: { Duration , Instant } ;
7
9
@@ -13,6 +15,7 @@ use self::newline_style::apply_newline_style;
13
15
use crate :: comment:: { CharClasses , FullCodeCharKind } ;
14
16
use crate :: config:: { Config , FileName , Verbosity } ;
15
17
use crate :: formatting:: generated:: is_generated_file;
18
+ use crate :: ignore_path:: IgnorePathSet ;
16
19
use crate :: issues:: BadIssueSeeker ;
17
20
use crate :: modules:: Module ;
18
21
use crate :: parse:: parser:: { DirectoryOwnership , Parser , ParserError } ;
@@ -39,6 +42,15 @@ impl<'b, T: Write + 'b> Session<'b, T> {
39
42
return Err ( ErrorKind :: VersionMismatch ) ;
40
43
}
41
44
45
+ let cargo_toml = Some ( OsStr :: new ( "Cargo.toml" ) ) ;
46
+ match input {
47
+ Input :: File ( path) if path. file_name ( ) == cargo_toml => {
48
+ let config = & self . config . clone ( ) ;
49
+ return format_cargo_toml ( path, config, self ) ;
50
+ }
51
+ _ => { }
52
+ }
53
+
42
54
rustc_span:: create_session_if_not_set_then ( self . config . edition ( ) . into ( ) , |_| {
43
55
if self . config . disable_all_formatting ( ) {
44
56
// When the input is from stdin, echo back the input.
@@ -164,6 +176,29 @@ fn format_project<T: FormatHandler>(
164
176
Ok ( context. report )
165
177
}
166
178
179
+ fn format_cargo_toml < T : FormatHandler > (
180
+ path : PathBuf ,
181
+ config : & Config ,
182
+ handler : & mut T ,
183
+ ) -> Result < FormatReport , ErrorKind > {
184
+ let mut report = FormatReport :: new ( ) ;
185
+
186
+ let ignore_path_set = IgnorePathSet :: from_ignore_list ( & config. ignore ( ) ) ?;
187
+ let file_name = FileName :: Real ( path. clone ( ) ) ;
188
+ if ignore_path_set. is_match ( & file_name) {
189
+ return Ok ( report) ;
190
+ }
191
+
192
+ let input = std:: fs:: read_to_string ( & path) ?;
193
+ let mut result = cargo_toml:: format_cargo_toml_inner ( & input, config) ?;
194
+
195
+ apply_newline_style ( config. newline_style ( ) , & mut result, & input) ;
196
+
197
+ handler. handle_formatted_file ( None , file_name, result, & mut report) ?;
198
+
199
+ Ok ( report)
200
+ }
201
+
167
202
// Used for formatting files.
168
203
#[ derive( new) ]
169
204
struct FormatContext < ' a , T : FormatHandler > {
@@ -232,7 +267,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
232
267
. add_non_formatted_ranges ( visitor. skipped_range . borrow ( ) . clone ( ) ) ;
233
268
234
269
self . handler . handle_formatted_file (
235
- & self . parse_session ,
270
+ Some ( & self . parse_session ) ,
236
271
path,
237
272
visitor. buffer . to_owned ( ) ,
238
273
& mut self . report ,
@@ -244,7 +279,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
244
279
trait FormatHandler {
245
280
fn handle_formatted_file (
246
281
& mut self ,
247
- parse_session : & ParseSess ,
282
+ parse_session : Option < & ParseSess > ,
248
283
path : FileName ,
249
284
result : String ,
250
285
report : & mut FormatReport ,
@@ -255,14 +290,14 @@ impl<'b, T: Write + 'b> FormatHandler for Session<'b, T> {
255
290
// Called for each formatted file.
256
291
fn handle_formatted_file (
257
292
& mut self ,
258
- parse_session : & ParseSess ,
293
+ parse_session : Option < & ParseSess > ,
259
294
path : FileName ,
260
295
result : String ,
261
296
report : & mut FormatReport ,
262
297
) -> Result < ( ) , ErrorKind > {
263
298
if let Some ( ref mut out) = self . out {
264
299
match source_file:: write_file (
265
- Some ( parse_session) ,
300
+ parse_session,
266
301
& path,
267
302
& result,
268
303
out,
0 commit comments