dataflow-rs 2.1.5

A lightweight rules engine for building IFTTT-style automation and data processing pipelines in Rust. Define rules with JSONLogic conditions, execute actions, and chain workflows.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! # Parse Function Module
//!
//! This module provides parsing capabilities for converting payload data into structured
//! context data. It supports JSON and XML parsing, allowing workflows to start by loading
//! payload into the context where it can be accessed by subsequent tasks.
//!
//! ## Features
//!
//! - Parse JSON payload into data field
//! - Parse XML payload into JSON data field
//! - Support for nested source paths (payload.body, data.field)
//! - Automatic change tracking for audit trails
//!
//! ## Example Usage
//!
//! ```json
//! {
//!     "name": "parse_json",
//!     "input": {
//!         "source": "payload",
//!         "target": "input_data"
//!     }
//! }
//! ```

use crate::engine::error::{DataflowError, Result};
use crate::engine::message::{Change, Message};
use crate::engine::utils::get_nested_value;
use log::debug;
use serde::Deserialize;
use serde_json::Value;
use std::sync::Arc;

/// Configuration for parse functions.
///
/// Specifies where to read the source data from and where to store
/// the parsed result in the data context.
#[derive(Debug, Clone, Deserialize)]
pub struct ParseConfig {
    /// Source path to read from.
    /// - "payload" - Read the entire payload
    /// - "payload.field" - Read a nested field from payload
    /// - "data.field" - Read from existing data context
    pub source: String,

    /// Target field name in data where the parsed result will be stored.
    /// The result is stored at `data.{target}`.
    pub target: String,
}

impl ParseConfig {
    /// Parses a `ParseConfig` from a JSON value.
    ///
    /// # Arguments
    /// * `input` - JSON object containing "source" and "target" fields
    ///
    /// # Errors
    /// Returns `DataflowError::Validation` if required fields are missing
    pub fn from_json(input: &Value) -> Result<Self> {
        let source = input
            .get("source")
            .and_then(Value::as_str)
            .ok_or_else(|| {
                DataflowError::Validation("Missing 'source' in parse config".to_string())
            })?
            .to_string();

        let target = input
            .get("target")
            .and_then(Value::as_str)
            .ok_or_else(|| {
                DataflowError::Validation("Missing 'target' in parse config".to_string())
            })?
            .to_string();

        Ok(ParseConfig { source, target })
    }

    /// Extract source data based on the source path configuration.
    ///
    /// # Arguments
    /// * `message` - The message to extract data from
    ///
    /// # Returns
    /// The extracted value, or Value::Null if not found
    fn extract_source(&self, message: &Message) -> Value {
        if self.source == "payload" {
            (*message.payload).clone()
        } else if let Some(path) = self.source.strip_prefix("payload.") {
            get_nested_value(&message.payload, path)
                .cloned()
                .unwrap_or(Value::Null)
        } else if let Some(path) = self.source.strip_prefix("data.") {
            get_nested_value(message.data(), path)
                .cloned()
                .unwrap_or(Value::Null)
        } else {
            // Try to get from context directly
            get_nested_value(&message.context, &self.source)
                .cloned()
                .unwrap_or(Value::Null)
        }
    }
}

/// Execute parse_json operation.
///
/// Extracts JSON data from the source path and stores it in the target data field.
/// This is typically used at the start of a workflow to load payload into context.
///
/// # Arguments
/// * `message` - The message to process (modified in place)
/// * `config` - Parse configuration specifying source and target
///
/// # Returns
/// * `Ok((200, changes))` - Success with list of changes for audit trail
/// * `Err` - If configuration is invalid
pub fn execute_parse_json(
    message: &mut Message,
    config: &ParseConfig,
) -> Result<(usize, Vec<Change>)> {
    debug!(
        "ParseJson: Extracting from '{}' to 'data.{}'",
        config.source, config.target
    );

    // Extract source data
    let source_data = config.extract_source(message);

    // If source is a JSON string, parse it into a structured value
    let source_data = match &source_data {
        Value::String(s) => serde_json::from_str(s).unwrap_or(source_data),
        _ => source_data,
    };

    // Get old value for change tracking
    let old_value = message
        .data()
        .get(&config.target)
        .cloned()
        .unwrap_or(Value::Null);

    // Store to target in data
    if let Some(data_obj) = message.data_mut().as_object_mut() {
        data_obj.insert(config.target.clone(), source_data.clone());
    }

    // Invalidate context cache
    message.invalidate_context_cache();

    debug!(
        "ParseJson: Successfully stored data to 'data.{}'",
        config.target
    );

    Ok((
        200,
        vec![Change {
            path: Arc::from(format!("data.{}", config.target)),
            old_value: Arc::new(old_value),
            new_value: Arc::new(source_data),
        }],
    ))
}

