-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathheader.rs
More file actions
54 lines (45 loc) · 1.46 KB
/
header.rs
File metadata and controls
54 lines (45 loc) · 1.46 KB
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
use crate::headers::{HeaderName, HeaderValue, Headers};
/// A trait representing a [`HeaderName`] and [`HeaderValue`] pair.
pub trait Header {
/// Access the header's name.
fn header_name(&self) -> HeaderName;
/// Access the header's value.
fn header_value(&self) -> HeaderValue;
/// Insert the header name and header value into something that looks like a
/// [`Headers`] map.
fn apply_header<H: AsMut<Headers>>(&self, mut headers: H) {
let name = self.header_name();
let value = self.header_value();
headers.as_mut().insert(name, value);
}
}
impl Header for (&'static str, &'static str) {
fn header_name(&self) -> HeaderName {
if self.0.chars().all(|c| c.is_ascii_lowercase()) {
HeaderName::from_lowercase_str(self.0)
} else {
HeaderName::from(self.0)
}
}
fn header_value(&self) -> HeaderValue {
HeaderValue::from_static_str(self.1)
}
}
impl Header for (String, String) {
fn header_name(&self) -> HeaderName {
self.0.parse().expect("Header name should be valid ASCII")
}
fn header_value(&self) -> HeaderValue {
self.1.parse().expect("Header value should be valid ASCII")
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn header_from_strings() {
let strings = ("Content-Length", "12");
assert_eq!(strings.header_name(), "Content-Length");
assert_eq!(strings.header_value(), "12");
}
}