bpmn-engine 0.1.0

BPMN 2.0 execution engine for Rust with JSON and XML format support
Documentation
//! BPMN 2.0 JSON Format
//!
//! JSON representation of BPMN 2.0 process definitions.
//!
//! This module defines the JSON schema for BPMN 2.0 processes,
//! designed to be compatible with standard BPMN 2.0 concepts
//! while using JSON as the serialization format.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// BPMN 2.0 JSON Process Definition
///
/// Represents a complete BPMN process definition in JSON format.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonProcess {
    /// Process ID
    pub id: String,
    /// Process name
    pub name: Option<String>,
    /// Process type (default: "process")
    #[serde(default = "default_process_type")]
    pub process_type: String,
    /// Is executable
    #[serde(default = "default_true")]
    pub is_executable: bool,
    /// Process elements (tasks, gateways, events, flows)
    pub elements: Vec<BpmnJsonElement>,
    /// Process variables
    #[serde(default)]
    pub variables: HashMap<String, BpmnJsonVariable>,
}

fn default_process_type() -> String {
    "process".to_string()
}

fn default_true() -> bool {
    true
}

/// BPMN JSON Element
///
/// Represents any BPMN element (task, gateway, event, flow, etc.)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum BpmnJsonElement {
    /// Start Event
    StartEvent(BpmnJsonStartEvent),
    /// End Event
    EndEvent(BpmnJsonEndEvent),
    /// Intermediate Catch Event
    IntermediateCatchEvent(BpmnJsonIntermediateCatchEvent),
    /// Intermediate Throw Event
    IntermediateThrowEvent(BpmnJsonIntermediateThrowEvent),
    /// Service Task
    ServiceTask(BpmnJsonServiceTask),
    /// User Task
    UserTask(BpmnJsonUserTask),
    /// Script Task
    ScriptTask(BpmnJsonScriptTask),
    /// Manual Task
    ManualTask(BpmnJsonManualTask),
    /// Exclusive Gateway
    ExclusiveGateway(BpmnJsonExclusiveGateway),
    /// Parallel Gateway
    ParallelGateway(BpmnJsonParallelGateway),
    /// Inclusive Gateway
    InclusiveGateway(BpmnJsonInclusiveGateway),
    /// Sequence Flow
    SequenceFlow(BpmnJsonSequenceFlow),
}

/// Base properties for all BPMN elements
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonElementBase {
    /// Element ID
    pub id: String,
    /// Element name
    pub name: Option<String>,
    /// Documentation
    pub documentation: Option<String>,
}

/// Start Event
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonStartEvent {
    #[serde(flatten)]
    pub base: BpmnJsonElementBase,
    /// Event definition (message, timer, signal, etc.)
    pub event_definition: Option<BpmnJsonEventDefinition>,
}

/// End Event
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonEndEvent {
    #[serde(flatten)]
    pub base: BpmnJsonElementBase,
    /// Event definition
    pub event_definition: Option<BpmnJsonEventDefinition>,
}

/// Intermediate Catch Event
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonIntermediateCatchEvent {
    #[serde(flatten)]
    pub base: BpmnJsonElementBase,
    /// Event definition
    pub event_definition: Option<BpmnJsonEventDefinition>,
}

/// Intermediate Throw Event
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonIntermediateThrowEvent {
    #[serde(flatten)]
    pub base: BpmnJsonElementBase,
    /// Event definition
    pub event_definition: Option<BpmnJsonEventDefinition>,
}

/// Service Task
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonServiceTask {
    #[serde(flatten)]
    pub base: BpmnJsonElementBase,
    /// Implementation (e.g., "webService", "expression")
    pub implementation: Option<String>,
    /// Operation reference
    pub operation_ref: Option<String>,
    /// Input/output mappings
    #[serde(default)]
    pub io_mapping: BpmnJsonIoMapping,
}

/// User Task
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonUserTask {
    #[serde(flatten)]
    pub base: BpmnJsonElementBase,
    /// Assignment
    pub assignment: Option<BpmnJsonAssignment>,
    /// Form key
    pub form_key: Option<String>,
}

/// Script Task
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonScriptTask {
    #[serde(flatten)]
    pub base: BpmnJsonElementBase,
    /// Script format (e.g., "javascript", "groovy")
    pub script_format: Option<String>,
    /// Script content
    pub script: Option<String>,
}

/// Manual Task
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonManualTask {
    #[serde(flatten)]
    pub base: BpmnJsonElementBase,
}

/// Exclusive Gateway
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonExclusiveGateway {
    #[serde(flatten)]
    pub base: BpmnJsonElementBase,
    /// Default flow ID
    pub default_flow: Option<String>,
}

/// Parallel Gateway
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonParallelGateway {
    #[serde(flatten)]
    pub base: BpmnJsonElementBase,
}

/// Inclusive Gateway
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonInclusiveGateway {
    #[serde(flatten)]
    pub base: BpmnJsonElementBase,
    /// Default flow ID
    pub default_flow: Option<String>,
}

/// Sequence Flow
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonSequenceFlow {
    #[serde(flatten)]
    pub base: BpmnJsonElementBase,
    /// Source element ID
    pub source_ref: String,
    /// Target element ID
    pub target_ref: String,
    /// Condition expression
    pub condition_expression: Option<BpmnJsonConditionExpression>,
}

/// Event Definition
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum BpmnJsonEventDefinition {
    /// Message Event Definition
    Message {
        /// Message reference
        message_ref: Option<String>,
    },
    /// Timer Event Definition
    Timer {
        /// Time definition (e.g., "PT1H", "R/PT1H")
        time_definition: Option<String>,
    },
    /// Signal Event Definition
    Signal {
        /// Signal reference
        signal_ref: Option<String>,
    },
    /// Error Event Definition
    Error {
        /// Error reference
        error_ref: Option<String>,
    },
    /// Escalation Event Definition
    Escalation {
        /// Escalation reference
        escalation_ref: Option<String>,
    },
    /// Cancel Event Definition
    Cancel,
    /// Compensation Event Definition
    Compensation {
        /// Activity reference
        activity_ref: Option<String>,
    },
    /// Conditional Event Definition
    Conditional {
        /// Condition expression
        condition: Option<BpmnJsonConditionExpression>,
    },
    /// Link Event Definition
    Link {
        /// Link name
        name: Option<String>,
    },
    /// Terminate Event Definition
    Terminate,
    /// None (no specific event definition)
    None,
}

/// Condition Expression
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonConditionExpression {
    /// Expression language (e.g., "javascript", "groovy")
    pub language: Option<String>,
    /// Expression body
    pub body: String,
}

/// Input/Output Mapping
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonIoMapping {
    /// Input parameters
    #[serde(default)]
    pub input_parameters: Vec<BpmnJsonIoParameter>,
    /// Output parameters
    #[serde(default)]
    pub output_parameters: Vec<BpmnJsonIoParameter>,
}

/// Input/Output Parameter
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonIoParameter {
    /// Parameter name
    pub name: String,
    /// Parameter source/target
    pub source: Option<String>,
    pub target: Option<String>,
    /// Parameter value/expression
    pub value: Option<String>,
}

/// Assignment
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonAssignment {
    /// Assignment type (e.g., "assignee", "candidateUsers", "candidateGroups")
    pub assignment_type: String,
    /// Assignment value
    pub value: String,
}

/// Variable Definition
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BpmnJsonVariable {
    /// Variable name
    pub name: String,
    /// Variable type
    pub variable_type: Option<String>,
    /// Default value
    pub default_value: Option<serde_json::Value>,
}