1 //! Usage examples of the version-compare library.
2 //!
3 //! This file shows various ways this library supports for comparing version numbers,
4 //! and it shows various ways of implementing it in code logic such as with a `match` statement.
5 //!
6 //! The `assert_eq!(...)` macros are used to assert the returned value by a given statement.
7 //!
8 //! You can run this example file by using the command `cargo run --example example`.
9 
10 extern crate version_compare;
11 
12 use version_compare::{CompOp, Version, VersionCompare};
13 
main()14 fn main() {
15     // Define some version numbers
16     let a = "1.2";
17     let b = "1.5.1";
18 
19     // The following comparison operators are used:
20     // - CompOp::Eq -> Equal
21     // - CompOp::Ne -> Not equal
22     // - CompOp::Lt -> Less than
23     // - CompOp::Le -> Less than or equal
24     // - CompOp::Ge -> Greater than or equal
25     // - CompOp::Gt -> Greater than
26 
27     // Easily compare version strings
28     assert_eq!(VersionCompare::compare(&a, &b).unwrap(), CompOp::Lt);
29     assert_eq!(
30         VersionCompare::compare_to(&a, &b, &CompOp::Le).unwrap(),
31         true
32     );
33     assert_eq!(
34         VersionCompare::compare_to(&a, &b, &CompOp::Gt).unwrap(),
35         false
36     );
37 
38     // Version string parsing
39     let a_ver = Version::from(a).unwrap();
40     let b_ver = Version::from(b).unwrap();
41 
42     // Directly compare parsed versions
43     assert_eq!(a_ver < b_ver, true);
44     assert_eq!(a_ver <= b_ver, true);
45     assert_eq!(a_ver > b_ver, false);
46     assert_eq!(a_ver != b_ver, true);
47     assert_eq!(a_ver.compare(&b_ver), CompOp::Lt);
48     assert_eq!(b_ver.compare(&a_ver), CompOp::Gt);
49     assert_eq!(a_ver.compare_to(&b_ver, &CompOp::Lt), true);
50 
51     // Match
52     match a_ver.compare(&b_ver) {
53         CompOp::Lt => println!("Version a is less than b"),
54         CompOp::Eq => println!("Version a is equal to b"),
55         CompOp::Gt => println!("Version a is greater than b"),
56         _ => unreachable!(),
57     }
58 }
59