Skip to main content

dispatch_core/
response.rs

1use core::marker::PhantomData;
2
3use crate::{StageError, SystemParam, param::ParamSet};
4
5/// Adapts a typed function that transforms a successful command response.
6pub trait ResponseHandler<C, R, O, T, E, Params> {
7    /// Validates resource access, extracts arguments, and transforms `output`.
8    fn run(&self, context: &mut C, output: O, resources: &mut R) -> Result<T, StageError<E>>;
9}
10
11impl<C, R, O, T, E, F> ResponseHandler<C, R, O, T, E, ()> for F
12where
13    F: Fn(&mut C, O) -> Result<T, E>,
14{
15    fn run(&self, context: &mut C, output: O, _resources: &mut R) -> Result<T, StageError<E>> {
16        self(context, output).map_err(StageError::User)
17    }
18}
19
20macro_rules! impl_response_handler {
21    ($($param:ident => $value:ident @ $index:expr),+ $(,)?) => {
22        impl<C, R, O, T, E, F, $($param),+>
23            ResponseHandler<C, R, O, T, E, ($($param,)+)> for F
24        where
25            $($param: SystemParam<R>,)+
26            F: Fn(&mut C, O, $($param),+) -> Result<T, E>,
27            for<'context, 'resources> F:
28                Fn(
29                    &'context mut C,
30                    O,
31                    $($param::Item<'resources>),+
32                ) -> Result<T, E>,
33        {
34            fn run(
35                &self,
36                context: &mut C,
37                output: O,
38                resources: &mut R,
39            ) -> Result<T, StageError<E>> {
40                let ($($value,)+) =
41                    <($($param,)+) as ParamSet<R>>::fetch(resources)
42                        .map_err(StageError::Param)?;
43
44                self(context, output, $($value),+).map_err(StageError::User)
45            }
46        }
47    };
48}
49
50impl_response_handler!(P0 => p0 @ 0);
51impl_response_handler!(P0 => p0 @ 0, P1 => p1 @ 1);
52impl_response_handler!(P0 => p0 @ 0, P1 => p1 @ 1, P2 => p2 @ 2);
53impl_response_handler!(P0 => p0 @ 0, P1 => p1 @ 1, P2 => p2 @ 2, P3 => p3 @ 3);
54impl_response_handler!(
55    P0 => p0 @ 0,
56    P1 => p1 @ 1,
57    P2 => p2 @ 2,
58    P3 => p3 @ 3,
59    P4 => p4 @ 4,
60);
61impl_response_handler!(
62    P0 => p0 @ 0,
63    P1 => p1 @ 1,
64    P2 => p2 @ 2,
65    P3 => p3 @ 3,
66    P4 => p4 @ 4,
67    P5 => p5 @ 5,
68);
69impl_response_handler!(
70    P0 => p0 @ 0,
71    P1 => p1 @ 1,
72    P2 => p2 @ 2,
73    P3 => p3 @ 3,
74    P4 => p4 @ 4,
75    P5 => p5 @ 5,
76    P6 => p6 @ 6,
77);
78impl_response_handler!(
79    P0 => p0 @ 0,
80    P1 => p1 @ 1,
81    P2 => p2 @ 2,
82    P3 => p3 @ 3,
83    P4 => p4 @ 4,
84    P5 => p5 @ 5,
85    P6 => p6 @ 6,
86    P7 => p7 @ 7,
87);
88
89/// Finalizes a successful command output before it is returned to the caller.
90pub trait ResponseStage<C, R, O, E> {
91    /// Value returned by [`crate::Dispatcher::dispatch`].
92    type Output;
93
94    /// Transforms the successful command output.
95    fn run_response(
96        &self,
97        context: &mut C,
98        output: O,
99        resources: &mut R,
100    ) -> Result<Self::Output, StageError<E>>;
101}
102
103/// The default response stage, which returns command output unchanged.
104#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
105pub struct IdentityResponse;
106
107impl<C, R, O, E> ResponseStage<C, R, O, E> for IdentityResponse {
108    type Output = O;
109
110    fn run_response(
111        &self,
112        _context: &mut C,
113        output: O,
114        _resources: &mut R,
115    ) -> Result<Self::Output, StageError<E>> {
116        Ok(output)
117    }
118}
119
120/// Adapts one resource-injected function into a [`ResponseStage`].
121pub struct ResponseFn<F, Params, T> {
122    function: F,
123    marker: PhantomData<fn() -> (Params, T)>,
124}
125
126impl<F, Params, T> ResponseFn<F, Params, T> {
127    pub(crate) const fn new(function: F) -> Self {
128        Self {
129            function,
130            marker: PhantomData,
131        }
132    }
133
134    /// Returns the wrapped function.
135    pub const fn function(&self) -> &F {
136        &self.function
137    }
138
139    /// Returns a mutable reference to the wrapped function.
140    pub fn function_mut(&mut self) -> &mut F {
141        &mut self.function
142    }
143
144    /// Consumes the adapter and returns the wrapped function.
145    pub fn into_inner(self) -> F {
146        self.function
147    }
148}
149
150impl<C, R, O, T, E, F, Params> ResponseStage<C, R, O, E> for ResponseFn<F, Params, T>
151where
152    F: ResponseHandler<C, R, O, T, E, Params>,
153{
154    type Output = T;
155
156    fn run_response(
157        &self,
158        context: &mut C,
159        output: O,
160        resources: &mut R,
161    ) -> Result<Self::Output, StageError<E>> {
162        self.function.run(context, output, resources)
163    }
164}