use crate::{
frame::{GetFrameType, io::WriteFrameType},
varint::{VarInt, WriteVarInt, be_varint},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RetireConnectionIdFrame {
sequence: VarInt,
}
impl super::GetFrameType for RetireConnectionIdFrame {
fn frame_type(&self) -> super::FrameType {
super::FrameType::RetireConnectionId
}
}
impl super::EncodeSize for RetireConnectionIdFrame {
fn max_encoding_size(&self) -> usize {
1 + 8
}
fn encoding_size(&self) -> usize {
1 + self.sequence.encoding_size()
}
}
impl RetireConnectionIdFrame {
pub fn new(sequence: VarInt) -> Self {
Self { sequence }
}
pub fn sequence(&self) -> u64 {
self.sequence.into_inner()
}
}
pub fn be_retire_connection_id_frame(input: &[u8]) -> nom::IResult<&[u8], RetireConnectionIdFrame> {
use nom::{Parser, combinator::map};
map(be_varint, RetireConnectionIdFrame::new).parse(input)
}
impl<T: bytes::BufMut> super::io::WriteFrame<RetireConnectionIdFrame> for T {
fn put_frame(&mut self, frame: &RetireConnectionIdFrame) {
self.put_frame_type(frame.frame_type());
self.put_varint(&frame.sequence);
}
}
#[cfg(test)]
mod tests {
use super::{RetireConnectionIdFrame, be_retire_connection_id_frame};
use crate::{
frame::{
EncodeSize, FrameType, GetFrameType,
io::{WriteFrame, WriteFrameType},
},
varint::VarInt,
};
#[test]
fn test_retire_connection_id_frame() {
let frame = RetireConnectionIdFrame::new(VarInt::from_u32(0x1234));
assert_eq!(frame.frame_type(), FrameType::RetireConnectionId);
assert_eq!(frame.max_encoding_size(), 1 + 8);
assert_eq!(frame.encoding_size(), 1 + 2);
assert_eq!(frame.sequence(), 0x1234);
}
#[test]
fn test_read_retire_connection_id_frame() {
let buf = vec![0x52, 0x34];
let (remain, frame) = be_retire_connection_id_frame(&buf).unwrap();
assert!(remain.is_empty());
assert_eq!(
frame,
RetireConnectionIdFrame::new(VarInt::from_u32(0x1234))
);
}
#[test]
fn test_write_retire_connection_id_frame() {
let mut buf = Vec::new();
let frame = RetireConnectionIdFrame::new(VarInt::from_u32(0x1234));
buf.put_frame(&frame);
let mut expected = Vec::new();
expected.put_frame_type(FrameType::RetireConnectionId);
expected.extend_from_slice(&[0x52, 0x34]);
assert_eq!(buf, expected);
}
}