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
use hyper::Request;

use crate::{Extract, SgBody};

/// Just extract and attach the extension to the request
#[derive(Debug, Clone)]
pub struct Extension<E>(E);

impl<E> Extension<E> {
    pub fn new(e: E) -> Self {
        Self(e)
    }

    pub fn into_inner(self) -> E {
        self.0
    }
}

impl<E> Extract for Option<Extension<E>>
where
    E: Send + Sync + 'static + Clone,
{
    fn extract(req: &Request<SgBody>) -> Self {
        req.extensions().get::<Extension<E>>().cloned()
    }
}

impl<E> Extract for Extension<Option<E>>
where
    E: Send + Sync + 'static + Clone,
{
    fn extract(req: &Request<SgBody>) -> Self {
        if let Some(ext) = req.extensions().get::<Extension<E>>() {
            Self(Some(ext.0.clone()))
        } else {
            Self(None)
        }
    }
}