use super::{Tag, WireType};
use vint64::VInt64;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Header {
pub tag: Tag,
pub critical: bool,
pub wire_type: WireType,
}
impl Header {
pub fn new(tag: Tag, critical: bool, wire_type: WireType) -> Self {
Header {
tag,
critical,
wire_type,
}
}
pub fn encode(self) -> VInt64 {
vint64::encode(self.into())
}
pub fn encoded_len(self) -> usize {
vint64::encoded_len(self.into())
}
}
impl From<u64> for Header {
fn from(encoded: u64) -> Self {
Header {
tag: encoded >> 4,
critical: encoded >> 3 & 1 == 1,
wire_type: WireType::from_unmasked(encoded),
}
}
}
impl From<Header> for u64 {
fn from(header: Header) -> u64 {
header.tag << 4 | (header.critical as u64) << 3 | header.wire_type as u64
}
}