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:: time:: { Duration , Instant } ;
6
8
7
9
use rustc_ast:: ast;
@@ -11,6 +13,7 @@ use self::newline_style::apply_newline_style;
11
13
use crate :: comment:: { CharClasses , FullCodeCharKind } ;
12
14
use crate :: config:: { Config , FileName , Verbosity } ;
13
15
use crate :: formatting:: generated:: is_generated_file;
16
+ use crate :: ignore_path:: IgnorePathSet ;
14
17
use crate :: modules:: Module ;
15
18
use crate :: parse:: parser:: { DirectoryOwnership , Parser , ParserError } ;
16
19
use crate :: parse:: session:: ParseSess ;
@@ -36,6 +39,15 @@ impl<'b, T: Write + 'b> Session<'b, T> {
36
39
return Err ( ErrorKind :: VersionMismatch ) ;
37
40
}
38
41
42
+ let cargo_toml = Some ( OsStr :: new ( "Cargo.toml" ) ) ;
43
+ match input {
44
+ Input :: File ( path) if path. file_name ( ) == cargo_toml => {
45
+ let config = & self . config . clone ( ) ;
46
+ return format_cargo_toml ( path, config, self ) ;
47
+ }
48
+ _ => { }
49
+ }
50
+
39
51
rustc_span:: create_session_if_not_set_then ( self . config . edition ( ) . into ( ) , |_| {
40
52
if self . config . disable_all_formatting ( ) {
41
53
// When the input is from stdin, echo back the input.
@@ -175,6 +187,29 @@ fn format_project<T: FormatHandler>(
175
187
Ok ( context. report )
176
188
}
177
189
190
+ fn format_cargo_toml < T : FormatHandler > (
191
+ path : PathBuf ,
192
+ config : & Config ,
193
+ handler : & mut T ,
194
+ ) -> Result < FormatReport , ErrorKind > {
195
+ let mut report = FormatReport :: new ( ) ;
196
+
197
+ let ignore_path_set = IgnorePathSet :: from_ignore_list ( & config. ignore ( ) ) ?;
198
+ let file_name = FileName :: Real ( path. clone ( ) ) ;
199
+ if ignore_path_set. is_match ( & file_name) {
200
+ return Ok ( report) ;
201
+ }
202
+
203
+ let input = std:: fs:: read_to_string ( & path) ?;
204
+ let mut result = cargo_toml:: format_cargo_toml_inner ( & input, config) ?;
205
+
206
+ apply_newline_style ( config. newline_style ( ) , & mut result, & input) ;
207
+
208
+ handler. handle_formatted_file ( None , file_name, result, & mut report) ?;
209
+
210
+ Ok ( report)
211
+ }
212
+
178
213
// Used for formatting files.
179
214
#[ derive( new) ]
180
215
struct FormatContext < ' a , T : FormatHandler > {
@@ -242,7 +277,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
242
277
. add_non_formatted_ranges ( visitor. skipped_range . borrow ( ) . clone ( ) ) ;
243
278
244
279
self . handler . handle_formatted_file (
245
- & self . parse_session ,
280
+ Some ( & self . parse_session ) ,
246
281
path,
247
282
visitor. buffer . to_owned ( ) ,
248
283
& mut self . report ,
@@ -254,7 +289,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
254
289
trait FormatHandler {
255
290
fn handle_formatted_file (
256
291
& mut self ,
257
- parse_session : & ParseSess ,
292
+ parse_session : Option < & ParseSess > ,
258
293
path : FileName ,
259
294
result : String ,
260
295
report : & mut FormatReport ,
@@ -265,14 +300,14 @@ impl<'b, T: Write + 'b> FormatHandler for Session<'b, T> {
265
300
// Called for each formatted file.
266
301
fn handle_formatted_file (
267
302
& mut self ,
268
- parse_session : & ParseSess ,
303
+ parse_session : Option < & ParseSess > ,
269
304
path : FileName ,
270
305
result : String ,
271
306
report : & mut FormatReport ,
272
307
) -> Result < ( ) , ErrorKind > {
273
308
if let Some ( ref mut out) = self . out {
274
309
match source_file:: write_file (
275
- Some ( parse_session) ,
310
+ parse_session,
276
311
& path,
277
312
& result,
278
313
out,
0 commit comments