Skip to content

Commit f6d778b

Browse files
committed
Add format_cargo_toml to the code path called by rustfmt
Now users can provide the path to their `Cargo.toml` files for rustfmt to format.
1 parent ae980ec commit f6d778b

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

src/formatting.rs

+39-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// High level formatting functions.
22

33
use std::collections::HashMap;
4+
use std::ffi::OsStr;
45
use std::io::{self, Write};
6+
use std::path::PathBuf;
57
use std::rc::Rc;
68
use std::time::{Duration, Instant};
79

@@ -13,6 +15,7 @@ use self::newline_style::apply_newline_style;
1315
use crate::comment::{CharClasses, FullCodeCharKind};
1416
use crate::config::{Config, FileName, Verbosity};
1517
use crate::formatting::generated::is_generated_file;
18+
use crate::ignore_path::IgnorePathSet;
1619
use crate::issues::BadIssueSeeker;
1720
use crate::modules::Module;
1821
use 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)]
169204
struct 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> {
244279
trait 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,

src/formatting/cargo_toml.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use itertools::Itertools;
22
use std::cmp::Ordering;
3-
use toml_edit::{visit_mut::*, Decor, Document, Formatted, Item, KeyMut, Table, TableLike, Value};
3+
use toml_edit::{
4+
visit_mut::*, Decor, Document, Formatted, Item, KeyMut, Table, TableLike, TomlError, Value,
5+
};
46

5-
use crate::Config;
7+
use crate::{Config, ErrorKind};
68

7-
#[allow(dead_code)]
8-
fn format_cargo_toml(content: &str, config: &Config) -> String {
9-
let mut doc = content.parse::<toml_edit::Document>().unwrap();
9+
pub(crate) fn format_cargo_toml_inner(content: &str, config: &Config) -> Result<String, ErrorKind> {
10+
let mut doc = content.parse::<toml_edit::Document>()?;
1011
let rules: Vec<Box<dyn VisitMut>> = vec![
1112
Box::new(SortSection {
1213
current_position: 0,
@@ -27,7 +28,14 @@ fn format_cargo_toml(content: &str, config: &Config) -> String {
2728
for mut rule in rules.into_iter() {
2829
rule.visit_document_mut(&mut doc);
2930
}
30-
return doc.to_string();
31+
32+
Ok(doc.to_string())
33+
}
34+
35+
impl From<TomlError> for ErrorKind {
36+
fn from(_: TomlError) -> Self {
37+
ErrorKind::ParseError
38+
}
3139
}
3240

3341
/// Sort key names alphabetically within each section, with the exception of the
@@ -363,7 +371,7 @@ mod tests {
363371
d = "git-rustfmt"
364372
c = "src/git-rustfmt/main.rs""#;
365373

366-
let formatted = format_cargo_toml(s, &Default::default());
374+
let formatted = format_cargo_toml_inner(s, &Default::default()).unwrap();
367375

368376
#[rustfmt::skip]
369377
let expected = r#"[package]

0 commit comments

Comments
 (0)