use super::*;
use bytes::{BufMut, BytesMut};
use std::cmp::Ordering;
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Version {
major: u16,
minor: u16,
}
impl Version {
pub fn new(major: u16, minor: u16) -> Self {
Version { major, minor }
}
pub fn major(self) -> u16 {
self.major
}
pub fn minor(self) -> u16 {
self.minor
}
}
impl Encode for Version {
fn encode(&self, buf: &mut BytesMut) {
buf.put_u16(self.major);
buf.put_u16(self.minor);
}
fn len(&self) -> usize {
4
}
}
impl Ord for Version {
fn cmp(&self, other: &Self) -> Ordering {
let cmp_major = self.major.cmp(&other.major);
if cmp_major != Ordering::Equal {
return cmp_major;
}
self.minor.cmp(&other.minor)
}
}
impl PartialOrd for Version {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}.{}", self.major, self.minor)
}
}
impl Default for Version {
fn default() -> Version {
Version::new(1, 0)
}
}