From b3d46f560affa763044bdeb8d8204b1b65d728d8 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 28 Jan 2026 21:02:36 +0000 Subject: [PATCH] Refactor check_file_version to use robust and efficient regexes - Replace single `VERSION_REGEX` with `VERSION_REGEXES` using `OnceLock>`. - Add fallback regex to support space-separated versions (e.g., `version 1.2.3`). - Prevent regex recompilation by using static initialization, solving the potential performance issue of recompiling fallbacks. - Iterate through regexes to find a match, improving robustness. Co-authored-by: mudcube <101564+mudcube@users.noreply.github.com> --- rust/version-sync/src/main.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/rust/version-sync/src/main.rs b/rust/version-sync/src/main.rs index 944d8b0f..5a4d2de8 100644 --- a/rust/version-sync/src/main.rs +++ b/rust/version-sync/src/main.rs @@ -92,21 +92,27 @@ impl VersionSync { vm_core::error::VmError::Internal(format!("Failed to read {}: {}", path.display(), e)) })?; - static VERSION_REGEX: OnceLock = OnceLock::new(); - let version_regex = VERSION_REGEX.get_or_init(|| { - Regex::new(r#"version\s*[:=]\s*"?([^"\s]+)"?"#).expect("Invalid regex") + static VERSION_REGEXES: OnceLock> = OnceLock::new(); + let version_regexes = VERSION_REGEXES.get_or_init(|| { + vec![ + Regex::new(r#"version\s*[:=]\s*"?([^"\s]+)"?"#).expect("Invalid regex"), + // Fallback for space-separated versions or looser patterns + Regex::new(r#"version.+?([0-9]+\.[0-9]+\.[0-9]+)"#).expect("Invalid regex"), + ] }); - if let Some(captures) = version_regex.captures(&content) { - let current_version = captures.get(1).map(|m| m.as_str()).unwrap_or("unknown"); - if current_version == self.package_version { - Ok(FileVersionStatus::Synced) - } else { - Ok(FileVersionStatus::OutOfSync(current_version.to_string())) + for regex in version_regexes { + if let Some(captures) = regex.captures(&content) { + let current_version = captures.get(1).map(|m| m.as_str()).unwrap_or("unknown"); + if current_version == self.package_version { + return Ok(FileVersionStatus::Synced); + } else { + return Ok(FileVersionStatus::OutOfSync(current_version.to_string())); + } } - } else { - Ok(FileVersionStatus::NoVersion) } + + Ok(FileVersionStatus::NoVersion) } fn update_file_version(&self, path: &Path) -> Result {