/// Execute parse_xml operation.
///
/// Extracts XML string from the source path, parses it to JSON, and stores
/// it in the target data field.
///
/// # Arguments
/// * `message` - The message to process (modified in place)
/// * `config` - Parse configuration specifying source and target
///
/// # Returns
/// * `Ok((200, changes))` - Success with list of changes for audit trail
/// * `Err` - If configuration is invalid or XML parsing fails
pub fn execute_parse_xml(
    message: &mut Message,
    config: &ParseConfig,
) -> Result<(usize, Vec<Change>)> {
    debug!(
        "ParseXml: Extracting from '{}' to 'data.{}'",
        config.source, config.target
    );

    // Extract source data
    let source_data = config.extract_source(message);

    // Get XML string
    let xml_string = match &source_data {
        Value::String(s) => s.clone(),
        _ => {
            return Err(DataflowError::Validation(format!(
                "ParseXml: Source '{}' is not a string",
                config.source
            )));
        }
    };

    // Parse XML to JSON
    let parsed_json = xml_to_json(&xml_string)?;

    // Get old value for change tracking
    let old_value = message
        .data()
        .get(&config.target)
        .cloned()
        .unwrap_or(Value::Null);

    // Store to target in data
    if let Some(data_obj) = message.data_mut().as_object_mut() {
        data_obj.insert(config.target.clone(), parsed_json.clone());
    }

    // Invalidate context cache
    message.invalidate_context_cache();

    debug!(
        "ParseXml: Successfully parsed and stored XML to 'data.{}'",
        config.target
    );

    Ok((
        200,
        vec![Change {
            path: Arc::from(format!("data.{}", config.target)),
            old_value: Arc::new(old_value),
            new_value: Arc::new(parsed_json),
        }],
    ))
}

