rust_rcs_core/internet/
header_field.rs

1// Copyright 2023 宋昊文
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use super::parameter::ParameterParser;
16use super::syntax;
17
18pub struct HeaderField<'a> {
19    pub value: &'a [u8],
20    pub parameters: &'a [u8],
21}
22
23impl HeaderField<'_> {
24    pub fn get_parameter_iterator(&self) -> ParameterParser {
25        ParameterParser::new(self.parameters, b';', false)
26    }
27}
28
29pub trait AsHeaderField<'a> {
30    type Target;
31    fn as_header_field(&'a self) -> Self::Target;
32}
33
34impl<'a> AsHeaderField<'a> for [u8] {
35    type Target = HeaderField<'a>;
36    fn as_header_field(&'a self) -> HeaderField {
37        if let Some(idx) = syntax::index_with_token_escaping(self, b';') {
38            // let parameters = parameter::parse_parameters(self, b';');
39            HeaderField {
40                value: &self[..idx],
41                parameters: &self[idx + 1..],
42            }
43        } else {
44            HeaderField {
45                value: self,
46                parameters: &[],
47            }
48        }
49    }
50}
51
52impl<'a> AsHeaderField<'a> for Vec<u8> {
53    type Target = HeaderField<'a>;
54    fn as_header_field(&'a self) -> HeaderField {
55        <[u8]>::as_header_field(self)
56    }
57}