diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2024-09-13 22:30:02 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2024-09-13 23:12:31 -0400 |
| commit | 407ca8df6284ce1a4c649b018c7326fd195bbd26 (patch) | |
| tree | 876091c17efbd765a4c7ef339548c0ff4dfb96d5 /src/channel/header.rs | |
| parent | 388a3d5a925aef7ff39339454ae0d720e05f038e (diff) | |
Support Last-Event-Id as a method of resuming channel events after a disconnect
Diffstat (limited to 'src/channel/header.rs')
| -rw-r--r-- | src/channel/header.rs | 34 |
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)); + } +} |
