use crc::crc16;
use lazy_static::lazy_static;
pub use crate::net::constants::PROTOCOL_VERSION;
lazy_static! {
static ref VERSION_CRC16: u16 = crc16::checksum_x25(PROTOCOL_VERSION.as_bytes());
}
pub struct ProtocolVersion;
impl ProtocolVersion {
#[inline]
#[cfg(test)]
pub fn get_version() -> &'static str {
PROTOCOL_VERSION
}
#[inline]
pub fn get_crc16() -> u16 {
*VERSION_CRC16
}
#[inline]
pub fn valid_version(protocol_version_crc16: u16) -> bool {
protocol_version_crc16 == ProtocolVersion::get_crc16()
}
}
#[cfg(test)]
mod test {
use crate::net::constants::PROTOCOL_VERSION;
use super::*;
#[test]
fn valid_version() {
let protocol_id = crc16::checksum_x25(PROTOCOL_VERSION.as_bytes());
assert!(ProtocolVersion::valid_version(protocol_id));
}
#[test]
fn not_valid_version() {
let protocol_id = crc16::checksum_x25(b"not-laminar");
assert!(!ProtocolVersion::valid_version(protocol_id));
}
#[test]
fn get_crc16() {
assert_eq!(ProtocolVersion::get_crc16(), *VERSION_CRC16);
}
#[test]
fn get_version() {
assert_eq!(ProtocolVersion::get_version(), PROTOCOL_VERSION);
}
}