Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
akorchyn committed Jan 13, 2025
1 parent 1379da7 commit f5bae23
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/config/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,13 @@ pub enum ConfigVersion {
#[serde(rename = "3")]
V3(ConfigV3),
}

impl ConfigVersion {
pub fn is_latest_version(&self) -> bool {
// Used match instead of matches! to compile fail if new version is added
match self {
ConfigVersion::V3(_) => true,
ConfigVersion::V2(_) | ConfigVersion::V1(_) => false,
}
}
}
17 changes: 8 additions & 9 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,12 @@ impl Config {
}
})?;

let config = match config_version {
migrations::ConfigVersion::V3(config) => config,
_ => {
eprintln!("Migrating config.toml to the latest version...");
let config: Config = config_version.into();
Self::write_config_toml(config.clone())?;
config
}
};
let is_latest_version = config_version.is_latest_version();
let config: Config = config_version.into();

if !is_latest_version {
Self::write_config_toml(config.clone())?;
}

Ok(config)
} else {
Expand Down Expand Up @@ -200,9 +197,11 @@ impl From<migrations::ConfigVersion> for Config {
loop {
config_version = match config_version {
migrations::ConfigVersion::V1(config_v1) => {
eprintln!("Migrating config.toml from V1 to V2...");
migrations::ConfigVersion::V2(config_v1.into())
}
migrations::ConfigVersion::V2(config_v2) => {
eprintln!("Migrating config.toml from V2 to V3...");
migrations::ConfigVersion::V3(config_v2.into())
}
migrations::ConfigVersion::V3(config_v3) => {
Expand Down

0 comments on commit f5bae23

Please sign in to comment.