| 1 | [![Build status on GitLab CI][gitlab-ci-master-badge]][gitlab-ci-link] |
| 2 | [![Crate version][crate-version-badge]][crate-link] |
| 3 | [![Documentation][docs-badge]][docs] |
| 4 | [![Download statistics][crate-download-badge]][crate-link] |
| 5 | [![Coverage status][coverage-badge]][coverage-link] |
| 6 | [![Dependencies][dependency-badge]][crate-link] |
| 7 | [![License][crate-license-badge]][crate-link] |
| 8 | |
| 9 | [coverage-badge]: https://gitlab.com/timvisee/version-compare/badges/master/coverage.svg |
| 10 | [coverage-link]: https://coveralls.io/gitlab/timvisee/version-compare |
| 11 | [crate-download-badge]: https://img.shields.io/crates/d/version-compare.svg |
| 12 | [crate-license-badge]: https://img.shields.io/crates/l/version-compare.svg |
| 13 | [crate-link]: https://crates.io/crates/version-compare |
| 14 | [crate-version-badge]: https://img.shields.io/crates/v/version-compare.svg |
| 15 | [dependency-badge]: https://img.shields.io/badge/dependencies-none!-green.svg |
| 16 | [docs-badge]: https://img.shields.io/docsrs/version-compare |
| 17 | [docs]: https://docs.rs/version-compare |
| 18 | [gitlab-ci-link]: https://gitlab.com/timvisee/version-compare/pipelines |
| 19 | [gitlab-ci-master-badge]: https://gitlab.com/timvisee/version-compare/badges/master/pipeline.svg |
| 20 | |
| 21 | # Rust library: version-compare |
| 22 | |
| 23 | > Rust library to easily compare version numbers with no specific format, and test against various comparison operators. |
| 24 | |
| 25 | Comparing version numbers is hard, especially with weird version number formats. |
| 26 | |
| 27 | This library helps you to easily compare any kind of version number with no |
| 28 | specific format using a best-effort approach. |
| 29 | Two version numbers can be compared to each other to get a comparison operator |
| 30 | (`<`, `==`, `>`), or test them against a comparison operator. |
| 31 | |
| 32 | Along with version comparison, the library provides various other tools for |
| 33 | working with version numbers. |
| 34 | |
| 35 | Inspired by PHPs [version_compare()](http://php.net/manual/en/function.version-compare.php). |
| 36 | |
| 37 | _Note: Still a work in progress. Configurability is currently very limited. Things will change._ |
| 38 | |
| 39 | ### Formats |
| 40 | |
| 41 | Version numbers that would parse successfully include: |
| 42 | `1`, `3.10.4.1`, `1.2.alpha`, `1.2.dev.4`, ` `, ` . -32 . 1`, `MyApp 3.2.0 / build 0932` ... |
| 43 | |
| 44 | See a list of how version numbers compare [here](https://github.com/timvisee/version-compare/blob/411ed7135741ed7cf2fcf4919012fb5412dc122b/src/test.rs#L50-L103). |
| 45 | |
| 46 | ## Example |
| 47 | |
| 48 | This library is very easy to use. Here's a basic usage example: |
| 49 | |
| 50 | `Cargo.toml`: |
| 51 | ```toml |
| 52 | [dependencies] |
| 53 | version-compare = "0.2" |
| 54 | ``` |
| 55 | |
| 56 | [`example.rs`](examples/example.rs): |
| 57 | ```rust |
| 58 | use version_compare::{compare, compare_to, Cmp, Version}; |
| 59 | |
| 60 | fn main() { |
| 61 | let a = "1.2"; |
| 62 | let b = "1.5.1"; |
| 63 | |
| 64 | // The following comparison operators are used: |
| 65 | // - Cmp::Eq -> Equal |
| 66 | // - Cmp::Ne -> Not equal |
| 67 | // - Cmp::Lt -> Less than |
| 68 | // - Cmp::Le -> Less than or equal |
| 69 | // - Cmp::Ge -> Greater than or equal |
| 70 | // - Cmp::Gt -> Greater than |
| 71 | |
| 72 | // Easily compare version strings |
| 73 | assert_eq!(compare(a, b), Ok(Cmp::Lt)); |
| 74 | assert_eq!(compare_to(a, b, Cmp::Le), Ok(true)); |
| 75 | assert_eq!(compare_to(a, b, Cmp::Gt), Ok(false)); |
| 76 | |
| 77 | // Parse and wrap version strings as a Version |
| 78 | let a = Version::from(a).unwrap(); |
| 79 | let b = Version::from(b).unwrap(); |
| 80 | |
| 81 | // The Version can easily be compared with |
| 82 | assert_eq!(a < b, true); |
| 83 | assert_eq!(a <= b, true); |
| 84 | assert_eq!(a > b, false); |
| 85 | assert_eq!(a != b, true); |
| 86 | assert_eq!(a.compare(&b), Cmp::Lt); |
| 87 | assert_eq!(a.compare_to(&b, Cmp::Lt), true); |
| 88 | |
| 89 | // Or match the comparison operators |
| 90 | match a.compare(b) { |
| 91 | Cmp::Lt => println!("Version a is less than b"), |
| 92 | Cmp::Eq => println!("Version a is equal to b"), |
| 93 | Cmp::Gt => println!("Version a is greater than b"), |
| 94 | _ => unreachable!(), |
| 95 | } |
| 96 | } |
| 97 | ``` |
| 98 | |
| 99 | See the [`examples`](examples) directory for more. |
| 100 | |
| 101 | ## Features |
| 102 | |
| 103 | * Compare version numbers, get: `<`, `==`, `>` |
| 104 | * Compare against a comparison operator |
| 105 | (`<`, `<=`, `==`, `!=`, `>=`, `>`) |
| 106 | * Parse complex and unspecified formats |
| 107 | * Static, standalone methods to easily compare version strings in a single line |
| 108 | of code |
| 109 | |
| 110 | #### Future ideas |
| 111 | |
| 112 | * Version ranges |
| 113 | * Support for [npm-style](https://semver.npmjs.com/) operators (e.g. `^1.0` or `~1.0`) |
| 114 | * Manifest: extend `Manifest` for to support a wide set of constraints |
| 115 | * Building blocks for building your own specific version number parser |
| 116 | * Batch comparisons |
| 117 | |
| 118 | #### Semver |
| 119 | |
| 120 | Version numbers using the [semver](http://semver.org/) format are compared |
| 121 | correctly with no additional configuration. |
| 122 | |
| 123 | If your version number strings follow this exact format you may be better off |
| 124 | using the [`semver`](https://crates.io/crates/semver) crate for more format |
| 125 | specific features. |
| 126 | |
| 127 | If that isn't certain however, `version-compare` makes comparing a breeze. |
| 128 | |
| 129 | ## Builds |
| 130 | |
| 131 | This library is automatically build and tested every day and for each commit using CI services. |
| 132 | |
| 133 | See the current status here: https://gitlab.com/timvisee/version-compare/-/pipelines |
| 134 | |
| 135 | ## License |
| 136 | |
| 137 | This project is released under the MIT license. Check out the [LICENSE](LICENSE) file for more information. |