summaryrefslogtreecommitdiff
path: root/src/channel/header.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/channel/header.rs')
-rw-r--r--src/channel/header.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/channel/header.rs b/src/channel/header.rs
new file mode 100644
index 0000000..eda8214
--- /dev/null
+++ b/src/channel/header.rs
@@ -0,0 +1,34 @@
+use axum::http::{HeaderName, HeaderValue};
+
+pub struct LastEventId(pub String);
+
+static LAST_EVENT_ID: HeaderName = HeaderName::from_static("last-event-id");
+
+impl headers::Header for LastEventId {
+ fn name() -> &'static HeaderName {
+ &LAST_EVENT_ID
+ }
+
+ fn decode<'i, I>(values: &mut I) -> Result<Self, headers::Error>
+ where
+ I: Iterator<Item = &'i HeaderValue>,
+ {
+ let value = values.next().ok_or_else(headers::Error::invalid)?;
+ if let Ok(value) = value.to_str() {
+ Ok(Self(value.into()))
+ } else {
+ Err(headers::Error::invalid())
+ }
+ }
+
+ fn encode<E>(&self, values: &mut E)
+ where
+ E: Extend<HeaderValue>,
+ {
+ let Self(value) = self;
+ // Must panic or suppress; the trait provides no other options.
+ let value = HeaderValue::from_str(value).expect("LastEventId is a valid header value");
+
+ values.extend(std::iter::once(value));
+ }
+}