dataflow_rs/engine/
workflow.rs

1use crate::engine::task::Task;
2use datalogic_rs::{DataLogic, Logic};
3use serde::Deserialize;
4use serde_json::Value;
5use std::fs;
6use std::path::Path;
7
8#[derive(Deserialize, Clone, Debug)]
9pub struct Workflow {
10    pub id: String,
11    pub name: String,
12    pub description: Option<String>,
13    pub condition: Option<Value>,
14    pub tasks: Vec<Task>,
15
16    #[serde(skip)]
17    pub task_logics: Vec<(Logic<'static>, Task)>,
18}
19
20impl Default for Workflow {
21    fn default() -> Self {
22        Self::new()
23    }
24}
25
26impl Workflow {
27    pub fn new() -> Self {
28        Workflow {
29            id: String::new(),
30            name: String::new(),
31            description: None,
32            condition: None,
33            tasks: Vec::new(),
34            task_logics: Vec::new(),
35        }
36    }
37
38    // Load workflow from JSON string
39    pub fn from_json(json_str: &str) -> Result<Self, serde_json::Error> {
40        serde_json::from_str(json_str)
41    }
42
43    // Load workflow from JSON file
44    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn std::error::Error>> {
45        let json_str = fs::read_to_string(path)?;
46        let workflow = Self::from_json(&json_str)?;
47        Ok(workflow)
48    }
49
50    pub fn prepare(&mut self, data_logic: &'static DataLogic) {
51        for task in &self.tasks {
52            let condition = task.condition.clone().unwrap();
53            let logic = data_logic.parse_logic_json(&condition, None).unwrap();
54            self.task_logics.push((logic, task.clone()));
55        }
56    }
57}
58
59// pub fn build(self, data_logic: &'static DataLogic) -> Workflow {
60//     let mut task_logics = Vec::new();
61//     for task in &self.tasks {
62//         let condition = task.condition.clone().unwrap();
63//         let logic = data_logic.parse_logic_json(&condition, None).unwrap();
64//         task_logics.push((logic, task.clone()));
65//     }
66
67//     Workflow {
68//         id: self.id.expect("Workflow ID is required"),
69//         name: self.name.expect("Workflow name is required"),
70//         description: self.description,
71//         condition: self.condition,
72//         tasks: self.tasks,
73//         task_logics,
74//     }
75// }