flo_scene/host/commands/
run_command.rs

1use crate::host::scene_message::*;
2use crate::host::stream_target::*;
3use crate::host::programs::*;
4
5use serde::*;
6
7use std::marker::{PhantomData};
8use std::sync::*;
9
10///
11/// The RunCommand is a query request that will run a named command with a parameter, returning the result as a stream of responses to a target
12///
13#[derive(Clone, PartialEq, Eq, Debug)]
14#[derive(Serialize, Deserialize)]
15pub struct RunCommand<TParameter, TResponse> {
16    /// Where the responses to the command should be sent
17    target:     StreamTarget,
18
19    /// The name of the command to run
20    name:       String,
21
22    /// Data to send to the command
23    parameter:  TParameter,
24
25    /// Phantom data for the response type
26    response:   PhantomData<Mutex<TResponse>>,
27}
28
29impl<TParameter, TResponse> RunCommand<TParameter, TResponse>
30where
31    TParameter: Unpin + Send,
32    TResponse:  Unpin + Send
33{
34    ///
35    /// Creates a new 'run command' request. The command with the specified name will be run, and will send its response to the target.
36    ///
37    pub fn new(target: impl Into<StreamTarget>, name: impl Into<String>, parameter: impl Into<TParameter>) -> Self {
38        Self {
39            target:     target.into(),
40            name:       name.into(),
41            parameter:  parameter.into(),
42            response:   PhantomData,
43        }
44    }
45
46    ///
47    /// Returns the program that the response to the command should be setn to
48    ///
49    pub fn target(&self) -> StreamTarget {
50        self.target.clone()
51    }
52
53    ///
54    /// The name of the command that is to be run
55    ///
56    pub fn name(&self) -> &str {
57        &self.name
58    }
59
60    ///
61    /// The parameter to the command
62    ///
63    pub fn parameter(&self) -> &TParameter {
64        &self.parameter
65    }
66}
67
68impl<TParameter, TResponse> SceneMessage for RunCommand<TParameter, TResponse>
69where
70    TParameter: 'static + Unpin + Send + Serialize,
71    TResponse:  'static + Unpin + Send + Serialize,
72    for<'de> TParameter: Deserialize<'de>,
73    for<'de> TResponse: Deserialize<'de>
74{
75    #[inline]
76    fn message_type_name() -> String { format!("flo_scene::RunCommand<{}, {}>", std::any::type_name::<TParameter>(), std::any::type_name::<TResponse>()) }
77}
78
79impl<TParameter, TResponse> QueryRequest for RunCommand<TParameter, TResponse> 
80where
81    TParameter: 'static + Unpin + Send + Serialize,
82    TResponse:  'static + Unpin + Send + Serialize,
83    for<'de> TParameter: Deserialize<'de>,
84    for<'de> TResponse: Deserialize<'de>
85{
86    type ResponseData = TResponse;
87
88    fn with_new_target(self, new_target: StreamTarget) -> Self {
89        RunCommand {
90            target:     new_target,
91            name:       self.name,
92            parameter:  self.parameter,
93            response:   PhantomData
94        }
95    }
96}