-
Notifications
You must be signed in to change notification settings - Fork 22
/
vercmp.rs
33 lines (26 loc) · 936 Bytes
/
vercmp.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::cmp::Ordering;
use alpm::{Alpm, SigLevel, Version};
fn main() {
let handle = Alpm::new("/", "tests/db").unwrap();
let core = handle
.register_syncdb("core", SigLevel::USE_DEFAULT)
.unwrap();
// compare two versions
if Version::new("2.2.3") > Version::new("2.0.0") {
println!("2.2.3 is bigger");
}
// compare with a package
let linux = core.pkg("linux").unwrap();
if linux.version() > Version::new("1.0.0") {
println!("linux is bigger with ver {}", linux.version());
}
// compare in a match
match linux.version().vercmp(Version::new("1.0.0")) {
Ordering::Less => println!("less"),
Ordering::Equal => println!("equal"),
Ordering::Greater => println!("greater"),
}
// sorting packages by version
let mut pkgs = core.pkgs().iter().collect::<Vec<_>>();
pkgs.sort_by(|a, b| a.version().vercmp(b.version()));
}