1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (C) 2020 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use super::*;
use crate::Method;

/// `Allow` header ([RFC 7826 section 18.6](https://tools.ietf.org/html/rfc7826#section-18.6)).
#[derive(Debug, Clone)]
pub struct Allow(Vec<Method>);

impl std::ops::Deref for Allow {
    type Target = Vec<Method>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl std::ops::DerefMut for Allow {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl AsRef<Vec<Method>> for Allow {
    fn as_ref(&self) -> &Vec<Method> {
        &self.0
    }
}

impl AsMut<Vec<Method>> for Allow {
    fn as_mut(&mut self) -> &mut Vec<Method> {
        &mut self.0
    }
}

impl From<Vec<Method>> for Allow {
    fn from(v: Vec<Method>) -> Self {
        Allow(v)
    }
}

impl<'a> From<&'a [Method]> for Allow {
    fn from(v: &'a [Method]) -> Self {
        Allow(v.to_vec())
    }
}

impl Allow {
    /// Creates a new `Allow` header builder.
    pub fn builder() -> AllowBuilder {
        AllowBuilder(Vec::new())
    }
}

/// Builder for the 'Allow' header.
#[derive(Debug, Clone)]
pub struct AllowBuilder(Vec<Method>);

impl AllowBuilder {
    /// Add the provided method to the `Allow` header.
    pub fn method(mut self, method: Method) -> Self {
        self.0.push(method);
        self
    }

    /// Build the `Allow` header.
    pub fn build(self) -> Allow {
        Allow(self.0)
    }
}

impl super::TypedHeader for Allow {
    fn from_headers(headers: impl AsRef<Headers>) -> Result<Option<Self>, HeaderParseError> {
        let headers = headers.as_ref();

        let header = match headers.get(&ALLOW) {
            None => return Ok(None),
            Some(header) => header,
        };

        let mut allow = Vec::new();
        for method in header.as_str().split(',') {
            let method = method.trim();

            allow.push(method.into());
        }

        Ok(Some(Allow(allow)))
    }

    fn insert_into(&self, mut headers: impl AsMut<Headers>) {
        let headers = headers.as_mut();

        let mut allow = String::new();
        for method in &self.0 {
            if !allow.is_empty() {
                allow.push_str(", ");
            }

            allow.push_str(method.into());
        }

        headers.insert(ALLOW, allow);
    }
}

impl super::TypedAppendableHeader for Allow {
    fn append_to(&self, mut headers: impl AsMut<Headers>) {
        let headers = headers.as_mut();

        let mut allow = String::new();
        for method in &self.0 {
            if !allow.is_empty() {
                allow.push_str(", ");
            }

            allow.push_str(method.into());
        }

        headers.append(ALLOW, allow);
    }
}