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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::cmp::min;
use std::fmt;
use std::io;
#[repr(C)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VersionInfo {
major: u16,
minor: u16,
patch: u16,
reserved: u16,
version_hash: [u8; 8],
}
impl fmt::Display for VersionInfo {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}.{}.{}", self.major, self.minor, self.patch)?;
if u64::from_ne_bytes(self.version_hash) != 0 {
write!(
fmt,
"-{}",
std::str::from_utf8(&self.version_hash).unwrap_or("INVALID")
)?;
}
Ok(())
}
}
impl VersionInfo {
pub fn new(major: u16, minor: u16, patch: u16, version_hash: [u8; 8]) -> VersionInfo {
VersionInfo {
major,
minor,
patch,
reserved: 0x8000,
version_hash,
}
}
pub fn compatible_with(&self, other: &VersionInfo) -> bool {
if !(self.valid() || other.valid()) {
return false;
}
if self.major == other.major && self.minor == other.minor && self.patch == other.patch {
if self.version_hash == [0u8; 8] {
true
} else {
self.version_hash == other.version_hash
}
} else {
false
}
}
pub fn write_to<W: WriteBytesExt>(&self, w: &mut W) -> io::Result<()> {
w.write_u16::<LittleEndian>(self.major)?;
w.write_u16::<LittleEndian>(self.minor)?;
w.write_u16::<LittleEndian>(self.patch)?;
w.write_u16::<LittleEndian>(self.reserved)?;
w.write(&self.version_hash).and_then(|written| {
if written != self.version_hash.len() {
Err(io::Error::new(
io::ErrorKind::Other,
"unable to write full version hash",
))
} else {
Ok(())
}
})
}
pub fn read_from<R: ReadBytesExt>(r: &mut R) -> io::Result<Self> {
let mut version_hash = [0u8; 8];
Ok(VersionInfo {
major: r.read_u16::<LittleEndian>()?,
minor: r.read_u16::<LittleEndian>()?,
patch: r.read_u16::<LittleEndian>()?,
reserved: r.read_u16::<LittleEndian>()?,
version_hash: {
r.read_exact(&mut version_hash)?;
version_hash
},
})
}
pub fn valid(&self) -> bool {
self.reserved == 0x8000
}
pub fn current(current_hash: &'static [u8]) -> Self {
let mut version_hash = [0u8; 8];
for i in 0..min(version_hash.len(), current_hash.len()) {
version_hash[i] = current_hash[i];
}
VersionInfo::new(
env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap(),
env!("CARGO_PKG_VERSION_MINOR").parse().unwrap(),
env!("CARGO_PKG_VERSION_PATCH").parse().unwrap(),
version_hash,
)
}
}