use std::{
clone::Clone,
cmp::Ordering,
hash::{Hash, Hasher},
};
#[derive(Clone, Debug, Eq)]
pub struct ApplicationVersion {
pub app: String,
pub major: u32,
pub minor: u32,
pub patch: u32,
}
impl Ord for ApplicationVersion {
fn cmp(&self, other: &ApplicationVersion) -> Ordering {
self.major
.cmp(&other.major) .then_with(
|| self.minor.cmp(&other.minor), )
.then_with(
|| self.patch.cmp(&other.patch), )
}
}
impl PartialOrd for ApplicationVersion {
fn partial_cmp(&self, other: &ApplicationVersion) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for ApplicationVersion {
fn eq(&self, other: &ApplicationVersion) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl Hash for ApplicationVersion {
fn hash<H: Hasher>(&self, state: &mut H) {
self.app.hash(state);
self.major.hash(state);
self.minor.hash(state);
self.patch.hash(state);
}
}
impl ApplicationVersion {
pub fn before(&self, other: &ApplicationVersion) -> bool {
if self.app != other.app {
return false;
}
self.cmp(other) == Ordering::Less
}
}
#[test]
fn test_version() {
let v1 = ApplicationVersion {
app: String::from("hello"),
major: 1,
minor: 7,
patch: 15,
};
let v2 = ApplicationVersion {
app: String::from("hello"),
major: 1,
minor: 7,
patch: 15,
};
let v3 = ApplicationVersion {
app: String::from("hello"),
major: 1,
minor: 7,
patch: 17,
};
let v4 = ApplicationVersion {
app: String::from("hello2"),
major: 1,
minor: 7,
patch: 17,
};
assert!(v1 == v2);
assert!(v1 < v3 && v2 < v3);
assert!(v1.before(&v3) && v2.before(&v3));
assert!(!v1.before(&v4) && !v2.before(&v4) && !v3.before(&v4));
}