http_box/http2/
setting.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/// Enable push setting.
20pub const S_ENABLE_PUSH: u16 = 0x2;
21
22/// Header table size setting.
23pub const S_HEADER_TABLE_SIZE: u16 = 0x1;
24
25/// Initial window size setting.
26pub const S_INITIAL_WINDOW_SIZE: u16 = 0x4;
27
28/// Maximum concurrent streams setting.
29pub const S_MAX_CONCURRENT_STREAMS: u16 = 0x3;
30
31/// Maximum frame size setting.
32pub const S_MAX_FRAME_SIZE: u16 = 0x5;
33
34/// Maximum header list size setting.
35pub const S_MAX_HEADER_LIST_SIZE: u16 = 0x6;
36
37/// Available settings.
38pub enum Setting {
39    /// Enable push setting.
40    EnablePush(u32),
41
42    /// Header table size setting.
43    HeaderTableSize(u32),
44
45    /// Initial window size setting.
46    InitialWindowSize(u32),
47
48    /// Maximum concurrent streams setting.
49    MaxConcurrentStreams(u32),
50
51    /// Maximum frame size setting.
52    MaxFrameSize(u32),
53
54    /// Maximum header list size setting.
55    MaxHeaderListSize(u32),
56
57    /// Unsupported setting.
58    Unsupported(u32)
59}
60
61impl Setting {
62    /// Create a new `Setting`.
63    pub fn new(&mut self, id: u16, value: u32) -> Setting {
64        match id {
65            S_HEADER_TABLE_SIZE      => Setting::HeaderTableSize(value),
66            S_ENABLE_PUSH            => Setting::EnablePush(value),
67            S_MAX_CONCURRENT_STREAMS => Setting::MaxConcurrentStreams(value),
68            S_INITIAL_WINDOW_SIZE    => Setting::InitialWindowSize(value),
69            S_MAX_FRAME_SIZE         => Setting::MaxFrameSize(value),
70            S_MAX_HEADER_LIST_SIZE   => Setting::MaxHeaderListSize(value),
71            _                        => Setting::Unsupported(value)
72        }
73    }
74
75    /// Format this for debug and display purposes.
76    fn format(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
77        match *self {
78            Setting::EnablePush(x) => {
79                write!(
80                    formatter,
81                    "<Setting::EnablePush: {}>",
82                    x
83                )
84            },
85            Setting::HeaderTableSize(x) => {
86                write!(
87                    formatter,
88                    "<Setting::HeaderTableSize: {}>",
89                    x
90                )
91            },
92            Setting::InitialWindowSize(x) => {
93                write!(
94                    formatter,
95                    "<Setting::InitialWindowSize: {}>",
96                    x
97                )
98            },
99            Setting::MaxConcurrentStreams(x) => {
100                write!(
101                    formatter,
102                    "<Setting::MaxConcurrentStreams: {}>",
103                    x
104                )
105            },
106            Setting::MaxFrameSize(x) => {
107                write!(
108                    formatter,
109                    "<Setting::MaxFrameSize: {}>",
110                    x
111                )
112            },
113            Setting::MaxHeaderListSize(x) => {
114                write!(
115                    formatter,
116                    "<Setting::MaxHeaderListSize: {}>",
117                    x
118                )
119            },
120            Setting::Unsupported(x) => {
121                write!(
122                    formatter,
123                    "<Setting::Unsupported: {}>",
124                    x
125                )
126            }
127        }
128    }
129
130    /// Indicates that this a `Setting::HeadersTableSize`.
131    pub fn is_enable_push(&self) -> bool {
132        match *self {
133            Setting::EnablePush(_) => true,
134            _ => false
135        }
136    }
137
138    /// Indicates that this a `Setting::HeadersTableSize`.
139    pub fn is_header_table_size(&self) -> bool {
140        match *self {
141            Setting::HeaderTableSize(_) => true,
142            _ => false
143        }
144    }
145
146    /// Indicates that this a `Setting::InitialWindowSize`.
147    pub fn is_initial_window_size(&self) -> bool {
148        match *self {
149            Setting::InitialWindowSize(_) => true,
150            _ => false
151        }
152    }
153
154    /// Indicates that this a `Setting::MaxConcurrentStreams`.
155    pub fn is_max_concurrent_streams(&self) -> bool {
156        match *self {
157            Setting::MaxConcurrentStreams(_) => true,
158            _ => false
159        }
160    }
161
162    /// Indicates that this a `Setting::MaxFrameSize`.
163    pub fn is_max_frame_size(&self) -> bool {
164        match *self {
165            Setting::MaxFrameSize(_) => true,
166            _ => false
167        }
168    }
169
170    /// Indicates that this a `Setting::MaxHeaderListSize`.
171    pub fn is_max_header_list_size(&self) -> bool {
172        match *self {
173            Setting::MaxHeaderListSize(_) => true,
174            _ => false
175        }
176    }
177
178    /// Indicates that this a `Setting::Unsupported`.
179    pub fn is_unsupported(&self) -> bool {
180        match *self {
181            Setting::Unsupported(_) => true,
182            _ => false
183        }
184    }
185
186    /// Retrieve the value.
187    pub fn value(&self) -> u32 {
188        match *self {
189              Setting::EnablePush(x)
190            | Setting::HeaderTableSize(x)
191            | Setting::InitialWindowSize(x)
192            | Setting::MaxConcurrentStreams(x)
193            | Setting::MaxFrameSize(x)
194            | Setting::MaxHeaderListSize(x)
195            | Setting::Unsupported(x) => {
196                x
197            }
198        }
199    }
200}
201
202impl fmt::Debug for Setting {
203    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
204        self.format(formatter)
205    }
206}
207
208impl fmt::Display for Setting {
209    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
210        self.format(formatter)
211    }
212}