Skip to main content

backhopper_core/
versions.rs

1// Copyright (C) 2026 Michael S. Klishin and Contributors
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// See LICENSE-APACHE and LICENSE-MIT for details.
4
5//! Version-like tag string comparison.
6
7use std::cmp::Ordering;
8
9/// Compares two version-like tag strings in *descending* order: numerically
10/// newer comes first. Pairs with `min_by`/`max_by` callers that want the
11/// "latest" or "oldest" tag.
12#[must_use]
13pub fn version_cmp(a: &str, b: &str) -> Ordering {
14    let a_parts = parse_version(a);
15    let b_parts = parse_version(b);
16    a_parts.cmp(&b_parts).reverse()
17}
18
19fn parse_version(tag: &str) -> Vec<u64> {
20    let stripped = tag.strip_prefix('v').unwrap_or(tag);
21    stripped
22        .split(['.', '-', '+', '_'])
23        .filter_map(|p| p.parse::<u64>().ok())
24        .collect()
25}