#version #compare #comparing

version-compare

Rust 库,用于轻松比较不带特定格式的版本号,并测试各种比较运算符

16 个版本

0.2.0 2024 年 3 月 16 日
0.1.1 2022 年 11 月 10 日
0.1.0 2021 年 10 月 14 日
0.0.11 2020 年 8 月 26 日
0.0.4 2017 年 3 月 17 日

解析器实现 中排名 26

Download history 142436/week @ 2024-04-09 144743/week @ 2024-04-16 142877/week @ 2024-04-23 155068/week @ 2024-04-30 156261/week @ 2024-05-07 147478/week @ 2024-05-14 144136/week @ 2024-05-21 147143/week @ 2024-05-28 139616/week @ 2024-06-04 143959/week @ 2024-06-11 141073/week @ 2024-06-18 147637/week @ 2024-06-25 153842/week @ 2024-07-02 154810/week @ 2024-07-09 149038/week @ 2024-07-16 136009/week @ 2024-07-23

每月下载量 621,524
1,699 个crate(60个直接)中使用

MIT 许可证 MIT

60KB
884 行(不包括注释)

Build status on GitLab CI Crate version Documentation Download statistics Coverage status Dependencies License

Rust 库:version-compare

Rust 库,用于轻松比较不带特定格式的版本号,并测试各种比较运算符。

比较版本号很困难,尤其是当版本号格式奇特时。

该库可以帮助您使用最佳努力方法轻松地比较任何类型的版本号(无特定格式)。可以将两个版本号进行比较以获取比较运算符(<==>),或测试它们是否满足比较运算符。

除了版本比较外,该库还提供各种其他用于处理版本号的工具。

受 PHP 的 version_compare() 启发。

注意:仍在开发中。当前可配置性非常有限。事情将会改变。

格式

可以成功解析的版本号包括
13.10.4.11.2.alpha1.2.dev.4 . -32 . 1MyApp 3.2.0 / build 0932 ...

有关版本号比较的列表,请参阅此处

示例

该库非常易于使用。以下是一个基本用法示例

Cargo.toml:

[dependencies]
version-compare = "0.1"

example.rs:

use version_compare::{compare, compare_to, Cmp, Version};

fn main() {
    let a = "1.2";
    let b = "1.5.1";

    // The following comparison operators are used:
    // - Cmp::Eq -> Equal
    // - Cmp::Ne -> Not equal
    // - Cmp::Lt -> Less than
    // - Cmp::Le -> Less than or equal
    // - Cmp::Ge -> Greater than or equal
    // - Cmp::Gt -> Greater than

    // Easily compare version strings
    assert_eq!(compare(a, b), Ok(Cmp::Lt));
    assert_eq!(compare_to(a, b, Cmp::Le), Ok(true));
    assert_eq!(compare_to(a, b, Cmp::Gt), Ok(false));

    // Parse and wrap version strings as a Version
    let a = Version::from(a).unwrap();
    let b = Version::from(b).unwrap();

    // The Version can easily be compared with
    assert_eq!(a < b, true);
    assert_eq!(a <= b, true);
    assert_eq!(a > b, false);
    assert_eq!(a != b, true);
    assert_eq!(a.compare(&b), Cmp::Lt);
    assert_eq!(a.compare_to(&b, Cmp::Lt), true);

    // Or match the comparison operators
    match a.compare(b) {
        Cmp::Lt => println!("Version a is less than b"),
        Cmp::Eq => println!("Version a is equal to b"),
        Cmp::Gt => println!("Version a is greater than b"),
        _ => unreachable!(),
    }
}

有关更多信息,请参阅examples 目录。

功能

  • 比较版本号,获取:<==>
  • 与比较运算符(<<===!=>=>)进行比较
  • 解析复杂和不指定的格式
  • 静态、独立的方法,可以轻松地在单行代码中比较版本字符串

未来想法

  • 版本范围
  • 支持npm样式运算符(例如^1.0~1.0
  • 清单:扩展Manifest以支持广泛的约束
  • 构建块,用于构建您自己的特定版本号解析器
  • 批量比较

semver

使用semver格式的版本号可以正确比较,无需额外配置。

如果您的版本号字符串遵循此精确格式,您可能更倾向于使用semver crate以获取更多格式特定功能。

如果不确定,则version-compare使比较变得轻而易举。

构建

此库每天自动构建和测试,并针对每个提交使用CI服务。

在此处查看当前状态:https://gitlab.com/timvisee/version-compare/-/pipelines

许可证

本项目采用MIT许可证发布。有关更多信息,请查看LICENSE文件。

无运行时依赖