AirLibrary/Updates/VersionCompare.rs
1#![allow(unused_variables, dead_code, unused_imports)]
2
3//! Semantic version comparison utility.
4//!
5//! `CompareVersions` parses two `"major.minor.patch"` strings and returns
6//! `-1`, `0`, or `1` following the same contract as C's `strcmp` so callers
7//! can use `match` on the result. Non-numeric segments are silently dropped.
8
9/// Compare two semver strings.
10/// Returns `1` if `v1 > v2`, `-1` if `v1 < v2`, `0` if equal.
11pub fn CompareVersions(v1:&str, v2:&str) -> i32 {
12 let v1_parts:Vec<u32> = v1.split('.').filter_map(|s| s.parse().ok()).collect();
13 let v2_parts:Vec<u32> = v2.split('.').filter_map(|s| s.parse().ok()).collect();
14
15 for (i, part) in v1_parts.iter().enumerate() {
16 if i >= v2_parts.len() {
17 return 1;
18 }
19 match part.cmp(&v2_parts[i]) {
20 std::cmp::Ordering::Greater => return 1,
21 std::cmp::Ordering::Less => return -1,
22 std::cmp::Ordering::Equal => continue,
23 }
24 }
25
26 if v1_parts.len() < v2_parts.len() { -1 } else { 0 }
27}