use crate::error::CodecError;
use crate::varint::VarInt;
use bytes::{Buf, BufMut};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubgroupHeader {
pub header_type: u8,
pub track_alias: VarInt,
pub group_id: VarInt,
pub subgroup_id: VarInt,
pub publisher_priority: Option<u8>,
}
impl SubgroupHeader {
pub fn has_extensions(&self) -> bool {
self.header_type & 0x01 != 0
}
pub fn has_end_of_group(&self) -> bool {
self.header_type & 0x02 != 0
}
pub fn has_explicit_subgroup_id(&self) -> bool {
self.header_type & 0x04 != 0
}
pub fn has_priority(&self) -> bool {
self.header_type & 0x20 == 0
}
pub fn encode(&self, buf: &mut impl BufMut) {
VarInt::from_usize(self.header_type as usize).encode(buf);
self.track_alias.encode(buf);
self.group_id.encode(buf);
if self.has_explicit_subgroup_id() {
self.subgroup_id.encode(buf);
}
if let Some(p) = self.publisher_priority {
buf.put_u8(p);
}
}
pub fn decode(buf: &mut impl Buf) -> Result<Self, CodecError> {
let header_type = VarInt::decode(buf)?.into_inner() as u8;
let base = header_type & 0xD0; if base != 0x10 && base != 0x30 {
return Err(CodecError::InvalidField);
}
let track_alias = VarInt::decode(buf)?;
let group_id = VarInt::decode(buf)?;
let subgroup_id =
if header_type & 0x04 != 0 { VarInt::decode(buf)? } else { VarInt::from_usize(0) };
let publisher_priority = if header_type & 0x20 == 0 {
if buf.remaining() < 1 {
return Err(CodecError::UnexpectedEnd);
}
Some(buf.get_u8())
} else {
None
};
Ok(Self { header_type, track_alias, group_id, subgroup_id, publisher_priority })
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubgroupObject {
pub object_id: VarInt,
pub extension_headers: Vec<u8>,
pub payload_length: VarInt,
pub object_status: Option<VarInt>,
pub payload: Vec<u8>,
}
#[derive(Debug, Clone)]
pub struct SubgroupObjectReader {
extensions_present: bool,
prev_object_id: Option<u64>,
}
impl SubgroupObjectReader {
pub fn new(header: &SubgroupHeader) -> Self {
Self { extensions_present: header.has_extensions(), prev_object_id: None }
}
pub fn read_object(&mut self, buf: &mut impl Buf) -> Result<SubgroupObject, CodecError> {
let delta = VarInt::decode(buf)?.into_inner();
let object_id_val = match self.prev_object_id {
None => delta,
Some(prev) => {
if self.extensions_present {
prev.checked_add(delta).ok_or(CodecError::InvalidField)?
} else {
prev.checked_add(1)
.and_then(|v| v.checked_add(delta))
.ok_or(CodecError::InvalidField)?
}
}
};
self.prev_object_id = Some(object_id_val);
let object_id = VarInt::from_u64(object_id_val).map_err(|_| CodecError::InvalidField)?;
let extension_headers = if self.extensions_present {
let ext_len = VarInt::decode(buf)?.into_inner() as usize;
crate::types::read_bytes(buf, ext_len)?
} else {
Vec::new()
};
let payload_length_vi = VarInt::decode(buf)?;
let payload_length_val = payload_length_vi.into_inner() as usize;
let (object_status, payload) = if payload_length_val == 0 {
let status = VarInt::decode(buf)?;
(Some(status), Vec::new())
} else {
let payload = crate::types::read_bytes(buf, payload_length_val)?;
(None, payload)
};
Ok(SubgroupObject {
object_id,
extension_headers,
payload_length: payload_length_vi,
object_status,
payload,
})
}
pub fn write_object(
&mut self,
object: &SubgroupObject,
buf: &mut impl BufMut,
) -> Result<(), CodecError> {
let oid = object.object_id.into_inner();
let delta = match self.prev_object_id {
None => oid,
Some(prev) => oid.checked_sub(prev).ok_or(CodecError::InvalidField)?,
};
VarInt::from_u64(delta).map_err(|_| CodecError::InvalidField)?.encode(buf);
if self.extensions_present {
let ext_len = object.extension_headers.len();
VarInt::from_usize(ext_len).encode(buf);
buf.put_slice(&object.extension_headers);
}
object.payload_length.encode(buf);
if object.payload_length.into_inner() == 0 {
if let Some(s) = &object.object_status {
s.encode(buf);
} else {
VarInt::from_u64(0).unwrap().encode(buf);
}
} else {
buf.put_slice(&object.payload);
}
self.prev_object_id = Some(oid);
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DatagramHeader {
pub datagram_type: u8,
pub track_alias: VarInt,
pub group_id: VarInt,
pub object_id: VarInt,
pub publisher_priority: u8,
pub extension_headers: Vec<u8>,
pub object_status: Option<VarInt>,
}
impl DatagramHeader {
pub fn has_object_id(&self) -> bool {
self.datagram_type & 0x04 == 0
}
pub fn is_end_of_group(&self) -> bool {
self.datagram_type & 0x02 != 0
}
pub fn is_status(&self) -> bool {
self.datagram_type & 0x20 != 0
}
pub fn has_extensions(&self) -> bool {
self.datagram_type & 0x01 != 0
}
pub fn encode(&self, buf: &mut impl BufMut) {
VarInt::from_usize(self.datagram_type as usize).encode(buf);
self.track_alias.encode(buf);
self.group_id.encode(buf);
if self.has_object_id() {
self.object_id.encode(buf);
}
buf.put_u8(self.publisher_priority);
if self.has_extensions() {
VarInt::from_usize(self.extension_headers.len()).encode(buf);
buf.put_slice(&self.extension_headers);
}
if self.is_status() {
if let Some(s) = &self.object_status {
s.encode(buf);
}
}
}
pub fn decode(buf: &mut impl Buf) -> Result<Self, CodecError> {
let datagram_type = VarInt::decode(buf)?.into_inner() as u8;
let track_alias = VarInt::decode(buf)?;
let group_id = VarInt::decode(buf)?;
let object_id =
if datagram_type & 0x04 == 0 { VarInt::decode(buf)? } else { VarInt::from_usize(0) };
if buf.remaining() < 1 {
return Err(CodecError::UnexpectedEnd);
}
let publisher_priority = buf.get_u8();
let extension_headers = if datagram_type & 0x01 != 0 {
let ext_len = VarInt::decode(buf)?.into_inner() as usize;
crate::types::read_bytes(buf, ext_len)?
} else {
Vec::new()
};
let object_status =
if datagram_type & 0x20 != 0 { Some(VarInt::decode(buf)?) } else { None };
Ok(Self {
datagram_type,
track_alias,
group_id,
object_id,
publisher_priority,
extension_headers,
object_status,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FetchHeader {
pub request_id: VarInt,
}
impl FetchHeader {
pub fn encode(&self, buf: &mut impl BufMut) {
VarInt::from_usize(0x05).encode(buf);
self.request_id.encode(buf);
}
pub fn decode(buf: &mut impl Buf) -> Result<Self, CodecError> {
let stream_type = VarInt::decode(buf)?.into_inner();
if stream_type != 0x05 {
return Err(CodecError::InvalidField);
}
let request_id = VarInt::decode(buf)?;
Ok(Self { request_id })
}
}