Files
osiris/src/objects/flow/template.rs
Timur Gordon 87c556df7a wip
2025-10-29 16:52:33 +01:00

118 lines
3.1 KiB
Rust

/// Flow Template
///
/// Defines a reusable workflow template with steps that can be instantiated multiple times.
use crate::store::{BaseData, Object, Storable};
use serde::{Deserialize, Serialize};
/// A step in a flow template
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct FlowStep {
/// Step name/identifier
pub name: String,
/// Step description
pub description: String,
/// Steps that must be completed before this step can start
#[serde(default)]
pub dependencies: Vec<String>,
}
impl FlowStep {
pub fn new(name: String, description: String) -> Self {
Self {
name,
description,
dependencies: Vec::new(),
}
}
pub fn with_dependencies(mut self, dependencies: Vec<String>) -> Self {
self.dependencies = dependencies;
self
}
pub fn add_dependency(&mut self, dependency: String) {
self.dependencies.push(dependency);
}
}
/// Flow Template - defines a reusable workflow
#[derive(Debug, Clone, Serialize, Deserialize, Default, crate::DeriveObject)]
pub struct FlowTemplate {
#[serde(flatten)]
pub base_data: BaseData,
/// Template name
pub name: String,
/// Template description
pub description: String,
/// Ordered list of steps
pub steps: Vec<FlowStep>,
/// Template metadata
#[serde(default)]
pub metadata: std::collections::HashMap<String, String>,
}
impl FlowTemplate {
/// Create a new flow template
pub fn new(id: u32) -> Self {
let mut base_data = BaseData::new();
base_data.id = id;
Self {
base_data,
name: String::new(),
description: String::new(),
steps: Vec::new(),
metadata: std::collections::HashMap::new(),
}
}
/// Builder: Set name
pub fn name(mut self, name: String) -> Self {
self.name = name;
self.base_data.update_modified();
self
}
/// Builder: Set description
pub fn description(mut self, description: String) -> Self {
self.description = description;
self.base_data.update_modified();
self
}
/// Add a step to the template
pub fn add_step(&mut self, name: String, description: String) {
self.steps.push(FlowStep::new(name, description));
self.base_data.update_modified();
}
/// Add a step with dependencies
pub fn add_step_with_dependencies(&mut self, name: String, description: String, dependencies: Vec<String>) {
let step = FlowStep::new(name, description).with_dependencies(dependencies);
self.steps.push(step);
self.base_data.update_modified();
}
/// Get step by name
pub fn get_step(&self, name: &str) -> Option<&FlowStep> {
self.steps.iter().find(|s| s.name == name)
}
/// Add metadata
pub fn add_metadata(&mut self, key: String, value: String) {
self.metadata.insert(key, value);
self.base_data.update_modified();
}
/// Build (for fluent API compatibility)
pub fn build(self) -> Self {
self
}
}