http_box/http2/frame_format.rs
1// +-----------------------------------------------------------------------------------------------+
2// | Copyright 2016 Sean Kerr |
3// | |
4// | Licensed under the Apache License, Version 2.0 (the "License"); |
5// | you may not use this file except in compliance with the License. |
6// | You may obtain a copy of the License at |
7// | |
8// | http://www.apache.org/licenses/LICENSE-2.0 |
9// | |
10// | Unless required by applicable law or agreed to in writing, software |
11// | distributed under the License is distributed on an "AS IS" BASIS, |
12// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13// | See the License for the specific language governing permissions and |
14// | limitations under the License. |
15// +-----------------------------------------------------------------------------------------------+
16
17use std::fmt;
18
19use http2::flags::Flags;
20use http2::frame_type::FrameType;
21
22/// Frame format.
23#[derive(Clone,Copy,PartialEq)]
24pub struct FrameFormat {
25 flags: u8,
26 payload_length_frame_type: u32,
27 stream_id: u32
28}
29
30impl FrameFormat {
31 /// Create a new `FrameFormat`.
32 pub fn new(&mut self, payload_length: u32, frame_type: u8, flags: u8, stream_id: u32)
33 -> FrameFormat {
34 FrameFormat{
35 flags: flags,
36 payload_length_frame_type: (payload_length << 8) | frame_type as u32,
37 stream_id: stream_id
38 }
39 }
40
41 /// Retrieve the frame flags.
42 pub fn flags(&self) -> Flags {
43 Flags::from_u8(self.flags)
44 }
45
46 /// Format this for debug and display purposes.
47 fn format(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
48 write!(
49 formatter,
50 "<FrameFormat: flags: {}, frame_type: {}, payload_length: {}, stream_id: {}>",
51 self.flags(),
52 self.frame_type(),
53 self.payload_length(),
54 self.stream_id
55 )
56 }
57
58 /// Retrieve the frame type.
59 pub fn frame_type(&self) -> FrameType {
60 FrameType::from_u8((self.payload_length_frame_type & 0xFF) as u8)
61 }
62
63 /// Retrieve the payload length.
64 pub fn payload_length(&self) -> u32 {
65 self.payload_length_frame_type >> 8
66 }
67
68 /// Retrieve the stream identifier.
69 pub fn stream_id(&self) -> u32 {
70 self.stream_id
71 }
72}
73
74impl fmt::Debug for FrameFormat {
75 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
76 self.format(formatter)
77 }
78}
79
80impl fmt::Display for FrameFormat {
81 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
82 self.format(formatter)
83 }
84}