Skip to main content

a3s_boot/http/
extractor.rs

1use super::request::BootRequest;
2use crate::Result;
3
4/// Custom request value extractor used by Nest-style controller argument binding.
5pub trait RequestExtractor<T>: Send + Sync + 'static {
6    fn extract(&self, request: &BootRequest) -> Result<T>;
7}
8
9impl<T, F> RequestExtractor<T> for F
10where
11    F: Fn(&BootRequest) -> Result<T> + Send + Sync + 'static,
12{
13    fn extract(&self, request: &BootRequest) -> Result<T> {
14        self(request)
15    }
16}
17
18pub fn extract_request_value<T, E>(request: &BootRequest, extractor: E) -> Result<T>
19where
20    E: RequestExtractor<T>,
21{
22    extractor.extract(request)
23}
24
25/// Transforms a single request value extracted from a path, query, header, or host parameter.
26pub trait RequestValuePipe<I, O>: Send + Sync + 'static {
27    fn transform(&self, value: I) -> Result<O>;
28}
29
30impl<I, O, F> RequestValuePipe<I, O> for F
31where
32    F: Fn(I) -> Result<O> + Send + Sync + 'static,
33{
34    fn transform(&self, value: I) -> Result<O> {
35        self(value)
36    }
37}
38
39pub fn transform_request_value<I, O, P>(value: I, pipe: P) -> Result<O>
40where
41    P: RequestValuePipe<I, O>,
42{
43    pipe.transform(value)
44}