http_box/http2/flags.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
19/// Acknowledgement flag.
20pub const FL_ACK: u8 = 0x1;
21
22/// End headers flag.
23pub const FL_END_HEADERS: u8 = 0x4;
24
25/// End stream flag.
26pub const FL_END_STREAM: u8 = 0x1;
27
28/// Padded flag.
29pub const FL_PADDED: u8 = 0x8;
30
31/// Priority flag.
32pub const FL_PRIORITY: u8 = 0x20;
33
34/// Flags.
35#[derive(Clone,Copy,PartialEq)]
36pub struct Flags {
37 flags: u8
38}
39
40/// Flags.
41impl Flags {
42 /// Create a new `Flags` from a `u8`.
43 pub fn from_u8(byte: u8) -> Flags {
44 Flags {
45 flags: byte
46 }
47 }
48
49 /// Convert this flags to its byte value.
50 pub fn as_byte(&self) -> u8 {
51 self.flags
52 }
53
54 /// Format this for debug and display purposes.
55 fn format(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
56 write!(
57 formatter,
58 "<Flags: ack: {}, end_headers: {}, end_stream: {}, padded: {}, priority: {}>",
59 self.flags & FL_ACK == FL_ACK,
60 self.flags & FL_END_HEADERS == FL_END_HEADERS,
61 self.flags & FL_END_STREAM == FL_END_STREAM,
62 self.flags & FL_PADDED == FL_PADDED,
63 self.flags & FL_PRIORITY == FL_PRIORITY
64 )
65 }
66
67 /// Indicates that the ack flag has been set.
68 pub fn is_ack(&self) -> bool {
69 self.flags & FL_ACK == FL_ACK
70 }
71
72 /// Indicates that the flags are empty.
73 pub fn is_empty(&self) -> bool {
74 self.flags == 0
75 }
76
77 /// Indicates that the end headers flag has been set.
78 pub fn is_end_headers(&self) -> bool {
79 self.flags & FL_END_HEADERS == FL_END_HEADERS
80 }
81
82 /// Indicates that the end stream flag has been set.
83 pub fn is_end_stream(&self) -> bool {
84 self.flags & FL_END_STREAM == FL_END_STREAM
85 }
86
87 /// Indicates that the padded flag has been set.
88 pub fn is_padded(&self) -> bool {
89 self.flags & FL_PADDED == FL_PADDED
90 }
91
92 /// Indicates that the priority flag has been set.
93 pub fn is_priority(&self) -> bool {
94 self.flags & FL_PRIORITY == FL_PRIORITY
95 }
96}
97
98impl fmt::Debug for Flags {
99 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
100 self.format(formatter)
101 }
102}
103
104impl fmt::Display for Flags {
105 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
106 self.format(formatter)
107 }
108}