use core::fmt;
use std::io;
const MAJOR_VERSION: &str = env!("CARGO_PKG_VERSION_MAJOR");
const MINOR_VERSION: &str = env!("CARGO_PKG_VERSION_MINOR");
const PATCH_VERSION: &str = env!("CARGO_PKG_VERSION_PATCH");
pub fn get_major_version() -> u8 {
MAJOR_VERSION.parse().unwrap_or_default()
}
pub fn get_minor_version() -> u8 {
MINOR_VERSION.parse().unwrap_or_default()
}
pub fn get_patch_version() -> u8 {
PATCH_VERSION.parse().unwrap_or_default()
}
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct Version {
major: u8,
minor: u8,
patch: u8,
}
impl Version {
pub fn new(major: u8, minor: u8, patch: u8) -> Self {
Version {
major,
minor,
patch,
}
}
pub fn major(&self) -> u8 {
self.major
}
pub fn minor(&self) -> u8 {
self.minor
}
pub fn patch(&self) -> u8 {
self.patch
}
pub fn from_bytes(bytes: &[u8]) -> io::Result<Self> {
if bytes.len() < 3 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Invalid version bytes.",
));
}
Ok(Version {
major: bytes[0],
minor: bytes[1],
patch: bytes[2],
})
}
pub fn to_bytes(&self) -> [u8; 3] {
[self.major, self.minor, self.patch]
}
}
impl Default for Version {
fn default() -> Self {
Version::new(
get_major_version(),
get_minor_version(),
get_patch_version(),
)
}
}
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
}
}