srt_protocol/options/
srt_version.rs1use std::{cmp::Ordering, fmt};
2
3#[derive(PartialEq, Eq, Clone, Copy)]
6pub struct SrtVersion {
7 pub major: u8,
8 pub minor: u8,
9 pub patch: u8,
10}
11
12impl SrtVersion {
13 pub const CURRENT: SrtVersion = SrtVersion {
14 major: 1,
15 minor: 3,
16 patch: 1,
17 };
18
19 pub fn new(major: u8, minor: u8, patch: u8) -> SrtVersion {
21 SrtVersion {
22 major,
23 minor,
24 patch,
25 }
26 }
27
28 pub fn parse(from: u32) -> SrtVersion {
30 let [_, major, minor, patch] = from.to_be_bytes();
31 SrtVersion {
32 major,
33 minor,
34 patch,
35 }
36 }
37
38 pub fn to_u32(self) -> u32 {
40 u32::from(self.major) * 0x10000 + u32::from(self.minor) * 0x100 + u32::from(self.patch)
41 }
42}
43
44impl PartialOrd for SrtVersion {
45 fn partial_cmp(&self, other: &SrtVersion) -> Option<Ordering> {
46 Some(self.cmp(other))
47 }
48}
49
50impl Ord for SrtVersion {
51 fn cmp(&self, other: &SrtVersion) -> Ordering {
52 match self.major.cmp(&other.major) {
53 Ordering::Equal => match self.minor.cmp(&other.minor) {
54 Ordering::Equal => self.patch.cmp(&other.patch),
55 o => o,
56 },
57 o => o,
58 }
59 }
60}
61
62impl fmt::Display for SrtVersion {
63 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64 write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
65 }
66}
67
68impl fmt::Debug for SrtVersion {
69 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70 write!(f, "{self}")
71 }
72}
73
74#[cfg(test)]
75mod test {
76 use super::SrtVersion;
77 #[test]
78 fn test_parse() {
79 assert_eq!(SrtVersion::parse(0x01_01_01), SrtVersion::new(1, 1, 1));
80 assert_eq!(SrtVersion::parse(0x00_00_00), SrtVersion::new(0, 0, 0));
81 }
82
83 #[test]
84 fn test_display_debug() {
85 assert_eq!(format!("{}", SrtVersion::new(12, 12, 12)), "12.12.12");
86 assert_eq!(format!("{:?}", SrtVersion::new(12, 12, 12)), "12.12.12");
87 }
88}