rtsp_types/headers/
allow.rs

1// Copyright (C) 2020 Sebastian Dröge <sebastian@centricular.com>
2//
3// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
4
5use super::*;
6use crate::Method;
7
8/// `Allow` header ([RFC 7826 section 18.6](https://tools.ietf.org/html/rfc7826#section-18.6)).
9#[derive(Debug, Clone)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct Allow(Vec<Method>);
12
13impl std::ops::Deref for Allow {
14    type Target = Vec<Method>;
15
16    fn deref(&self) -> &Self::Target {
17        &self.0
18    }
19}
20
21impl std::ops::DerefMut for Allow {
22    fn deref_mut(&mut self) -> &mut Self::Target {
23        &mut self.0
24    }
25}
26
27impl AsRef<Vec<Method>> for Allow {
28    fn as_ref(&self) -> &Vec<Method> {
29        &self.0
30    }
31}
32
33impl AsMut<Vec<Method>> for Allow {
34    fn as_mut(&mut self) -> &mut Vec<Method> {
35        &mut self.0
36    }
37}
38
39impl From<Vec<Method>> for Allow {
40    fn from(v: Vec<Method>) -> Self {
41        Allow(v)
42    }
43}
44
45impl<'a> From<&'a [Method]> for Allow {
46    fn from(v: &'a [Method]) -> Self {
47        Allow(v.to_vec())
48    }
49}
50
51impl Allow {
52    /// Creates a new `Allow` header builder.
53    pub fn builder() -> AllowBuilder {
54        AllowBuilder(Vec::new())
55    }
56}
57
58/// Builder for the 'Allow' header.
59#[derive(Debug, Clone)]
60pub struct AllowBuilder(Vec<Method>);
61
62impl AllowBuilder {
63    /// Add the provided method to the `Allow` header.
64    pub fn method(mut self, method: Method) -> Self {
65        self.0.push(method);
66        self
67    }
68
69    /// Build the `Allow` header.
70    pub fn build(self) -> Allow {
71        Allow(self.0)
72    }
73}
74
75impl super::TypedHeader for Allow {
76    fn from_headers(headers: impl AsRef<Headers>) -> Result<Option<Self>, HeaderParseError> {
77        let headers = headers.as_ref();
78
79        let header = match headers.get(&ALLOW) {
80            None => return Ok(None),
81            Some(header) => header,
82        };
83
84        let mut allow = Vec::new();
85        for method in header.as_str().split(',') {
86            let method = method.trim();
87
88            allow.push(method.into());
89        }
90
91        Ok(Some(Allow(allow)))
92    }
93
94    fn insert_into(&self, mut headers: impl AsMut<Headers>) {
95        let headers = headers.as_mut();
96
97        let mut allow = String::new();
98        for method in &self.0 {
99            if !allow.is_empty() {
100                allow.push_str(", ");
101            }
102
103            allow.push_str(method.into());
104        }
105
106        headers.insert(ALLOW, allow);
107    }
108}
109
110impl super::TypedAppendableHeader for Allow {
111    fn append_to(&self, mut headers: impl AsMut<Headers>) {
112        let headers = headers.as_mut();
113
114        let mut allow = String::new();
115        for method in &self.0 {
116            if !allow.is_empty() {
117                allow.push_str(", ");
118            }
119
120            allow.push_str(method.into());
121        }
122
123        headers.append(ALLOW, allow);
124    }
125}