summaryrefslogtreecommitdiff
path: root/src/event/extract.rs
blob: 4a35937cafb23e47ac0b2266a3c00dfd8ce76fb9 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use std::ops::Deref;

use axum::{
    extract::{FromRequestParts, OptionalFromRequestParts},
    http::{request::Parts, HeaderName, HeaderValue},
};
use axum_extra::typed_header::TypedHeader;
use serde::{de::DeserializeOwned, Serialize};

// A typed header. When used as a bare extractor, reads from the
// `Last-Event-Id` HTTP header.
pub struct LastEventId<T>(pub T);

static LAST_EVENT_ID: HeaderName = HeaderName::from_static("last-event-id");

impl<T> headers::Header for LastEventId<T>
where
    T: Serialize + DeserializeOwned,
{
    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)?;
        let value = value.to_str().map_err(|_| headers::Error::invalid())?;
        let value = serde_json::from_str(value).map_err(|_| headers::Error::invalid())?;
        Ok(Self(value))
    }

    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 = serde_json::to_string(value).expect("value can be encoded as JSON");
        let value = HeaderValue::from_str(&value).expect("LastEventId is a valid header value");

        values.extend(std::iter::once(value));
    }
}

impl<S, T> FromRequestParts<S> for LastEventId<T>
where
    S: Send + Sync,
    T: Serialize + DeserializeOwned,
{
    type Rejection = <TypedHeader<Self> as FromRequestParts<S>>::Rejection;

    async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
        // This is purely for ergonomics: it allows `LastEventId` to be extracted
        // without having to wrap it in `TypedHeader<>`. Callers _can_ still do that,
        // but they aren't forced to.
        let header =
            <TypedHeader<Self> as FromRequestParts<S>>::from_request_parts(parts, state).await?;

        Ok(header.into())
    }
}

impl<S, T> OptionalFromRequestParts<S> for LastEventId<T>
where
    S: Send + Sync,
    T: Serialize + DeserializeOwned,
{
    type Rejection = <TypedHeader<Self> as FromRequestParts<S>>::Rejection;

    async fn from_request_parts(
        parts: &mut Parts,
        state: &S,
    ) -> Result<Option<Self>, Self::Rejection> {
        // This is purely for ergonomics: it allows `Option<LastEventId>` to be extracted
        // without having to wrap it in `TypedHeader<>`. Callers _can_ still do that,
        // but they aren't forced to.
        let header =
            <TypedHeader<Self> as OptionalFromRequestParts<S>>::from_request_parts(parts, state)
                .await?;

        Ok(header.map(Self::from))
    }
}

impl<T> Deref for LastEventId<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        let Self(header) = self;
        header
    }
}

impl<T> From<TypedHeader<LastEventId<T>>> for LastEventId<T> {
    fn from(header: TypedHeader<Self>) -> Self {
        let TypedHeader(value) = header;
        value
    }
}

impl<T> From<T> for LastEventId<T> {
    fn from(value: T) -> Self {
        Self(value)
    }
}

impl<T> LastEventId<T> {
    pub fn into_inner(self) -> T {
        let Self(value) = self;
        value
    }
}