| 1 | //! Rust library to easily compare version numbers with no specific format, and test against various comparison operators. |
| 2 | //! |
| 3 | //! Comparing version numbers is hard, especially with weird version number formats. |
| 4 | //! |
| 5 | //! This library helps you to easily compare any kind of version number with no |
| 6 | //! specific format using a best-effort approach. |
| 7 | //! Two version numbers can be compared to each other to get a comparison operator |
| 8 | //! (`<`, `==`, `>`), or test them against a comparison operator. |
| 9 | //! |
| 10 | //! Along with version comparison, the library provides various other tools for |
| 11 | //! working with version numbers. |
| 12 | //! |
| 13 | //! Inspired by PHPs [version_compare()](http://php.net/manual/en/function.version-compare.php). |
| 14 | //! |
| 15 | //! ### Formats |
| 16 | //! |
| 17 | //! Version numbers that would parse successfully include: |
| 18 | //! `1`, `3.10.4.1`, `1.2.alpha`, `1.2.dev.4`, ` `, ` . -32 . 1`, `MyApp 3.2.0 / build 0932` ... |
| 19 | //! |
| 20 | //! See a list of how version numbers compare [here](https://github.com/timvisee/version-compare/blob/411ed7135741ed7cf2fcf4919012fb5412dc122b/src/test.rs#L50-L103). |
| 21 | //! |
| 22 | //! ## Examples |
| 23 | //! |
| 24 | //! [example.rs](examples/example.rs): |
| 25 | //! ```rust |
| 26 | //! use version_compare::{compare, compare_to, Cmp, Version}; |
| 27 | //! |
| 28 | //! let a = "1.2" ; |
| 29 | //! let b = "1.5.1" ; |
| 30 | //! |
| 31 | //! // The following comparison operators are used: |
| 32 | //! // - Cmp::Eq -> Equal |
| 33 | //! // - Cmp::Ne -> Not equal |
| 34 | //! // - Cmp::Lt -> Less than |
| 35 | //! // - Cmp::Le -> Less than or equal |
| 36 | //! // - Cmp::Ge -> Greater than or equal |
| 37 | //! // - Cmp::Gt -> Greater than |
| 38 | //! |
| 39 | //! // Easily compare version strings |
| 40 | //! assert_eq!(compare(a, b), Ok(Cmp::Lt)); |
| 41 | //! assert_eq!(compare_to(a, b, Cmp::Le), Ok(true)); |
| 42 | //! assert_eq!(compare_to(a, b, Cmp::Gt), Ok(false)); |
| 43 | //! |
| 44 | //! // Parse and wrap version strings as a Version |
| 45 | //! let a = Version::from(a).unwrap(); |
| 46 | //! let b = Version::from(b).unwrap(); |
| 47 | //! |
| 48 | //! // The Version can easily be compared with |
| 49 | //! assert_eq!(a < b, true); |
| 50 | //! assert_eq!(a <= b, true); |
| 51 | //! assert_eq!(a > b, false); |
| 52 | //! assert_eq!(a != b, true); |
| 53 | //! assert_eq!(a.compare(&b), Cmp::Lt); |
| 54 | //! assert_eq!(a.compare_to(&b, Cmp::Lt), true); |
| 55 | //! |
| 56 | //! // Or match the comparison operators |
| 57 | //! match a.compare(b) { |
| 58 | //! Cmp::Lt => println!("Version a is less than b" ), |
| 59 | //! Cmp::Eq => println!("Version a is equal to b" ), |
| 60 | //! Cmp::Gt => println!("Version a is greater than b" ), |
| 61 | //! _ => unreachable!(), |
| 62 | //! } |
| 63 | //! ``` |
| 64 | //! |
| 65 | //! See the [`examples`](https://github.com/timvisee/version-compare/tree/master/examples) directory for more. |
| 66 | //! |
| 67 | //! ## Features |
| 68 | //! |
| 69 | //! * Compare version numbers, get: `<`, `==`, `>` |
| 70 | //! * Compare against a comparison operator |
| 71 | //! (`<`, `<=`, `==`, `!=`, `>=`, `>`) |
| 72 | //! * Parse complex and unspecified formats |
| 73 | //! * Static, standalone methods to easily compare version strings in a single line |
| 74 | //! of code |
| 75 | //! |
| 76 | //! ### Semver |
| 77 | //! |
| 78 | //! Version numbers using the [semver](http://semver.org/) format are compared |
| 79 | //! correctly with no additional configuration. |
| 80 | //! |
| 81 | //! If your version number strings follow this exact format you may be better off |
| 82 | //! using the [`semver`](https://crates.io/crates/semver) crate for more format |
| 83 | //! specific features. |
| 84 | //! |
| 85 | //! If that isn't certain however, `version-compare` makes comparing a breeze. |
| 86 | //! |
| 87 | //! _[View complete README](https://github.com/timvisee/version-compare/blob/master/README.md)_ |
| 88 | |
| 89 | mod cmp; |
| 90 | mod compare; |
| 91 | mod manifest; |
| 92 | mod part; |
| 93 | mod version; |
| 94 | |
| 95 | #[cfg (test)] |
| 96 | mod test; |
| 97 | |
| 98 | // Re-exports |
| 99 | pub use crate::cmp::Cmp; |
| 100 | pub use crate::compare::{compare, compare_to}; |
| 101 | pub use crate::manifest::Manifest; |
| 102 | pub use crate::part::Part; |
| 103 | pub use crate::version::Version; |
| 104 | |