11// High level formatting functions.
22
33use std:: collections:: HashMap ;
4+ use std:: ffi:: OsStr ;
45use std:: io:: { self , Write } ;
6+ use std:: path:: PathBuf ;
57use std:: rc:: Rc ;
68use std:: time:: { Duration , Instant } ;
79
@@ -13,6 +15,7 @@ use self::newline_style::apply_newline_style;
1315use crate :: comment:: { CharClasses , FullCodeCharKind } ;
1416use crate :: config:: { Config , FileName , Verbosity } ;
1517use crate :: formatting:: generated:: is_generated_file;
18+ use crate :: ignore_path:: IgnorePathSet ;
1619use crate :: issues:: BadIssueSeeker ;
1720use crate :: modules:: Module ;
1821use crate :: parse:: parser:: { DirectoryOwnership , Parser , ParserError } ;
@@ -39,6 +42,15 @@ impl<'b, T: Write + 'b> Session<'b, T> {
3942 return Err ( ErrorKind :: VersionMismatch ) ;
4043 }
4144
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+
4254 rustc_span:: create_session_if_not_set_then ( self . config . edition ( ) . into ( ) , |_| {
4355 if self . config . disable_all_formatting ( ) {
4456 // When the input is from stdin, echo back the input.
@@ -164,6 +176,29 @@ fn format_project<T: FormatHandler>(
164176 Ok ( context. report )
165177}
166178
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+
167202// Used for formatting files.
168203#[ derive( new) ]
169204struct FormatContext < ' a , T : FormatHandler > {
@@ -232,7 +267,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
232267 . add_non_formatted_ranges ( visitor. skipped_range . borrow ( ) . clone ( ) ) ;
233268
234269 self . handler . handle_formatted_file (
235- & self . parse_session ,
270+ Some ( & self . parse_session ) ,
236271 path,
237272 visitor. buffer . to_owned ( ) ,
238273 & mut self . report ,
@@ -244,7 +279,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
244279trait FormatHandler {
245280 fn handle_formatted_file (
246281 & mut self ,
247- parse_session : & ParseSess ,
282+ parse_session : Option < & ParseSess > ,
248283 path : FileName ,
249284 result : String ,
250285 report : & mut FormatReport ,
@@ -255,14 +290,14 @@ impl<'b, T: Write + 'b> FormatHandler for Session<'b, T> {
255290 // Called for each formatted file.
256291 fn handle_formatted_file (
257292 & mut self ,
258- parse_session : & ParseSess ,
293+ parse_session : Option < & ParseSess > ,
259294 path : FileName ,
260295 result : String ,
261296 report : & mut FormatReport ,
262297 ) -> Result < ( ) , ErrorKind > {
263298 if let Some ( ref mut out) = self . out {
264299 match source_file:: write_file (
265- Some ( parse_session) ,
300+ parse_session,
266301 & path,
267302 & result,
268303 out,
0 commit comments