From b203875e60d96ff5cd97886f4040a5320e0e7648 Mon Sep 17 00:00:00 2001 From: GeckoEidechse <40122905+GeckoEidechse@users.noreply.github.com> Date: Wed, 15 Nov 2023 18:00:29 +0100 Subject: fix: Proper release-candidate number conversion (#669) * refactor: Return early if not a release-candidate * test: Add test case for double digit rc number * feat: Implement proper rc number conversion * test: Remove the leading v from version number They don't appear in the actual version numbers either. * test: Add test for double digit rc and patch num --- src-tauri/src/util.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) (limited to 'src-tauri/src') diff --git a/src-tauri/src/util.rs b/src-tauri/src/util.rs index 9ac8580b..15511d21 100644 --- a/src-tauri/src/util.rs +++ b/src-tauri/src/util.rs @@ -229,10 +229,41 @@ pub fn move_dir_all( /// Helps with converting release candidate numbers which are different on Thunderstore /// due to restrictions imposed by the platform pub fn convert_release_candidate_number(version_number: String) -> String { - // This simply converts `-rc` to `0` - // Works as intended for RCs < 10, e.g. `v1.9.2-rc1` -> `v1.9.201` - // Doesn't work for larger numbers, e.g. `v1.9.2-rc11` -> `v1.9.2011` (should be `v1.9.211`) - version_number.replace("-rc", "0").replace(".00", ".") + let release_candidate_suffix = "-rc"; + + if !version_number.contains(release_candidate_suffix) { + // Not an release-candidate version number, nothing to do, return early + return version_number; + } + + // Version number is guaranteed to contain `-rc` + let re = regex::Regex::new(r"(\d+)\.(\d+)\.(\d+)-rc(\d+)").unwrap(); + if let Some(captures) = re.captures(&version_number) { + // Extract versions + let major_version: u32 = captures[1].parse().unwrap(); + let minor_version: u32 = captures[2].parse().unwrap(); + let patch_version: u32 = captures[3].parse().unwrap(); + let release_candidate: u32 = captures[4].parse().unwrap(); + + // Zero pad + let padded_release_candidate = format!("{:02}", release_candidate); + + // Combine + let combined_patch_version = format!("{}{}", patch_version, padded_release_candidate); + + // Strip leading zeroes + let trimmed_combined_patch_version = combined_patch_version.trim_start_matches('0'); + + // Combine all + let version_number = format!( + "{}.{}.{}", + major_version, minor_version, trimmed_combined_patch_version + ); + return version_number; + } + + // We should never end up here + panic!(); } #[cfg(test)] @@ -274,4 +305,22 @@ mod tests { assert_eq!(output, expected_output); } + + #[test] + fn test_double_digit_release_candidate_number_conversion() { + let input = "1.2.3-rc45".to_string(); + let output = convert_release_candidate_number(input); + let expected_output = "1.2.345"; + + assert_eq!(output, expected_output); + } + + #[test] + fn test_double_digit_patch_and_rc_number_conversion() { + let input = "1.2.34-rc56".to_string(); + let output = convert_release_candidate_number(input); + let expected_output = "1.2.3456"; + + assert_eq!(output, expected_output); + } } -- cgit v1.2.3