github_webhooks/axum/headers/
event.rs

1use headers::{Header, HeaderName, HeaderValue};
2
3/// The `X-Github-Event` header.
4pub static X_GITHUB_EVENT: HeaderName = HeaderName::from_static("x-github-event");
5
6/// An axum-style `TypedHeader` for the `X-Github-Event` header.
7/// Example:
8/// ```rs,no_run
9/// fn handle(
10///     TypedHeader(XGithubEvent(event)): TypedHeader<XGithubEvent>,
11/// ) -> impl IntoResponse {
12///     // ...
13/// }
14/// ```
15pub struct XGithubEvent(pub String);
16
17impl Header for XGithubEvent {
18	fn name() -> &'static HeaderName {
19		&X_GITHUB_EVENT
20	}
21
22	fn decode<'i, I>(values: &mut I) -> Result<Self, headers::Error>
23	where
24		I: Iterator<Item = &'i HeaderValue>,
25	{
26		let value = values
27			.next()
28			.and_then(|h| HeaderValue::to_str(h).ok())
29			.and_then(|s| Some(s.to_string()))
30			// .and_then(|s| s.parse::<EventType>().ok())
31			.ok_or_else(headers::Error::invalid)?;
32
33		Ok(Self(value))
34	}
35
36	fn encode<E>(&self, _values: &mut E)
37	where
38		E: Extend<HeaderValue>,
39	{
40		// unnecessary, since we're only decoding
41		unreachable!()
42	}
43}