refactor: use style edition when loading from partial config

This commit is contained in:
Caleb Cartwright 2024-08-01 18:42:45 -05:00 committed by Caleb Cartwright
parent 5ee4d3b532
commit 53d5ccd5e9
2 changed files with 32 additions and 3 deletions

View File

@ -210,7 +210,7 @@ pub fn $i(&self) -> <$ty as StyleEditionDefault>::ConfigType {
)+
#[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: (

View File

@ -217,9 +217,37 @@ pub fn to_toml(&self) -> Result<String, ToTomlError> {
::toml::to_string(&cloned).map_err(ToTomlError)
}
pub(super) fn to_parsed_config(
self,
style_edition_override: Option<StyleEdition>,
edition_override: Option<Edition>,
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<StyleEdition>,
edition: Option<Edition>,
) -> 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 @@ pub(crate) fn from_toml(toml: &str, dir: &Path) -> Result<Config, String> {
err.push_str(msg)
}
}
match parsed.try_into() {
match parsed.try_into::<PartialConfig>() {
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");