use super::*;
use crate::{cid::ConnectionId, packet::SpinBit};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct OneRttHeader {
spin: SpinBit,
dcid: ConnectionId,
}
impl OneRttHeader {
pub fn new(spin: SpinBit, dcid: ConnectionId) -> Self {
Self { spin, dcid }
}
}
impl EncodeHeader for OneRttHeader {
fn size(&self) -> usize {
1 + self.dcid.len()
}
}
impl GetType for OneRttHeader {
fn get_type(&self) -> Type {
Type::Short(OneRtt(self.spin))
}
}
impl super::GetDcid for OneRttHeader {
fn get_dcid(&self) -> &ConnectionId {
&self.dcid
}
}
pub mod io {
use bytes::BufMut;
use super::{GetType, OneRttHeader};
use crate::packet::{header::io::WriteHeader, r#type::io::WritePacketType, signal::SpinBit};
pub fn be_one_rtt_header(
spin: SpinBit,
dcid_len: usize,
input: &[u8],
) -> nom::IResult<&[u8], OneRttHeader> {
use nom::bytes::streaming::take;
let (remain, dcid) = take(dcid_len)(input)?;
let dcid = crate::cid::ConnectionId::from_slice(dcid);
Ok((remain, OneRttHeader { spin, dcid }))
}
impl<T: BufMut> WriteHeader<OneRttHeader> for T {
fn put_header(&mut self, header: &OneRttHeader) {
let ty = header.get_type();
self.put_packet_type(&ty);
self.put_slice(&header.dcid);
}
}
}
#[cfg(test)]
mod tests {
use crate::packet::header::io::WriteHeader;
#[test]
fn test_read_one_rtt_header() {
use super::io::be_one_rtt_header;
use crate::packet::{header::ConnectionId, SpinBit};
let (remain, header) = be_one_rtt_header(SpinBit::One, 0, &[][..]).unwrap();
assert_eq!(remain.len(), 0);
assert_eq!(header.spin, SpinBit::One);
assert_eq!(header.dcid, ConnectionId::default());
}
#[test]
fn test_write_one_rtt_header() {
use super::OneRttHeader;
use crate::{cid::ConnectionId, packet::SpinBit};
let mut buf = vec![];
let header = OneRttHeader {
spin: SpinBit::One,
dcid: ConnectionId::default(),
};
buf.put_header(&header);
assert_eq!(buf, [0x60]);
}
}