/// Convert XML string to JSON Value.
///
/// Uses quick-xml with serde for conversion. The resulting JSON structure
/// follows the convention where:
/// - Element names become object keys
/// - Text content is stored under "$text" key
/// - Attributes are stored under "$attr" key
/// - Multiple child elements with same name become arrays
fn xml_to_json(xml: &str) -> Result<Value> {
    use quick_xml::de::from_str;

    // Parse XML to JSON using quick-xml's serde support
    let parsed: Value = from_str(xml)
        .map_err(|e| DataflowError::Validation(format!("Failed to parse XML: {}", e)))?;

    Ok(parsed)
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_parse_config_from_json() {
        let input = json!({
            "source": "payload",
            "target": "input_data"
        });

        let config = ParseConfig::from_json(&input).unwrap();
        assert_eq!(config.source, "payload");
        assert_eq!(config.target, "input_data");
    }

    #[test]
    fn test_parse_config_missing_source() {
        let input = json!({
            "target": "input_data"
        });

        let result = ParseConfig::from_json(&input);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_config_missing_target() {
        let input = json!({
            "source": "payload"
        });

        let result = ParseConfig::from_json(&input);
        assert!(result.is_err());
    }

    #[test]
    fn test_execute_parse_json_from_payload() {
        let payload = json!({
            "name": "John",
            "age": 30
        });
        let mut message = Message::new(Arc::new(payload));

        let config = ParseConfig {
            source: "payload".to_string(),
            target: "input".to_string(),
        };

        let result = execute_parse_json(&mut message, &config);
        assert!(result.is_ok());

        let (status, changes) = result.unwrap();
        assert_eq!(status, 200);
        assert_eq!(changes.len(), 1);
        assert_eq!(changes[0].path.as_ref(), "data.input");

        // Verify data was stored
        assert_eq!(message.data()["input"]["name"], json!("John"));
        assert_eq!(message.data()["input"]["age"], json!(30));
    }

    #[test]
    fn test_execute_parse_json_from_nested_payload() {
        let payload = json!({
            "body": {
                "user": {
                    "name": "Alice"
                }
            }
        });
        let mut message = Message::new(Arc::new(payload));

        let config = ParseConfig {
            source: "payload.body.user".to_string(),
            target: "user_data".to_string(),
        };

        let result = execute_parse_json(&mut message, &config);
        assert!(result.is_ok());

        let (status, _) = result.unwrap();
        assert_eq!(status, 200);

        // Verify nested data was extracted
        assert_eq!(message.data()["user_data"]["name"], json!("Alice"));
    }

    #[test]
    fn test_execute_parse_json_from_data() {
        let mut message = Message::new(Arc::new(json!({})));
        message.context["data"] = json!({
            "existing": {
                "value": 42
            }
        });

        let config = ParseConfig {
            source: "data.existing".to_string(),
            target: "copied".to_string(),
        };

        let result = execute_parse_json(&mut message, &config);
        assert!(result.is_ok());

        // Verify data was copied
        assert_eq!(message.data()["copied"]["value"], json!(42));
    }

    #[test]
    fn test_execute_parse_xml_simple() {
        let xml_payload = json!("<root><name>John</name><age>30</age></root>");
        let mut message = Message::new(Arc::new(xml_payload));

        let config = ParseConfig {
            source: "payload".to_string(),
            target: "parsed".to_string(),
        };

        let result = execute_parse_xml(&mut message, &config);
        assert!(result.is_ok());

        let (status, _) = result.unwrap();
        assert_eq!(status, 200);

        // Verify XML was parsed
        let parsed = &message.data()["parsed"];
        assert!(parsed.is_object());
    }

    #[test]
    fn test_execute_parse_xml_not_string() {
        let payload = json!({"not": "a string"});
        let mut message = Message::new(Arc::new(payload));

        let config = ParseConfig {
            source: "payload".to_string(),
            target: "parsed".to_string(),
        };

        let result = execute_parse_xml(&mut message, &config);
        assert!(result.is_err());
    }

    #[test]
    fn test_xml_to_json_simple() {
        let xml = "<root><name>Test</name></root>";
        let result = xml_to_json(xml);
        assert!(result.is_ok());

        let json = result.unwrap();
        assert!(json.is_object());
    }

    #[test]
    fn test_xml_to_json_invalid() {
        // Test with unclosed tag
        let xml = "<root><unclosed>";
        let result = xml_to_json(xml);
        assert!(result.is_err());
    }

    #[test]
    fn test_xml_to_json_with_attributes() {
        let xml = r#"<person id="123"><name>John</name></person>"#;
        let result = xml_to_json(xml);
        assert!(result.is_ok());
    }

    #[test]
    fn test_xml_to_json_nested() {
        let xml = r#"<root><user><name>Alice</name><email>alice@example.com</email></user></root>"#;
        let result = xml_to_json(xml);
        assert!(result.is_ok());

        let json = result.unwrap();
        assert!(json.is_object());
    }

    #[test]
    fn test_execute_parse_json_from_string_payload() {
        // Simulate WASM layer storing payload as a raw JSON string
        let payload = Value::String(r#"{"name":"John","age":30}"#.to_string());
        let mut message = Message::new(Arc::new(payload));

        let config = ParseConfig {
            source: "payload".to_string(),
            target: "input".to_string(),
        };

        let result = execute_parse_json(&mut message, &config);
        assert!(result.is_ok());

        let (status, _) = result.unwrap();
        assert_eq!(status, 200);

        // Verify the JSON string was parsed into a structured value
        assert_eq!(message.data()["input"]["name"], json!("John"));
        assert_eq!(message.data()["input"]["age"], json!(30));
    }
}