aboutsummaryrefslogtreecommitdiff
path: root/src/headermap.rs
blob: 8712df1f9b809b881c47319076931e9e78c07b3d (plain)
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
use crate::headers::Header;
use anyhow::Result;
use std::fmt::Display;

pub struct HeaderMap(pub Vec<(String, String)>);

impl HeaderMap {
    pub fn new() -> Self {
        Self(vec![])
    }
    pub fn add<H: Header>(mut self, h: H) -> Self {
        self.0.push((H::NAME.to_string(), format!("{h}")));
        self
    }
    pub fn get<H: Header>(&self) -> Option<Result<H>> {
        self.0
            .iter()
            .find(|(k, _)| k == H::NAME)
            .map(|(_, v)| H::from_str(v))
    }
}

impl Display for HeaderMap {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for (k, v) in &self.0 {
            write!(f, "{k}: {v}\r\n")?;
        }
        Ok(())
    }
}