Skip to content

Commit 0c6515c

Browse files
feat: support style edition differing defaults in config loading
1 parent c0c3dc7 commit 0c6515c

File tree

2 files changed

+90
-26
lines changed

2 files changed

+90
-26
lines changed

Diff for: src/config/mod.rs

+44-17
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,21 @@ impl Config {
271271
///
272272
/// Returns a `Config` if the config could be read and parsed from
273273
/// the file, otherwise errors.
274-
pub(super) fn from_toml_path(file_path: &Path) -> Result<Config, Error> {
274+
pub(super) fn from_toml_path(
275+
file_path: &Path,
276+
edition: Option<Edition>,
277+
style_edition: Option<StyleEdition>,
278+
) -> Result<Config, Error> {
275279
let mut file = File::open(&file_path)?;
276280
let mut toml = String::new();
277281
file.read_to_string(&mut toml)?;
278-
Config::from_toml(&toml, file_path.parent().unwrap())
279-
.map_err(|err| Error::new(ErrorKind::InvalidData, err))
282+
Config::from_toml_for_style_edition(
283+
&toml,
284+
file_path.parent().unwrap(),
285+
edition,
286+
style_edition,
287+
)
288+
.map_err(|err| Error::new(ErrorKind::InvalidData, err))
280289
}
281290

282291
/// Resolves the config for input in `dir`.
@@ -288,7 +297,11 @@ impl Config {
288297
///
289298
/// Returns the `Config` to use, and the path of the project file if there was
290299
/// one.
291-
pub(super) fn from_resolved_toml_path(dir: &Path) -> Result<(Config, Option<PathBuf>), Error> {
300+
pub(super) fn from_resolved_toml_path(
301+
dir: &Path,
302+
edition: Option<Edition>,
303+
style_edition: Option<StyleEdition>,
304+
) -> Result<(Config, Option<PathBuf>), Error> {
292305
/// Try to find a project file in the given directory and its parents.
293306
/// Returns the path of the nearest project file if one exists,
294307
/// or `None` if no project file was found.
@@ -333,12 +346,26 @@ impl Config {
333346
}
334347

335348
match resolve_project_file(dir)? {
336-
None => Ok((Config::default(), None)),
337-
Some(path) => Config::from_toml_path(&path).map(|config| (config, Some(path))),
349+
None => Ok((
350+
Config::default_for_possible_style_edition(style_edition, edition),
351+
None,
352+
)),
353+
Some(path) => Config::from_toml_path(&path, edition, style_edition)
354+
.map(|config| (config, Some(path))),
338355
}
339356
}
340357

341-
pub(crate) fn from_toml(toml: &str, dir: &Path) -> Result<Config, String> {
358+
#[allow(dead_code)]
359+
pub(super) fn from_toml(toml: &str, dir: &Path) -> Result<Config, String> {
360+
Self::from_toml_for_style_edition(toml, dir, None, None)
361+
}
362+
363+
pub(crate) fn from_toml_for_style_edition(
364+
toml: &str,
365+
dir: &Path,
366+
edition: Option<Edition>,
367+
style_edition: Option<StyleEdition>,
368+
) -> Result<Config, String> {
342369
let parsed: ::toml::Value = toml
343370
.parse()
344371
.map_err(|e| format!("Could not parse TOML: {}", e))?;
@@ -358,7 +385,7 @@ impl Config {
358385
if !err.is_empty() {
359386
eprint!("{err}");
360387
}
361-
Ok(parsed_config.to_parsed_config(None, None, dir))
388+
Ok(parsed_config.to_parsed_config(style_edition, edition, dir))
362389
}
363390
Err(e) => {
364391
err.push_str("Error: Decoding config file failed:\n");
@@ -376,21 +403,21 @@ pub fn load_config<O: CliOptions>(
376403
file_path: Option<&Path>,
377404
options: Option<O>,
378405
) -> Result<(Config, Option<PathBuf>), Error> {
379-
let (over_ride, _edition, _style_edition) = match options {
380-
Some(ref opts) => (
381-
config_path(opts)?,
382-
opts.edition(),
383-
opts.style_edition(),
384-
),
406+
let (over_ride, edition, style_edition) = match options {
407+
Some(ref opts) => (config_path(opts)?, opts.edition(), opts.style_edition()),
385408
None => (None, None, None),
386409
};
387410

388411
let result = if let Some(over_ride) = over_ride {
389-
Config::from_toml_path(over_ride.as_ref()).map(|p| (p, Some(over_ride.to_owned())))
412+
Config::from_toml_path(over_ride.as_ref(), edition, style_edition)
413+
.map(|p| (p, Some(over_ride.to_owned())))
390414
} else if let Some(file_path) = file_path {
391-
Config::from_resolved_toml_path(file_path)
415+
Config::from_resolved_toml_path(file_path, edition, style_edition)
392416
} else {
393-
Ok((Config::default(), None))
417+
Ok((
418+
Config::default_for_possible_style_edition(style_edition, edition),
419+
None,
420+
))
394421
};
395422

396423
result.map(|(mut c, p)| {

Diff for: src/test/mod.rs

+46-9
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ use std::iter::Peekable;
66
use std::mem;
77
use std::path::{Path, PathBuf};
88
use std::process::{Command, Stdio};
9-
use std::str::Chars;
9+
use std::str::{Chars, FromStr};
1010
use std::thread;
1111

1212
use crate::config::{Color, Config, EmitMode, FileName, NewlineStyle};
1313
use crate::formatting::{ReportedErrors, SourceFile};
1414
use crate::rustfmt_diff::{make_diff, print_diff, DiffLine, Mismatch, ModifiedChunk, OutputWriter};
1515
use crate::source_file;
16-
use crate::{is_nightly_channel, FormatReport, FormatReportFormatterBuilder, Input, Session};
16+
use crate::{
17+
is_nightly_channel, Edition, FormatReport, FormatReportFormatterBuilder, Input, Session,
18+
StyleEdition,
19+
};
1720

1821
use rustfmt_config_proc_macro::nightly_only_test;
1922

@@ -710,13 +713,22 @@ fn print_mismatches<T: Fn(u32) -> String>(
710713

711714
fn read_config(filename: &Path) -> Config {
712715
let sig_comments = read_significant_comments(filename);
716+
let (edition, style_edition) = get_editions_from_comments(&sig_comments);
713717
// Look for a config file. If there is a 'config' property in the significant comments, use
714718
// that. Otherwise, if there are no significant comments at all, look for a config file with
715719
// the same name as the test file.
716720
let mut config = if !sig_comments.is_empty() {
717-
get_config(sig_comments.get("config").map(Path::new))
721+
get_config(
722+
sig_comments.get("config").map(Path::new),
723+
edition,
724+
style_edition,
725+
)
718726
} else {
719-
get_config(filename.with_extension("toml").file_name().map(Path::new))
727+
get_config(
728+
filename.with_extension("toml").file_name().map(Path::new),
729+
edition,
730+
style_edition,
731+
)
720732
};
721733

722734
for (key, val) in &sig_comments {
@@ -747,13 +759,28 @@ enum IdempotentCheckError {
747759
Parse,
748760
}
749761

762+
fn get_editions_from_comments(
763+
comments: &HashMap<String, String>,
764+
) -> (Option<Edition>, Option<StyleEdition>) {
765+
(
766+
comments
767+
.get("edition")
768+
.map(|e| Edition::from_str(e).expect(&format!("invalid edition value: '{}'", e))),
769+
comments.get("style_edition").map(|se| {
770+
StyleEdition::from_str(se).expect(&format!("invalid style_edition value: '{}'", se))
771+
}),
772+
)
773+
}
774+
750775
fn idempotent_check(
751776
filename: &PathBuf,
752777
opt_config: &Option<PathBuf>,
753778
) -> Result<FormatReport, IdempotentCheckError> {
754779
let sig_comments = read_significant_comments(filename);
755780
let config = if let Some(ref config_file_path) = opt_config {
756-
Config::from_toml_path(config_file_path).expect("`rustfmt.toml` not found")
781+
let (edition, style_edition) = get_editions_from_comments(&sig_comments);
782+
Config::from_toml_path(config_file_path, edition, style_edition)
783+
.expect("`rustfmt.toml` not found")
757784
} else {
758785
read_config(filename)
759786
};
@@ -777,14 +804,18 @@ fn idempotent_check(
777804
// Reads test config file using the supplied (optional) file name. If there's no file name or the
778805
// file doesn't exist, just return the default config. Otherwise, the file must be read
779806
// successfully.
780-
fn get_config(config_file: Option<&Path>) -> Config {
807+
fn get_config(
808+
config_file: Option<&Path>,
809+
edition: Option<Edition>,
810+
style_edition: Option<StyleEdition>,
811+
) -> Config {
781812
let config_file_name = match config_file {
782-
None => return Default::default(),
813+
None => return Config::default_for_possible_style_edition(style_edition, edition),
783814
Some(file_name) => {
784815
let mut full_path = PathBuf::from("tests/config/");
785816
full_path.push(file_name);
786817
if !full_path.exists() {
787-
return Default::default();
818+
return Config::default_for_possible_style_edition(style_edition, edition);
788819
};
789820
full_path
790821
}
@@ -796,7 +827,13 @@ fn get_config(config_file: Option<&Path>) -> Config {
796827
.read_to_string(&mut def_config)
797828
.expect("Couldn't read config");
798829

799-
Config::from_toml(&def_config, Path::new("tests/config/")).expect("invalid TOML")
830+
Config::from_toml_for_style_edition(
831+
&def_config,
832+
Path::new("tests/config/"),
833+
edition,
834+
style_edition,
835+
)
836+
.expect("invalid TOML")
800837
}
801838

802839
// Reads significant comments of the form: `// rustfmt-key: value` into a hash map.

0 commit comments

Comments
 (0)