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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use std::{cmp::Ordering, fmt};
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct SrtVersion {
pub major: u8,
pub minor: u8,
pub patch: u8,
}
impl SrtVersion {
pub const CURRENT: SrtVersion = SrtVersion {
major: 1,
minor: 3,
patch: 1,
};
pub fn new(major: u8, minor: u8, patch: u8) -> SrtVersion {
SrtVersion {
major,
minor,
patch,
}
}
pub fn parse(from: u32) -> SrtVersion {
SrtVersion {
major: (from / 0x10000) as u8,
minor: ((from / 0x100) % 0x100) as u8,
patch: (from % 0x100) as u8,
}
}
pub fn to_u32(self) -> u32 {
u32::from(self.major) * 0x10000 + u32::from(self.minor) * 0x100 + u32::from(self.patch)
}
}
impl PartialOrd for SrtVersion {
fn partial_cmp(&self, other: &SrtVersion) -> Option<Ordering> {
Some(match self.major.cmp(&other.major) {
Ordering::Equal => match self.minor.cmp(&other.minor) {
Ordering::Equal => self.patch.cmp(&other.patch),
o => o,
},
o => o,
})
}
}
impl Ord for SrtVersion {
fn cmp(&self, other: &SrtVersion) -> Ordering {
self.partial_cmp(other).unwrap()
}
}
impl fmt::Display for SrtVersion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
}
}
impl fmt::Debug for SrtVersion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self)
}
}
#[cfg(test)]
mod test {
use super::SrtVersion;
#[test]
fn test_parse() {
assert_eq!(SrtVersion::parse(0x01_01_01), SrtVersion::new(1, 1, 1));
assert_eq!(SrtVersion::parse(0x00_00_00), SrtVersion::new(0, 0, 0));
}
#[test]
fn test_display_debug() {
assert_eq!(format!("{}", SrtVersion::new(12, 12, 12)), "12.12.12");
assert_eq!(format!("{:?}", SrtVersion::new(12, 12, 12)), "12.12.12");
}
}