phlow_sdk/structs/
mod.rs

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    pub is_test_mode: bool,
36}
37
38impl ModuleSetup {
39    pub fn is_main(&self) -> bool {
40        self.main_sender.is_some() || self.is_test_mode
41    }
42}
43
44#[derive(Default)]
45pub struct Package {
46    pub response: Option<oneshot::Sender<Value>>,
47    pub request_data: Option<Value>,
48    pub origin: ModuleId,
49    pub span: Option<tracing::Span>,
50    pub dispatch: Option<tracing::Dispatch>,
51}
52
53// Only production mode
54impl Debug for Package {
55    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
56        let map: HashMap<_, _> = vec![
57            ("request_data", self.request_data.to_value()),
58            ("step_position", self.origin.to_value()),
59        ]
60        .into_iter()
61        .collect();
62
63        write!(
64            f,
65            "{}",
66            map.to_value().to_json(valu3::prelude::JsonMode::Inline)
67        )
68    }
69}
70
71impl Package {
72    pub fn get_data(&self) -> Option<&Value> {
73        self.request_data.as_ref()
74    }
75
76    pub fn send(&mut self, response_data: Value) {
77        if let Some(send) = self.response.take() {
78            sender_safe!(send, response_data);
79        }
80    }
81}