Skip to main content

bpmn_engine/model/
json.rs

1//! BPMN 2.0 JSON Format
2//!
3//! JSON representation of BPMN 2.0 process definitions.
4//!
5//! This module defines the JSON schema for BPMN 2.0 processes,
6//! designed to be compatible with standard BPMN 2.0 concepts
7//! while using JSON as the serialization format.
8
9use serde::{Deserialize, Serialize};
10use std::collections::HashMap;
11
12/// BPMN 2.0 JSON Process Definition
13///
14/// Represents a complete BPMN process definition in JSON format.
15#[derive(Debug, Clone, Serialize, Deserialize)]
16#[serde(rename_all = "camelCase")]
17pub struct BpmnJsonProcess {
18    /// Process ID
19    pub id: String,
20    /// Process name
21    pub name: Option<String>,
22    /// Process type (default: "process")
23    #[serde(default = "default_process_type")]
24    pub process_type: String,
25    /// Is executable
26    #[serde(default = "default_true")]
27    pub is_executable: bool,
28    /// Process elements (tasks, gateways, events, flows)
29    pub elements: Vec<BpmnJsonElement>,
30    /// Process variables
31    #[serde(default)]
32    pub variables: HashMap<String, BpmnJsonVariable>,
33}
34
35fn default_process_type() -> String {
36    "process".to_string()
37}
38
39fn default_true() -> bool {
40    true
41}
42
43/// BPMN JSON Element
44///
45/// Represents any BPMN element (task, gateway, event, flow, etc.)
46#[derive(Debug, Clone, Serialize, Deserialize)]
47#[serde(tag = "type", rename_all = "camelCase")]
48pub enum BpmnJsonElement {
49    /// Start Event
50    StartEvent(BpmnJsonStartEvent),
51    /// End Event
52    EndEvent(BpmnJsonEndEvent),
53    /// Intermediate Catch Event
54    IntermediateCatchEvent(BpmnJsonIntermediateCatchEvent),
55    /// Intermediate Throw Event
56    IntermediateThrowEvent(BpmnJsonIntermediateThrowEvent),
57    /// Service Task
58    ServiceTask(BpmnJsonServiceTask),
59    /// User Task
60    UserTask(BpmnJsonUserTask),
61    /// Script Task
62    ScriptTask(BpmnJsonScriptTask),
63    /// Manual Task
64    ManualTask(BpmnJsonManualTask),
65    /// Exclusive Gateway
66    ExclusiveGateway(BpmnJsonExclusiveGateway),
67    /// Parallel Gateway
68    ParallelGateway(BpmnJsonParallelGateway),
69    /// Inclusive Gateway
70    InclusiveGateway(BpmnJsonInclusiveGateway),
71    /// Sequence Flow
72    SequenceFlow(BpmnJsonSequenceFlow),
73}
74
75/// Base properties for all BPMN elements
76#[derive(Debug, Clone, Serialize, Deserialize)]
77#[serde(rename_all = "camelCase")]
78pub struct BpmnJsonElementBase {
79    /// Element ID
80    pub id: String,
81    /// Element name
82    pub name: Option<String>,
83    /// Documentation
84    pub documentation: Option<String>,
85}
86
87/// Start Event
88#[derive(Debug, Clone, Serialize, Deserialize)]
89#[serde(rename_all = "camelCase")]
90pub struct BpmnJsonStartEvent {
91    #[serde(flatten)]
92    pub base: BpmnJsonElementBase,
93    /// Event definition (message, timer, signal, etc.)
94    pub event_definition: Option<BpmnJsonEventDefinition>,
95}
96
97/// End Event
98#[derive(Debug, Clone, Serialize, Deserialize)]
99#[serde(rename_all = "camelCase")]
100pub struct BpmnJsonEndEvent {
101    #[serde(flatten)]
102    pub base: BpmnJsonElementBase,
103    /// Event definition
104    pub event_definition: Option<BpmnJsonEventDefinition>,
105}
106
107/// Intermediate Catch Event
108#[derive(Debug, Clone, Serialize, Deserialize)]
109#[serde(rename_all = "camelCase")]
110pub struct BpmnJsonIntermediateCatchEvent {
111    #[serde(flatten)]
112    pub base: BpmnJsonElementBase,
113    /// Event definition
114    pub event_definition: Option<BpmnJsonEventDefinition>,
115}
116
117/// Intermediate Throw Event
118#[derive(Debug, Clone, Serialize, Deserialize)]
119#[serde(rename_all = "camelCase")]
120pub struct BpmnJsonIntermediateThrowEvent {
121    #[serde(flatten)]
122    pub base: BpmnJsonElementBase,
123    /// Event definition
124    pub event_definition: Option<BpmnJsonEventDefinition>,
125}
126
127/// Service Task
128#[derive(Debug, Clone, Serialize, Deserialize)]
129#[serde(rename_all = "camelCase")]
130pub struct BpmnJsonServiceTask {
131    #[serde(flatten)]
132    pub base: BpmnJsonElementBase,
133    /// Implementation (e.g., "webService", "expression")
134    pub implementation: Option<String>,
135    /// Operation reference
136    pub operation_ref: Option<String>,
137    /// Input/output mappings
138    #[serde(default)]
139    pub io_mapping: BpmnJsonIoMapping,
140}
141
142/// User Task
143#[derive(Debug, Clone, Serialize, Deserialize)]
144#[serde(rename_all = "camelCase")]
145pub struct BpmnJsonUserTask {
146    #[serde(flatten)]
147    pub base: BpmnJsonElementBase,
148    /// Assignment
149    pub assignment: Option<BpmnJsonAssignment>,
150    /// Form key
151    pub form_key: Option<String>,
152}
153
154/// Script Task
155#[derive(Debug, Clone, Serialize, Deserialize)]
156#[serde(rename_all = "camelCase")]
157pub struct BpmnJsonScriptTask {
158    #[serde(flatten)]
159    pub base: BpmnJsonElementBase,
160    /// Script format (e.g., "javascript", "groovy")
161    pub script_format: Option<String>,
162    /// Script content
163    pub script: Option<String>,
164}
165
166/// Manual Task
167#[derive(Debug, Clone, Serialize, Deserialize)]
168#[serde(rename_all = "camelCase")]
169pub struct BpmnJsonManualTask {
170    #[serde(flatten)]
171    pub base: BpmnJsonElementBase,
172}
173
174/// Exclusive Gateway
175#[derive(Debug, Clone, Serialize, Deserialize)]
176#[serde(rename_all = "camelCase")]
177pub struct BpmnJsonExclusiveGateway {
178    #[serde(flatten)]
179    pub base: BpmnJsonElementBase,
180    /// Default flow ID
181    pub default_flow: Option<String>,
182}
183
184/// Parallel Gateway
185#[derive(Debug, Clone, Serialize, Deserialize)]
186#[serde(rename_all = "camelCase")]
187pub struct BpmnJsonParallelGateway {
188    #[serde(flatten)]
189    pub base: BpmnJsonElementBase,
190}
191
192/// Inclusive Gateway
193#[derive(Debug, Clone, Serialize, Deserialize)]
194#[serde(rename_all = "camelCase")]
195pub struct BpmnJsonInclusiveGateway {
196    #[serde(flatten)]
197    pub base: BpmnJsonElementBase,
198    /// Default flow ID
199    pub default_flow: Option<String>,
200}
201
202/// Sequence Flow
203#[derive(Debug, Clone, Serialize, Deserialize)]
204#[serde(rename_all = "camelCase")]
205pub struct BpmnJsonSequenceFlow {
206    #[serde(flatten)]
207    pub base: BpmnJsonElementBase,
208    /// Source element ID
209    pub source_ref: String,
210    /// Target element ID
211    pub target_ref: String,
212    /// Condition expression
213    pub condition_expression: Option<BpmnJsonConditionExpression>,
214}
215
216/// Event Definition
217#[derive(Debug, Clone, Serialize, Deserialize)]
218#[serde(tag = "type", rename_all = "camelCase")]
219pub enum BpmnJsonEventDefinition {
220    /// Message Event Definition
221    Message {
222        /// Message reference
223        message_ref: Option<String>,
224    },
225    /// Timer Event Definition
226    Timer {
227        /// Time definition (e.g., "PT1H", "R/PT1H")
228        time_definition: Option<String>,
229    },
230    /// Signal Event Definition
231    Signal {
232        /// Signal reference
233        signal_ref: Option<String>,
234    },
235    /// Error Event Definition
236    Error {
237        /// Error reference
238        error_ref: Option<String>,
239    },
240    /// Escalation Event Definition
241    Escalation {
242        /// Escalation reference
243        escalation_ref: Option<String>,
244    },
245    /// Cancel Event Definition
246    Cancel,
247    /// Compensation Event Definition
248    Compensation {
249        /// Activity reference
250        activity_ref: Option<String>,
251    },
252    /// Conditional Event Definition
253    Conditional {
254        /// Condition expression
255        condition: Option<BpmnJsonConditionExpression>,
256    },
257    /// Link Event Definition
258    Link {
259        /// Link name
260        name: Option<String>,
261    },
262    /// Terminate Event Definition
263    Terminate,
264    /// None (no specific event definition)
265    None,
266}
267
268/// Condition Expression
269#[derive(Debug, Clone, Serialize, Deserialize)]
270#[serde(rename_all = "camelCase")]
271pub struct BpmnJsonConditionExpression {
272    /// Expression language (e.g., "javascript", "groovy")
273    pub language: Option<String>,
274    /// Expression body
275    pub body: String,
276}
277
278/// Input/Output Mapping
279#[derive(Debug, Clone, Default, Serialize, Deserialize)]
280#[serde(rename_all = "camelCase")]
281pub struct BpmnJsonIoMapping {
282    /// Input parameters
283    #[serde(default)]
284    pub input_parameters: Vec<BpmnJsonIoParameter>,
285    /// Output parameters
286    #[serde(default)]
287    pub output_parameters: Vec<BpmnJsonIoParameter>,
288}
289
290/// Input/Output Parameter
291#[derive(Debug, Clone, Serialize, Deserialize)]
292#[serde(rename_all = "camelCase")]
293pub struct BpmnJsonIoParameter {
294    /// Parameter name
295    pub name: String,
296    /// Parameter source/target
297    pub source: Option<String>,
298    pub target: Option<String>,
299    /// Parameter value/expression
300    pub value: Option<String>,
301}
302
303/// Assignment
304#[derive(Debug, Clone, Serialize, Deserialize)]
305#[serde(rename_all = "camelCase")]
306pub struct BpmnJsonAssignment {
307    /// Assignment type (e.g., "assignee", "candidateUsers", "candidateGroups")
308    pub assignment_type: String,
309    /// Assignment value
310    pub value: String,
311}
312
313/// Variable Definition
314#[derive(Debug, Clone, Serialize, Deserialize)]
315#[serde(rename_all = "camelCase")]
316pub struct BpmnJsonVariable {
317    /// Variable name
318    pub name: String,
319    /// Variable type
320    pub variable_type: Option<String>,
321    /// Default value
322    pub default_value: Option<serde_json::Value>,
323}
324