use super::{chunk_header::*, chunk_type::*, *};
use bytes::{Buf, BufMut, Bytes, BytesMut};
use std::fmt;
#[derive(Default, Debug, Clone, PartialEq)]
pub(crate) struct ChunkIForwardTsn {
pub(crate) new_cumulative_tsn: u32,
pub(crate) streams: Vec<ChunkIForwardTsnStream>,
}
#[derive(Default, Debug, Clone, PartialEq)]
pub(crate) struct ChunkIForwardTsnStream {
pub(crate) identifier: u16,
pub(crate) unordered: bool,
pub(crate) mid: u32,
}
pub(crate) const I_FORWARD_TSN_STREAM_LENGTH: usize = 8;
impl fmt::Display for ChunkIForwardTsn {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut res = vec![self.header().to_string()];
res.push(format!("New Cumulative TSN: {}", self.new_cumulative_tsn));
for s in &self.streams {
res.push(format!(
" - si={}, {}mid={}",
s.identifier,
if s.unordered { "unordered " } else { "" },
s.mid
));
}
write!(f, "{}", res.join("\n"))
}
}
impl Chunk for ChunkIForwardTsn {
fn header(&self) -> ChunkHeader {
ChunkHeader {
typ: CT_I_FORWARD_TSN,
flags: 0,
value_length: self.value_length() as u16,
}
}
fn unmarshal(buf: &Bytes) -> Result<Self> {
let header = ChunkHeader::unmarshal(buf)?;
if header.typ != CT_I_FORWARD_TSN {
return Err(Error::ErrChunkTypeNotForwardTsn);
}
if header.value_length() < 4
|| !(header.value_length() - 4).is_multiple_of(I_FORWARD_TSN_STREAM_LENGTH)
{
return Err(Error::ErrChunkTooShort);
}
let reader = &mut buf.slice(CHUNK_HEADER_SIZE..CHUNK_HEADER_SIZE + header.value_length());
let new_cumulative_tsn = reader.get_u32();
let mut streams = Vec::with_capacity(reader.remaining() / I_FORWARD_TSN_STREAM_LENGTH);
while reader.remaining() >= I_FORWARD_TSN_STREAM_LENGTH {
let identifier = reader.get_u16();
let unordered = reader.get_u16() & 1 != 0;
let mid = reader.get_u32();
streams.push(ChunkIForwardTsnStream {
identifier,
unordered,
mid,
});
}
Ok(ChunkIForwardTsn {
new_cumulative_tsn,
streams,
})
}
fn marshal_to(&self, writer: &mut BytesMut) -> Result<usize> {
self.header().marshal_to(writer)?;
writer.put_u32(self.new_cumulative_tsn);
for s in &self.streams {
writer.put_u16(s.identifier);
writer.put_u16(u16::from(s.unordered));
writer.put_u32(s.mid);
}
Ok(writer.len())
}
fn check(&self) -> Result<()> {
Ok(())
}
fn value_length(&self) -> usize {
4 + I_FORWARD_TSN_STREAM_LENGTH * self.streams.len()
}
fn as_any(&self) -> &dyn Any {
self
}
}