use crate::coding::{DecodeError, EncodeError, VarInt};
use crate::coding::{AsyncRead, AsyncWrite};
use std::ops::Deref;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Version(VarInt);
impl Version {
pub const DRAFT_00: Version = Version(VarInt::from_u32(0xff00));
pub const KIXEL_00: Version = Version(VarInt::from_u32(0xbad00));
}
impl From<VarInt> for Version {
fn from(v: VarInt) -> Self {
Self(v)
}
}
impl From<Version> for VarInt {
fn from(v: Version) -> Self {
v.0
}
}
impl Version {
pub async fn decode<R: AsyncRead>(r: &mut R) -> Result<Self, DecodeError> {
let v = VarInt::decode(r).await?;
Ok(Self(v))
}
pub async fn encode<W: AsyncWrite>(&self, w: &mut W) -> Result<(), EncodeError> {
self.0.encode(w).await?;
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Versions(Vec<Version>);
impl Versions {
pub async fn decode<R: AsyncRead>(r: &mut R) -> Result<Self, DecodeError> {
let count = VarInt::decode(r).await?.into_inner();
let mut vs = Vec::new();
for _ in 0..count {
let v = Version::decode(r).await?;
vs.push(v);
}
Ok(Self(vs))
}
pub async fn encode<W: AsyncWrite>(&self, w: &mut W) -> Result<(), EncodeError> {
let size: VarInt = self.0.len().try_into()?;
size.encode(w).await?;
for v in &self.0 {
v.encode(w).await?;
}
Ok(())
}
}
impl Deref for Versions {
type Target = Vec<Version>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<Vec<Version>> for Versions {
fn from(vs: Vec<Version>) -> Self {
Self(vs)
}
}