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
use trillium::{async_trait, Conn};

use crate::TryFromConn;

/// A trait to extract content from [`Conn`]s to be used as the second
/// argument to an api handler. Implement this for your types.
#[async_trait]
pub trait FromConn: Send + Sync + Sized + 'static {
    /// returning None from this will not call the api handler, but
    /// will halt the conn.
    async fn from_conn(conn: &mut Conn) -> Option<Self>;
}

#[async_trait]
impl FromConn for () {
    async fn from_conn(_: &mut Conn) -> Option<Self> {
        Some(())
    }
}

#[async_trait]
impl FromConn for String {
    async fn from_conn(conn: &mut Conn) -> Option<Self> {
        conn.request_body_string().await.ok()
    }
}

#[async_trait]
impl FromConn for Vec<u8> {
    async fn from_conn(conn: &mut Conn) -> Option<Self> {
        conn.request_body().await.read_bytes().await.ok()
    }
}

#[async_trait]
impl<E: FromConn> FromConn for Option<E> {
    async fn from_conn(conn: &mut Conn) -> Option<Self> {
        Some(E::from_conn(conn).await)
    }
}

#[async_trait]
impl<T, E> FromConn for Result<T, E>
where
    T: TryFromConn<Error = E>,
    E: Send + Sync + 'static,
{
    async fn from_conn(conn: &mut Conn) -> Option<Self> {
        Some(T::try_from_conn(conn).await)
    }
}