1pub mod modules;
2use crate::sender_safe;
3use crossbeam::channel::{self, Receiver};
4pub use modules::*;
5use std::collections::HashMap;
6use std::fmt::{Debug, Formatter};
7use tokio::sync::oneshot;
8use valu3::{traits::ToValueBehavior, value::Value};
9pub type ModuleId = usize;
10pub type MainRuntimeSender = channel::Sender<Package>;
11pub type ModuleSetupSender = oneshot::Sender<Option<channel::Sender<ModulePackage>>>;
12
13pub type ModuleReceiver = Receiver<ModulePackage>;
14
15#[derive(Debug, Clone)]
16pub struct ApplicationData {
17 pub name: Option<String>,
18 pub version: Option<String>,
19 pub environment: Option<String>,
20 pub description: Option<String>,
21 pub author: Option<String>,
22 pub license: Option<String>,
23 pub repository: Option<String>,
24 pub homepage: Option<String>,
25}
26
27#[derive(Debug)]
28pub struct ModuleSetup {
29 pub id: ModuleId,
30 pub setup_sender: ModuleSetupSender,
31 pub main_sender: Option<MainRuntimeSender>,
32 pub with: Value,
33 pub dispatch: tracing::Dispatch,
34 pub app_data: ApplicationData,
35}
36
37impl ModuleSetup {
38 pub fn is_main(&self) -> bool {
39 self.main_sender.is_some()
40 }
41}
42
43#[derive(Default)]
44pub struct Package {
45 pub response: Option<oneshot::Sender<Value>>,
46 pub request_data: Option<Value>,
47 pub origin: ModuleId,
48 pub span: Option<tracing::Span>,
49 pub dispatch: Option<tracing::Dispatch>,
50}
51
52impl Debug for Package {
54 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
55 let map: HashMap<_, _> = vec![
56 ("request_data", self.request_data.to_value()),
57 ("step_position", self.origin.to_value()),
58 ]
59 .into_iter()
60 .collect();
61
62 write!(
63 f,
64 "{}",
65 map.to_value().to_json(valu3::prelude::JsonMode::Inline)
66 )
67 }
68}
69
70impl Package {
71 pub fn get_data(&self) -> Option<&Value> {
72 self.request_data.as_ref()
73 }
74
75 pub fn send(&mut self, response_data: Value) {
76 if let Some(send) = self.response.take() {
77 sender_safe!(send, response_data);
78 }
79 }
80}