refactor: track configs set from cli flags

This commit is contained in:
Caleb Cartwright 2024-07-26 17:43:50 -05:00 committed by Caleb Cartwright
parent e21c1e220b
commit a23d3ccb84
4 changed files with 113 additions and 21 deletions

View File

@ -263,24 +263,35 @@ fn format_string(input: String, options: GetOptsOptions) -> Result<i32> {
let (mut config, _) = load_config(Some(Path::new(".")), Some(options.clone()))?; let (mut config, _) = load_config(Some(Path::new(".")), Some(options.clone()))?;
if options.check { if options.check {
config.set().emit_mode(EmitMode::Diff); config.set_cli().emit_mode(EmitMode::Diff);
} else { } else {
match options.emit_mode { match options.emit_mode {
// Emit modes which work with standard input // Emit modes which work with standard input
// None means default, which is Stdout. // None means default, which is Stdout.
None | Some(EmitMode::Stdout) | Some(EmitMode::Checkstyle) | Some(EmitMode::Json) => {} None => {
config
.set()
.emit_mode(options.emit_mode.unwrap_or(EmitMode::Stdout));
}
Some(EmitMode::Stdout) | Some(EmitMode::Checkstyle) | Some(EmitMode::Json) => {
config
.set_cli()
.emit_mode(options.emit_mode.unwrap_or(EmitMode::Stdout));
}
Some(emit_mode) => { Some(emit_mode) => {
return Err(OperationError::StdinBadEmit(emit_mode).into()); return Err(OperationError::StdinBadEmit(emit_mode).into());
} }
} }
config
.set()
.emit_mode(options.emit_mode.unwrap_or(EmitMode::Stdout));
} }
config.set().verbose(Verbosity::Quiet); config.set().verbose(Verbosity::Quiet);
// parse file_lines // parse file_lines
config.set().file_lines(options.file_lines); if options.file_lines.is_all() {
config.set().file_lines(options.file_lines);
} else {
config.set_cli().file_lines(options.file_lines);
}
for f in config.file_lines().files() { for f in config.file_lines().files() {
match *f { match *f {
FileName::Stdin => {} FileName::Stdin => {}
@ -650,36 +661,46 @@ fn verify_file_lines(&self, files: &[PathBuf]) {
impl CliOptions for GetOptsOptions { impl CliOptions for GetOptsOptions {
fn apply_to(self, config: &mut Config) { fn apply_to(self, config: &mut Config) {
if self.verbose { if self.verbose {
config.set().verbose(Verbosity::Verbose); config.set_cli().verbose(Verbosity::Verbose);
} else if self.quiet { } else if self.quiet {
config.set().verbose(Verbosity::Quiet); config.set_cli().verbose(Verbosity::Quiet);
} else { } else {
config.set().verbose(Verbosity::Normal); config.set().verbose(Verbosity::Normal);
} }
config.set().file_lines(self.file_lines);
config.set().unstable_features(self.unstable_features); if self.file_lines.is_all() {
config.set().file_lines(self.file_lines);
} else {
config.set_cli().file_lines(self.file_lines);
}
if self.unstable_features {
config.set_cli().unstable_features(self.unstable_features);
} else {
config.set().unstable_features(self.unstable_features);
}
if let Some(skip_children) = self.skip_children { if let Some(skip_children) = self.skip_children {
config.set().skip_children(skip_children); config.set_cli().skip_children(skip_children);
} }
if let Some(error_on_unformatted) = self.error_on_unformatted { if let Some(error_on_unformatted) = self.error_on_unformatted {
config.set().error_on_unformatted(error_on_unformatted); config.set_cli().error_on_unformatted(error_on_unformatted);
} }
if let Some(edition) = self.edition { if let Some(edition) = self.edition {
config.set().edition(edition); config.set_cli().edition(edition);
} }
if self.check { if self.check {
config.set().emit_mode(EmitMode::Diff); config.set_cli().emit_mode(EmitMode::Diff);
} else if let Some(emit_mode) = self.emit_mode { } else if let Some(emit_mode) = self.emit_mode {
config.set().emit_mode(emit_mode); config.set_cli().emit_mode(emit_mode);
} }
if self.backup { if self.backup {
config.set().make_backup(true); config.set_cli().make_backup(true);
} }
if let Some(color) = self.color { if let Some(color) = self.color {
config.set().color(color); config.set_cli().color(color);
} }
if self.print_misformatted_file_names { if self.print_misformatted_file_names {
config.set().print_misformatted_file_names(true); config.set_cli().print_misformatted_file_names(true);
} }
for (key, val) in self.inline_config { for (key, val) in self.inline_config {

View File

@ -89,7 +89,10 @@ pub struct Config {
// - 1: true if the option was manually initialized // - 1: true if the option was manually initialized
// - 2: the option value // - 2: the option value
// - 3: true if the option is unstable // - 3: true if the option is unstable
$($i: (Cell<bool>, bool, <$ty as StyleEditionDefault>::ConfigType, bool)),+ // - 4: true if the option was set manually from a CLI flag
// FIXME: 4 is probably unnecessary and duplicative
// https://github.com/rust-lang/rustfmt/issues/6252
$($i: (Cell<bool>, bool, <$ty as StyleEditionDefault>::ConfigType, bool, bool)),+
} }
// Just like the Config struct but with each property wrapped // Just like the Config struct but with each property wrapped
@ -136,6 +139,35 @@ pub fn $i(&mut self, value: <$ty as StyleEditionDefault>::ConfigType) {
)+ )+
} }
#[allow(unreachable_pub)]
pub struct CliConfigSetter<'a>(&'a mut Config);
impl<'a> CliConfigSetter<'a> {
$(
#[allow(unreachable_pub)]
pub fn $i(&mut self, value: <$ty as StyleEditionDefault>::ConfigType) {
(self.0).$i.2 = value;
(self.0).$i.4 = true;
match stringify!($i) {
"max_width"
| "use_small_heuristics"
| "fn_call_width"
| "single_line_if_else_max_width"
| "single_line_let_else_max_width"
| "attr_fn_like_width"
| "struct_lit_width"
| "struct_variant_width"
| "array_width"
| "chain_width" => self.0.set_heuristics(),
"merge_imports" => self.0.set_merge_imports(),
"fn_args_layout" => self.0.set_fn_args_layout(),
"hide_parse_errors" => self.0.set_hide_parse_errors(),
&_ => (),
}
}
)+
}
// Query each option, returns true if the user set the option, false if // Query each option, returns true if the user set the option, false if
// a default was used. // a default was used.
#[allow(unreachable_pub)] #[allow(unreachable_pub)]
@ -150,6 +182,20 @@ pub fn $i(&self) -> bool {
)+ )+
} }
// Query each option, returns true if the user set the option via a CLI flag,
// false if a default was used.
#[allow(unreachable_pub)]
pub struct CliConfigWasSet<'a>(&'a Config);
impl<'a> CliConfigWasSet<'a> {
$(
#[allow(unreachable_pub)]
pub fn $i(&self) -> bool {
(self.0).$i.4
}
)+
}
impl Config { impl Config {
$( $(
#[allow(unreachable_pub)] #[allow(unreachable_pub)]
@ -169,7 +215,8 @@ pub fn default_with_style_edition(style_edition: StyleEdition) -> Config {
<$ty as StyleEditionDefault>::style_edition_default( <$ty as StyleEditionDefault>::style_edition_default(
style_edition style_edition
), ),
$stb $stb,
false,
), ),
)+ )+
} }
@ -180,11 +227,21 @@ pub fn set(&mut self) -> ConfigSetter<'_> {
ConfigSetter(self) ConfigSetter(self)
} }
#[allow(unreachable_pub)]
pub fn set_cli(&mut self) -> CliConfigSetter<'_> {
CliConfigSetter(self)
}
#[allow(unreachable_pub)] #[allow(unreachable_pub)]
pub fn was_set(&self) -> ConfigWasSet<'_> { pub fn was_set(&self) -> ConfigWasSet<'_> {
ConfigWasSet(self) ConfigWasSet(self)
} }
#[allow(unreachable_pub)]
pub fn was_set_cli(&self) -> CliConfigWasSet<'_> {
CliConfigWasSet(self)
}
fn fill_from_parsed_config(mut self, parsed: PartialConfig, dir: &Path) -> Config { fn fill_from_parsed_config(mut self, parsed: PartialConfig, dir: &Path) -> Config {
$( $(
if let Some(option_value) = parsed.$i { if let Some(option_value) = parsed.$i {

View File

@ -201,7 +201,7 @@ pub(crate) fn all() -> FileLines {
} }
/// Returns `true` if this `FileLines` contains all lines in all files. /// Returns `true` if this `FileLines` contains all lines in all files.
pub(crate) fn is_all(&self) -> bool { pub fn is_all(&self) -> bool {
self.0.is_none() self.0.is_none()
} }

View File

@ -741,6 +741,20 @@ fn test_unstable_from_toml() {
assert_eq!(config.unstable_features(), true); assert_eq!(config.unstable_features(), true);
} }
#[test]
fn test_set_cli() {
let mut config = Config::default();
assert_eq!(config.was_set().edition(), false);
assert_eq!(config.was_set_cli().edition(), false);
config.set().edition(Edition::Edition2021);
assert_eq!(config.was_set().edition(), false);
assert_eq!(config.was_set_cli().edition(), false);
config.set_cli().edition(Edition::Edition2021);
assert_eq!(config.was_set().edition(), false);
assert_eq!(config.was_set_cli().edition(), true);
assert_eq!(config.was_set_cli().emit_mode(), false);
}
#[cfg(test)] #[cfg(test)]
mod deprecated_option_merge_imports { mod deprecated_option_merge_imports {
use super::*; use super::*;