h2/frame/
priority.rs

1use crate::frame::*;
2
3#[derive(Debug, Eq, PartialEq)]
4pub struct Priority {
5    stream_id: StreamId,
6    dependency: StreamDependency,
7}
8
9#[derive(Debug, Eq, PartialEq)]
10pub struct StreamDependency {
11    /// The ID of the stream dependency target
12    dependency_id: StreamId,
13
14    /// The weight for the stream. The value exposed (and set) here is always in
15    /// the range [0, 255], instead of [1, 256] (as defined in section 5.3.2.)
16    /// so that the value fits into a `u8`.
17    weight: u8,
18
19    /// True if the stream dependency is exclusive.
20    is_exclusive: bool,
21}
22
23impl Priority {
24    pub fn load(head: Head, payload: &[u8]) -> Result<Self, Error> {
25        let dependency = StreamDependency::load(payload)?;
26
27        if dependency.dependency_id() == head.stream_id() {
28            return Err(Error::InvalidDependencyId);
29        }
30
31        Ok(Priority {
32            stream_id: head.stream_id(),
33            dependency,
34        })
35    }
36}
37
38impl<B> From<Priority> for Frame<B> {
39    fn from(src: Priority) -> Self {
40        Frame::Priority(src)
41    }
42}
43
44// ===== impl StreamDependency =====
45
46impl StreamDependency {
47    pub fn new(dependency_id: StreamId, weight: u8, is_exclusive: bool) -> Self {
48        StreamDependency {
49            dependency_id,
50            weight,
51            is_exclusive,
52        }
53    }
54
55    pub fn load(src: &[u8]) -> Result<Self, Error> {
56        if src.len() != 5 {
57            return Err(Error::InvalidPayloadLength);
58        }
59
60        // Parse the stream ID and exclusive flag
61        let (dependency_id, is_exclusive) = StreamId::parse(&src[..4]);
62
63        // Read the weight
64        let weight = src[4];
65
66        Ok(StreamDependency::new(dependency_id, weight, is_exclusive))
67    }
68
69    pub fn dependency_id(&self) -> StreamId {
70        self.dependency_id
71    }
72}