crux_core/core/
request.rs

1use std::fmt::{self, Debug};
2
3use crate::{
4    capability::Operation,
5    core::resolve::{Resolve, ResolveError},
6};
7
8/// Request represents an effect request from the core to the shell.
9///
10/// The `operation` is the input needed to process the effect, and will be one
11/// of the capabilities' [`Operation`] types.
12///
13/// The request can be resolved by passing it to `Core::resolve` along with the
14/// corresponding result of type `Operation::Output`.
15pub struct Request<Op>
16where
17    Op: Operation,
18{
19    pub operation: Op,
20    pub(crate) resolve: Resolve<Op::Output>,
21}
22
23impl<Op> Request<Op>
24where
25    Op: Operation,
26{
27    pub(crate) fn resolves_never(operation: Op) -> Self {
28        Self {
29            operation,
30            resolve: Resolve::Never,
31        }
32    }
33
34    pub(crate) fn resolves_once<F>(operation: Op, resolve: F) -> Self
35    where
36        F: FnOnce(Op::Output) + Send + 'static,
37    {
38        Self {
39            operation,
40            resolve: Resolve::Once(Box::new(resolve)),
41        }
42    }
43
44    pub(crate) fn resolves_many_times<F>(operation: Op, resolve: F) -> Self
45    where
46        F: Fn(Op::Output) -> Result<(), ()> + Send + 'static,
47    {
48        Self {
49            operation,
50            resolve: Resolve::Many(Box::new(resolve)),
51        }
52    }
53
54    /// Resolve the request with the given output.
55    ///
56    /// Note: This method should only be used in tests that work directly with
57    /// [`Command`](crate::command::Command).
58    /// If you are using [`AppTester`](crate::testing::AppTester) to test your app,
59    /// you should use [`AppTester::resolve`](crate::testing::AppTester::resolve) instead.
60    pub fn resolve(&mut self, output: Op::Output) -> Result<(), ResolveError> {
61        self.resolve.resolve(output)
62    }
63}
64
65impl<Op> fmt::Debug for Request<Op>
66where
67    Op: Operation + Debug,
68{
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        f.debug_tuple("Request").field(&self.operation).finish()
71    }
72}