From 53d5ccd5e965086f7d43d43a1eabdb7064780cb5 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Thu, 1 Aug 2024 18:42:45 -0500 Subject: [PATCH] refactor: use style edition when loading from partial config --- src/config/config_type.rs | 2 +- src/config/mod.rs | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/config/config_type.rs b/src/config/config_type.rs index 91efb71744b7..4b83e974932f 100644 --- a/src/config/config_type.rs +++ b/src/config/config_type.rs @@ -210,7 +210,7 @@ macro_rules! create_config { )+ #[allow(unreachable_pub)] - pub fn default_with_style_edition(style_edition: StyleEdition) -> Config { + pub(super) fn default_with_style_edition(style_edition: StyleEdition) -> Config { Config { $( $i: ( diff --git a/src/config/mod.rs b/src/config/mod.rs index 5d6047c385d6..6462c62a3580 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -217,9 +217,37 @@ impl PartialConfig { ::toml::to_string(&cloned).map_err(ToTomlError) } + + pub(super) fn to_parsed_config( + self, + style_edition_override: Option, + edition_override: Option, + dir: &Path, + ) -> Config { + Config::default_for_possible_style_edition( + style_edition_override.or(self.style_edition), + edition_override.or(self.edition), + ) + .fill_from_parsed_config(self, dir) + } } impl Config { + pub fn default_for_possible_style_edition( + style_edition: Option, + edition: Option, + ) -> Config { + style_edition.map_or_else( + || { + edition.map_or_else( + || Config::default(), + |e| Self::default_with_style_edition(e.into()), + ) + }, + |se| Self::default_with_style_edition(se), + ) + } + pub(crate) fn version_meets_requirement(&self) -> bool { if self.was_set().required_version() { let version = env!("CARGO_PKG_VERSION"); @@ -324,12 +352,13 @@ impl Config { err.push_str(msg) } } - match parsed.try_into() { + + match parsed.try_into::() { Ok(parsed_config) => { if !err.is_empty() { eprint!("{err}"); } - Ok(Config::default().fill_from_parsed_config(parsed_config, dir)) + Ok(parsed_config.to_parsed_config(None, None, dir)) } Err(e) => { err.push_str("Error: Decoding config file failed:\n");