msr_plugin_csv_event_journal/api/
controller.rs

1use msr_core::event_journal::{Entry, StoredRecord};
2
3use msr_plugin::{reply_channel, send_message_receive_result};
4
5use crate::{MessageSender, PluginResult};
6
7use super::{query, Command, Config, Query, RecordEntryOutcome, State, Status};
8
9/// Remote controller for the plugin
10///
11/// Wraps the message-based communication with the plugin
12/// into asynchronous functions.
13#[derive(Debug, Clone)]
14pub struct Controller {
15    message_tx: MessageSender,
16}
17
18impl Controller {
19    #[must_use]
20    pub const fn new(message_tx: MessageSender) -> Self {
21        Self { message_tx }
22    }
23
24    pub async fn command_replace_config(&self, new_config: Config) -> PluginResult<Config> {
25        let (reply_tx, reply_rx) = reply_channel();
26        let command = Command::ReplaceConfig(reply_tx, new_config);
27
28        send_message_receive_result(command, &self.message_tx, reply_rx).await
29    }
30
31    pub async fn command_switch_state(&self, new_state: State) -> PluginResult<()> {
32        let (reply_tx, reply_rx) = reply_channel();
33        let command = Command::SwitchState(reply_tx, new_state);
34        send_message_receive_result(command, &self.message_tx, reply_rx).await
35    }
36
37    pub async fn command_record_entry(&self, new_entry: Entry) -> PluginResult<RecordEntryOutcome> {
38        let (reply_tx, reply_rx) = reply_channel();
39        let command = Command::RecordEntry(reply_tx, new_entry);
40
41        send_message_receive_result(command, &self.message_tx, reply_rx).await
42    }
43
44    pub async fn command_shutdown(&self) -> PluginResult<()> {
45        let (reply_tx, reply_rx) = reply_channel();
46        let command = Command::Shutdown(reply_tx);
47        send_message_receive_result(command, &self.message_tx, reply_rx).await
48    }
49
50    pub async fn query_config(&self) -> PluginResult<Config> {
51        let (reply_tx, reply_rx) = reply_channel();
52        let query = Query::Config(reply_tx);
53        send_message_receive_result(query, &self.message_tx, reply_rx).await
54    }
55
56    pub async fn query_status(&self, request: query::StatusRequest) -> PluginResult<Status> {
57        let (reply_tx, reply_rx) = reply_channel();
58        let query = Query::Status(reply_tx, request);
59        send_message_receive_result(query, &self.message_tx, reply_rx).await
60    }
61
62    pub async fn query_recent_records(
63        &self,
64        request: query::RecentRecordsRequest,
65    ) -> PluginResult<Vec<StoredRecord>> {
66        let (reply_tx, reply_rx) = reply_channel();
67        let query = Query::RecentRecords(reply_tx, request);
68        send_message_receive_result(query, &self.message_tx, reply_rx).await
69    }
70
71    pub async fn query_filter_records(
72        &self,
73        request: query::FilterRecordsRequest,
74    ) -> PluginResult<Vec<StoredRecord>> {
75        let (reply_tx, reply_rx) = reply_channel();
76        let query = Query::FilterRecords(reply_tx, request);
77        send_message_receive_result(query, &self.message_tx, reply_rx).await
78    }
79}