use crate::frame::*;
#[derive(Debug, Eq, PartialEq)]
pub struct Priority {
stream_id: StreamId,
dependency: StreamDependency,
}
#[derive(Debug, Eq, PartialEq)]
pub struct StreamDependency {
dependency_id: StreamId,
weight: u8,
is_exclusive: bool,
}
impl Priority {
pub fn load(head: Head, payload: &[u8]) -> Result<Self, Error> {
let dependency = StreamDependency::load(payload)?;
if dependency.dependency_id() == head.stream_id() {
return Err(Error::InvalidDependencyId);
}
Ok(Priority {
stream_id: head.stream_id(),
dependency,
})
}
}
impl<B> From<Priority> for Frame<B> {
fn from(src: Priority) -> Self {
Frame::Priority(src)
}
}
impl StreamDependency {
pub fn new(dependency_id: StreamId, weight: u8, is_exclusive: bool) -> Self {
StreamDependency {
dependency_id,
weight,
is_exclusive,
}
}
pub fn load(src: &[u8]) -> Result<Self, Error> {
if src.len() != 5 {
return Err(Error::InvalidPayloadLength);
}
let (dependency_id, is_exclusive) = StreamId::parse(&src[..4]);
let weight = src[4];
Ok(StreamDependency::new(dependency_id, weight, is_exclusive))
}
pub fn dependency_id(&self) -> StreamId {
self.dependency_id
}
}