aboutsummaryrefslogtreecommitdiff
path: root/src/headermap.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/headermap.rs')
-rw-r--r--src/headermap.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/headermap.rs b/src/headermap.rs
new file mode 100644
index 0000000..8712df1
--- /dev/null
+++ b/src/headermap.rs
@@ -0,0 +1,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(())
+ }
+}