dataflow_rs/engine/functions/
mod.rs

1use crate::engine::error::Result;
2use crate::engine::message::{Change, Message};
3use async_trait::async_trait;
4use serde_json::Value;
5use std::cell::RefCell;
6
7pub mod validation;
8pub use validation::ValidationFunction;
9
10pub mod http;
11pub use http::*;
12
13pub mod map;
14pub use map::MapFunction;
15
16// Thread-local DataLogic instance to avoid mutex contention
17thread_local! {
18    pub static FUNCTION_DATA_LOGIC: RefCell<datalogic_rs::DataLogic> =
19        RefCell::new(datalogic_rs::DataLogic::new());
20}
21
22// Re-export all built-in functions for easier access
23pub mod builtins {
24    use super::*;
25
26    // Standard function names used for registering built-ins
27    pub const VALIDATION_FUNCTION: &str = "validate";
28    pub const MAP_FUNCTION: &str = "map";
29    pub const HTTP_FUNCTION: &str = "http";
30
31    // Get all built-in functions with their standard names
32    pub fn get_all_functions() -> Vec<(String, Box<dyn AsyncFunctionHandler + Send + Sync>)> {
33        vec![
34            // Create validation function with thread-local DataLogic
35            (
36                VALIDATION_FUNCTION.to_string(),
37                Box::new(ValidationFunction::new()),
38            ),
39            // Create map function with thread-local DataLogic
40            (MAP_FUNCTION.to_string(), Box::new(MapFunction::new())),
41            // Create HTTP function with 30-second timeout
42            (HTTP_FUNCTION.to_string(), Box::new(HttpFunction::new(30))),
43        ]
44    }
45
46    // Get all built-in async functions with their standard names
47    // This is the same as get_all_functions but separated to maintain
48    // API compatibility for AsyncEngine
49    pub fn get_all_async_functions() -> Vec<(String, Box<dyn AsyncFunctionHandler + Send + Sync>)> {
50        get_all_functions()
51    }
52}
53
54/// Async interface for task functions that operate on messages
55///
56/// This trait defines how async task functions process a message with given
57/// input parameters. It is particularly useful for IO-bound operations
58/// like HTTP requests, file operations, and database queries.
59#[async_trait]
60pub trait AsyncFunctionHandler: Send + Sync {
61    /// Execute the function asynchronously on a message with input parameters
62    ///
63    /// # Arguments
64    ///
65    /// * `message` - The message to process
66    /// * `input` - Function input parameters
67    ///
68    /// # Returns
69    ///
70    /// * `Result<(usize, Vec<Change>)>` - Result containing status code and changes, or error
71    async fn execute(&self, message: &mut Message, input: &Value) -> Result<(usize, Vec<Change>)>;
72}