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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::hash::Hash;
use std::{borrow::Cow, fmt};

use crate::{Helper, WebError, WebResult, Buf, BufMut, MarkBuf};

#[derive(Clone, Debug)]
pub enum HeaderValue {
    Stand(&'static str),
    Value(Vec<u8>),
}

impl HeaderValue {
    pub fn from_static(s: &'static str) -> HeaderValue {
        HeaderValue::Stand(s)
    }

    pub fn from_bytes(b: &[u8]) -> HeaderValue {
        HeaderValue::Value(b.to_vec())
    }

    pub fn from_cow(b: Cow<[u8]>) -> HeaderValue {
        HeaderValue::Value(Vec::from(b.to_owned()))
    }

    pub fn bytes_len(&self) -> usize {
        match self {
            Self::Stand(s) => s.as_bytes().len(),
            Self::Value(s) => s.len(),
        }
    }

    pub fn as_bytes(&self) -> &[u8] {
        match self {
            Self::Stand(s) => &s.as_bytes(),
            Self::Value(s) => &s,
        }
    }

    pub fn as_string(&self) -> Option<String> {
        match self {
            Self::Stand(s) => Some(s.to_string()),
            Self::Value(s) => String::from_utf8(s.clone()).map_or(None, |s| Some(s)),
        }
    }

    pub fn encode<B: Buf+BufMut+MarkBuf>(&self, buffer: &mut B) -> WebResult<usize> {
        match self {
            Self::Stand(name) => Ok(buffer.put_slice(name.as_bytes())),
            Self::Value(vec) => Ok(buffer.put_slice(&**vec)),
        }
    }
}

impl Hash for HeaderValue {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        match self {
            HeaderValue::Stand(stand) => {
                (*stand.as_bytes()).hash(state);
            }
            HeaderValue::Value(val) => {
                val.hash(state);
            }
        }
    }
}

impl fmt::Display for HeaderValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut f = f.debug_struct("HeaderValue");
        match &self {
            Self::Stand(value) => f.field("value", value),
            Self::Value(value) => f.field("value", &String::from_utf8_lossy(value)),
        };
        f.finish()
    }
}

impl TryInto<usize> for &HeaderValue {
    type Error = WebError;

    fn try_into(self) -> Result<usize, WebError> {
        match self {
            HeaderValue::Stand(s) => s.parse().map_err(WebError::from),
            HeaderValue::Value(v) => {
                let mut result = 0usize;
                for b in v {
                    if !Helper::is_digit(*b) {
                        return Err(WebError::IntoError);
                    }
                    match result.overflowing_mul(10) {
                        (u, false) => {
                            result = u + (b - Helper::DIGIT_0) as usize;
                        }
                        (_u, true) => return Err(WebError::IntoError),
                    }
                }
                Ok(result)
            }
        }
    }
}

impl TryInto<String> for &HeaderValue {
    type Error = WebError;

    fn try_into(self) -> Result<String, WebError> {
        match self {
            HeaderValue::Stand(s) => Ok(s.to_string()),
            HeaderValue::Value(v) => Ok(String::from_utf8_lossy(v).to_string()),
        }
    }
}

impl TryFrom<&'static str> for HeaderValue {
    type Error = WebError;

    fn try_from(value: &'static str) -> Result<Self, Self::Error> {
        Ok(HeaderValue::Stand(value))
    }
}

impl TryFrom<String> for HeaderValue {
    type Error = WebError;
    fn try_from(value: String) -> Result<Self, Self::Error> {
        Ok(HeaderValue::Value(value.into_bytes()))
    }
}

impl Eq for HeaderValue {}

impl PartialEq<HeaderValue> for HeaderValue {
    fn eq(&self, other: &HeaderValue) -> bool {
        match (self, other) {
            (Self::Stand(l0), Self::Stand(r0)) => l0 == r0,
            (Self::Value(l0), Self::Value(r0)) => l0 == r0,
            (Self::Stand(l0), Self::Value(r0)) => l0.as_bytes() == r0,
            (Self::Value(l0), Self::Stand(r0)) => l0 == r0.as_bytes(),
        }
    }
}

impl PartialEq<str> for HeaderValue {
    fn eq(&self, other: &str) -> bool {
        match self {
            HeaderValue::Stand(s) => s == &other,
            HeaderValue::Value(s) => &s[..] == other.as_bytes(),
        }
    }
}

impl PartialEq<HeaderValue> for [u8] {
    fn eq(&self, other: &HeaderValue) -> bool {
        other == self
    }
}

impl PartialEq<[u8]> for HeaderValue {
    fn eq(&self, other: &[u8]) -> bool {
        match self {
            HeaderValue::Stand(s) => s.as_bytes() == other,
            HeaderValue::Value(s) => &s[..] == other,
        }
    }
}

impl PartialEq<HeaderValue> for str {
    fn eq(&self, url: &HeaderValue) -> bool {
        url == self
    }
}