Skip to main content

AirLibrary/Updates/
VersionCompare.rs

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