1use std::fmt::{self, Debug};
23use crate::{
4 capability::Operation,
5 core::resolve::{Resolve, ResolveError},
6};
78/// 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
17Op: Operation,
18{
19pub operation: Op,
20pub(crate) resolve: Resolve<Op::Output>,
21}
2223impl<Op> Request<Op>
24where
25Op: Operation,
26{
27pub(crate) fn resolves_never(operation: Op) -> Self {
28Self {
29 operation,
30 resolve: Resolve::Never,
31 }
32 }
3334pub(crate) fn resolves_once<F>(operation: Op, resolve: F) -> Self
35where
36F: FnOnce(Op::Output) + Send + 'static,
37 {
38Self {
39 operation,
40 resolve: Resolve::Once(Box::new(resolve)),
41 }
42 }
4344pub(crate) fn resolves_many_times<F>(operation: Op, resolve: F) -> Self
45where
46F: Fn(Op::Output) -> Result<(), ()> + Send + 'static,
47 {
48Self {
49 operation,
50 resolve: Resolve::Many(Box::new(resolve)),
51 }
52 }
5354/// 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.
60pub fn resolve(&mut self, output: Op::Output) -> Result<(), ResolveError> {
61self.resolve.resolve(output)
62 }
63}
6465impl<Op> fmt::Debug for Request<Op>
66where
67Op: Operation + Debug,
68{
69fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 f.debug_tuple("Request").field(&self.operation).finish()
71 }
72}