composable_tower_http/extract/
extractor.rs

1use std::future::Future;
2
3use http::HeaderMap;
4
5use super::chain::chain_extractor::ChainExtractor;
6
7use super::chain::lite::{AsyncChainLite, ChainLite};
8use super::{
9    and::AndExtractor,
10    any::Any,
11    convert::{AsyncConvert, Convert},
12    map::{AsyncMap, Map, MapError},
13    optional::Optional,
14    or::OrExtractor,
15};
16
17pub trait Extractor {
18    type Extracted: Clone + Send + Sync;
19
20    type Error;
21
22    fn extract(
23        &self,
24        headers: &HeaderMap,
25    ) -> impl Future<Output = Result<Self::Extracted, Self::Error>> + Send;
26
27    fn extracted_type_name(&self) -> &'static str {
28        std::any::type_name::<Self::Extracted>()
29    }
30}
31
32pub trait ExtractorExt: Sized + Extractor {
33    fn map<Fn>(self, map: Fn) -> Map<Self, Fn> {
34        Map::new(self, map)
35    }
36
37    fn async_map<Fn>(self, map: Fn) -> AsyncMap<Self, Fn> {
38        AsyncMap::new(self, map)
39    }
40
41    fn map_err<Fn>(self, map_err: Fn) -> MapError<Self, Fn> {
42        MapError::new(self, map_err)
43    }
44
45    fn convert<Fn>(self, convert: Fn) -> Convert<Self, Fn> {
46        Convert::new(self, convert)
47    }
48
49    fn async_convert<Fn>(self, convert: Fn) -> AsyncConvert<Self, Fn> {
50        AsyncConvert::new(self, convert)
51    }
52
53    fn chain<C>(self, chain: C) -> ChainExtractor<Self, C> {
54        ChainExtractor::new(self, chain)
55    }
56
57    fn chain_lite<Fn>(self, chain: Fn) -> ChainLite<Self, Fn> {
58        ChainLite::new(self, chain)
59    }
60
61    fn async_chain_lite<Fn>(self, chain: Fn) -> AsyncChainLite<Self, Fn> {
62        AsyncChainLite::new(self, chain)
63    }
64
65    fn optional(self) -> Optional<Self> {
66        Optional::new(self)
67    }
68
69    fn any<Ex>(self, other: Ex) -> Any<Self, Ex> {
70        Any::new(self, other)
71    }
72
73    fn or<Ex>(self, other: Ex) -> OrExtractor<Self, Ex> {
74        OrExtractor::new(self, other)
75    }
76
77    fn and<Ex>(self, other: Ex) -> AndExtractor<Self, Ex> {
78        AndExtractor::new(self, other)
79    }
80}
81
82impl<T> ExtractorExt for T where T: Sized + Extractor {}