pub const MIN_COMPATIBLE_VERSION: (u64, u64, u64) = (0, 2, 0);
pub const CURRENT_VERSION: (u64, u64, u64) = (0, 2, 2);
pub fn is_compatible(peer_version: (u64, u64, u64)) -> bool {
peer_version.0 == CURRENT_VERSION.0 && peer_version.1 >= MIN_COMPATIBLE_VERSION.1
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct VersionHandshake {
pub version: (u64, u64, u64),
pub min_compatible: (u64, u64, u64),
pub build_id: String,
}
impl VersionHandshake {
pub fn current() -> Self {
Self {
version: CURRENT_VERSION,
min_compatible: MIN_COMPATIBLE_VERSION,
build_id: env!("CARGO_PKG_VERSION").to_string(),
}
}
pub fn is_compatible_with(&self, other: &VersionHandshake) -> bool {
is_compatible(other.version)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_current_version_compatible_with_itself() {
assert!(is_compatible(CURRENT_VERSION));
}
#[test]
fn test_older_minor_version_compatible() {
let old_minor = (0u64, 2u64, 0u64);
assert!(is_compatible(old_minor));
let mid_minor = (0u64, 2u64, 1u64);
assert!(is_compatible(mid_minor));
}
#[test]
fn test_different_major_version_incompatible() {
let v1 = (1u64, 0u64, 0u64);
assert!(!is_compatible(v1));
let v2 = (2u64, 2u64, 0u64);
assert!(!is_compatible(v2));
}
#[test]
fn test_minor_below_minimum_incompatible() {
let too_old = (0u64, 1u64, 99u64);
assert!(!is_compatible(too_old));
}
#[test]
fn test_version_handshake_serialization() {
let hs = VersionHandshake::current();
let json = serde_json::to_string(&hs).expect("serialise");
let back: VersionHandshake = serde_json::from_str(&json).expect("deserialise");
assert_eq!(hs, back);
}
#[test]
fn test_version_handshake_incompatible_major() {
let current = VersionHandshake::current();
let other = VersionHandshake {
version: (1u64, 0u64, 0u64),
min_compatible: (1u64, 0u64, 0u64),
build_id: "old-major".to_string(),
};
assert!(!current.is_compatible_with(&other));
}
#[test]
fn test_version_handshake_compatible_peer() {
let current = VersionHandshake::current();
let peer = VersionHandshake {
version: (0u64, 2u64, 1u64),
min_compatible: MIN_COMPATIBLE_VERSION,
build_id: "peer".to_string(),
};
assert!(current.is_compatible_with(&peer));
}
}