dataflow-rs 4.0.0

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
//! `publish_json`'s and `publish_xml`'s config: what under `data` to
//! serialize, and where under `data` to write the text.

use super::paths::{DataPath, PublishSource};
use super::xml::{validation_error, value_to_xml};
use crate::base::{IssueCode, Result, Severity};
use crate::expr::{CompileCx, Expr, Path};
use crate::function::{FunctionConfig, Outcome, SyncCtx};
use ::log::debug;
use datavalue::OwnedDataValue;
use serde_json::Value;
use std::borrow::Cow;

/// `publish_json`'s and `publish_xml`'s input.
#[derive(Debug, FunctionConfig)]
pub(crate) struct PublishConfig {
    /// What to serialize, relative to `data`, with v3's fallback: a source
    /// beginning with `data.` is also read without that prefix when
    /// `data.<source>` is missing.
    #[config(records)]
    pub source: PublishSource,
    /// Where the text is written, relative to `data`.
    #[config(records)]
    pub target: DataPath,
    /// Indent the JSON (`publish_json` only).
    #[config(default = false)]
    pub pretty: bool,
    /// The XML document's root element (`publish_xml` only).
    #[config(records, default = "root")]
    pub root_element: Expr<String>,
}

impl PublishConfig {
    /// Compiles `input` for `publish_json` (`xml` false) or `publish_xml`:
    /// the other function's own field is ignored, with an Advisory naming
    /// it, so a v3 definition that carried both still runs and a v4 one
    /// learns the field does nothing here.
    pub(crate) fn compile_for(input: &Value, xml: bool, cx: &mut CompileCx<'_>) -> Option<Self> {
        let (other, owner) = if xml {
            ("pretty", "publish_json")
        } else {
            ("root_element", "publish_xml")
        };
        let input = if input.get(other).is_some() {
            let mut owned = input.clone();
            if let Value::Object(object) = &mut owned {
                object.remove(other);
            }
            cx.at(other, |cx| {
                cx.issue_as(
                    IssueCode::UnknownKey,
                    Severity::Advisory,
                    format!("`{other}` is `{owner}`'s field; this call ignores it"),
                );
            });
            Cow::Owned(owned)
        } else {
            Cow::Borrowed(input)
        };
        <Self as FunctionConfig>::compile(&input, cx)
    }

    /// The paths this call reads and writes: the source with its fallback,
    /// then the target.
    #[allow(clippy::type_complexity, reason = "one call site")]
    fn paths<'p>(
        &'p self,
        cx: &mut SyncCtx<'_, '_, 'p>,
    ) -> Result<(Cow<'p, Path>, Option<Cow<'p, Path>>, Cow<'p, Path>)> {
        let (source, fallback) = self.source.paths(cx)?;
        let target = self.target.path(cx)?;
        Ok((source, fallback, target))
    }

    /// `publish_json`: the source serialized to JSON text, compact or, with
    /// `pretty`, indented, both in the source's key order.
    ///
    /// # Errors
    ///
    /// `VALIDATION_ERROR` when the source is missing or `null`.
    pub(crate) fn json<'p>(&'p self, cx: &mut SyncCtx<'_, '_, 'p>) -> Result<Outcome> {
        let (source, fallback, target) = self.paths(cx)?;
        debug!("PublishJson: Serializing '{source}' to '{target}'");
        let text = {
            let found = find(cx, &source, fallback.as_deref(), "PublishJson")?;
            if self.pretty {
                serde_json::to_string_pretty(&*found).map_err(|e| {
                    validation_error(format_args!("Failed to serialize to JSON: {e}"))
                })?
            } else {
                found.to_json_string()
            }
        };
        cx.write(&target, OwnedDataValue::String(text), None);
        Ok(Outcome::Ok)
    }

    /// `publish_xml`: the source serialized to an XML document under
    /// `root_element`, its keys in the source's order.
    ///
    /// # Errors
    ///
    /// `VALIDATION_ERROR` when the source is missing or `null`.
    pub(crate) fn xml<'p>(&'p self, cx: &mut SyncCtx<'_, '_, 'p>) -> Result<Outcome> {
        let (source, fallback, target) = self.paths(cx)?;
        debug!("PublishXml: Serializing '{source}' to '{target}'");
        let xml = match self.root_element.constant() {
            // A constant root reads nothing and cannot fail: the source is
            // serialized where it is found.
            Some(root) => value_to_xml(
                &*find(cx, &source, fallback.as_deref(), "PublishXml")?,
                root,
            ),
            // Owned, as v3's bridge was: the root element is evaluated after
            // the source is found, as in v3, and the evaluation needs the
            // context.
            None => {
                let found = find(cx, &source, fallback.as_deref(), "PublishXml")?.into_owned();
                let root = cx.eval(&self.root_element)?;
                value_to_xml(&found, &root)
            }
        };
        cx.write(&target, OwnedDataValue::String(xml), None);
        Ok(Outcome::Ok)
    }
}

/// The source's value: at `source`, else at the fallback. A missing value and
/// an explicit `null` both fail.
fn find<'a>(
    cx: &'a SyncCtx<'_, '_, '_>,
    source: &Path,
    fallback: Option<&Path>,
    who: &str,
) -> Result<Cow<'a, OwnedDataValue>> {
    match cx
        .rc
        .get(source)
        .or_else(|| fallback.and_then(|f| cx.rc.get(f)))
    {
        Some(found) if !matches!(*found, OwnedDataValue::Null) => Ok(found),
        _ => Err(validation_error(format_args!(
            "{who}: Source '{source}' not found or is null"
        ))),
    }
}

#[cfg(test)]
mod tests {
    use crate::function::builtins::one_task::{Golden, Observed, context_text_after};
    use datavalue::OwnedDataValue;
    use serde_json::{Value, json};

    fn context() -> Value {
        json!({
            "data": {
                "obj": {"a": [1, {"k": "v & <w>"}], "m": null, "t": true, "z": 1},
                "data": {"inner": 1},
                "num": 7, "text": "hi", "nothing": null,
                "kind": "obj", "slot": "out_slot", "tag": "doc"
            },
            "metadata": {"m": 1}
        })
    }

    /// `function` from `obj` to `out`, with `extra`'s keys over those, its
    /// whole observation held by `golden`.
    fn publish(golden: &mut Golden, function: &str, extra: Value) -> Observed {
        let mut input = json!({"source": "obj", "target": "out"});
        for (k, v) in extra.as_object().expect("an object") {
            input[k] = v.clone();
        }
        golden.observe(function, &input, &Value::Null, &context())
    }

    #[test]
    fn publish_json_writes_compact_and_pretty_text() {
        let mut golden = Golden::new("publish", "publish_json_writes_compact_and_pretty_text");
        let compact = publish(&mut golden, "publish_json", json!({}));
        assert_eq!(compact.status, Some(200));
        let pretty = publish(&mut golden, "publish_json", json!({"pretty": true}));
        assert!(
            pretty.context.contains(r#""out":"{\n  \"a\": ["#),
            "{}",
            pretty.context
        );
    }

    #[test]
    fn compact_pretty_and_xml_keep_the_sources_key_order() {
        // Built from JSON text so the keys stay out of order whatever
        // serde_json's map type is. All three write the source's order in
        // every build: v3's default build sorted pretty's and
        // XML's keys.
        let text = r#"{"data":{"obj":{"z":1,"a":{"y":2,"b":[3,{"q":1,"c":2.0}]}}},"metadata":{},"temp_data":{}}"#;
        let context = || OwnedDataValue::from_json(text).expect("JSON");
        let compact = json!({"source": "obj", "target": "out"});
        assert_eq!(
            context_text_after("publish_json", &compact, context()),
            concat!(
                r#"{"data":{"obj":{"z":1,"a":{"y":2,"b":[3,{"q":1,"c":2.0}]}},"#,
                r#""out":"{\"z\":1,\"a\":{\"y\":2,\"b\":[3,{\"q\":1,\"c\":2.0}]}}"},"#,
                r#""metadata":{},"temp_data":{}}"#
            )
        );
        let out = |function: &str, pretty: bool| {
            let input = json!({"source": "obj", "target": "out", "pretty": pretty});
            let written: Value =
                serde_json::from_str(&context_text_after(function, &input, context())).unwrap();
            written["data"]["out"]
                .as_str()
                .expect("a string")
                .to_owned()
        };
        assert_eq!(
            out("publish_json", false),
            r#"{"z":1,"a":{"y":2,"b":[3,{"q":1,"c":2.0}]}}"#
        );
        assert_eq!(
            out("publish_json", true),
            concat!(
                "{\n  \"z\": 1,\n  \"a\": {\n    \"y\": 2,\n    \"b\": [\n      3,\n",
                "      {\n        \"q\": 1,\n        \"c\": 2.0\n      }\n    ]\n  }\n}"
            )
        );
        assert_eq!(
            out("publish_xml", false),
            "<root><z>1</z><a><y>2</y><b><item>3</item><item><q>1</q><c>2.0</c></item></b></a></root>"
        );
    }

    #[test]
    fn publish_json_reads_through_the_data_fallback() {
        let mut golden = Golden::new("publish", "publish_json_reads_through_the_data_fallback");
        // `data.obj`: `data.data.obj` is missing, so `data.obj` is read.
        let observed = publish(&mut golden, "publish_json", json!({"source": "data.obj"}));
        assert_eq!(observed.status, Some(200));
        // `data.inner` finds `data.data.inner` first.
        publish(&mut golden, "publish_json", json!({"source": "data.inner"}));
        publish(&mut golden, "publish_json", json!({"source": "data"}));
        publish(&mut golden, "publish_json", json!({"source": "obj.a.1"}));
        publish(&mut golden, "publish_json", json!({"source": "num"}));
        publish(&mut golden, "publish_json", json!({"source": "text"}));
    }

    #[test]
    fn a_missing_or_null_source_fails_with_the_validation_error() {
        let mut golden = Golden::new(
            "publish",
            "a_missing_or_null_source_fails_with_the_validation_error",
        );
        for (function, source, message) in [
            (
                "publish_json",
                "absent",
                "Validation error: PublishJson: Source 'data.absent' not found or is null",
            ),
            (
                "publish_json",
                "nothing",
                "Validation error: PublishJson: Source 'data.nothing' not found or is null",
            ),
            (
                "publish_json",
                "data.absent",
                "Validation error: PublishJson: Source 'data.data.absent' not found or is null",
            ),
            (
                "publish_xml",
                "absent",
                "Validation error: PublishXml: Source 'data.absent' not found or is null",
            ),
            (
                "publish_xml",
                "nothing",
                "Validation error: PublishXml: Source 'data.nothing' not found or is null",
            ),
        ] {
            let observed = publish(&mut golden, function, json!({"source": source}));
            assert_eq!(observed.status, Some(500), "{function} {source}");
            assert_eq!(observed.error_code.as_deref(), Some("VALIDATION_ERROR"));
            assert!(
                observed
                    .errors
                    .contains(&format!("Task t error: {message}")),
                "{}",
                observed.errors
            );
        }
    }

    #[test]
    fn a_computed_source_and_target_resolve_per_call() {
        let mut golden = Golden::new("publish", "a_computed_source_and_target_resolve_per_call");
        for function in ["publish_json", "publish_xml"] {
            publish(
                &mut golden,
                function,
                json!({"source": {"var": "data.kind"}}),
            );
            publish(
                &mut golden,
                function,
                json!({"source": {"cat": ["data.", {"var": "data.kind"}]}}),
            );
            publish(
                &mut golden,
                function,
                json!({"target": {"var": "data.slot"}}),
            );
            // One that folds is resolved at compile; the answer is the same.
            publish(
                &mut golden,
                function,
                json!({"source": {"cat": ["o", "bj"]}}),
            );
        }
    }

    #[test]
    fn publish_xml_writes_the_document_under_the_root_element() {
        let mut golden = Golden::new(
            "publish",
            "publish_xml_writes_the_document_under_the_root_element",
        );
        let observed = publish(&mut golden, "publish_xml", json!({}));
        assert!(
            observed.context.contains("<root><a><item>1</item>"),
            "{}",
            observed.context
        );
        assert!(observed.context.contains("v &amp; &lt;w&gt;"));
        publish(
            &mut golden,
            "publish_xml",
            json!({"root_element": "envelope"}),
        );
        publish(
            &mut golden,
            "publish_xml",
            json!({"root_element": {"var": "data.tag"}}),
        );
        // Scalars, a list and a key that is no XML name.
        publish(&mut golden, "publish_xml", json!({"source": "num"}));
        publish(&mut golden, "publish_xml", json!({"source": "obj.a"}));
        publish(&mut golden, "publish_xml", json!({"source": "text"}));
    }

    #[test]
    fn a_source_that_exists_under_data_wins_over_the_fallback() {
        let mut golden = Golden::new(
            "publish",
            "a_source_that_exists_under_data_wins_over_the_fallback",
        );
        let context = json!({"data": {"data": {"x": 1}, "x": 2}});
        for function in ["publish_json", "publish_xml"] {
            let observed = golden.observe(
                function,
                &json!({"source": "data.x", "target": "out"}),
                &Value::Null,
                &context,
            );
            assert!(observed.context.contains('1'), "{}", observed.context);
        }
    }

    #[test]
    fn an_empty_remainder_publishes_the_whole_of_data() {
        let mut golden = Golden::new("publish", "an_empty_remainder_publishes_the_whole_of_data");
        let context = json!({"data": {"e": "", "d": "data.", "k": 1, "o": {"x": 1}}});
        for function in ["publish_json", "publish_xml"] {
            for source in [
                json!(""),
                json!("data."),
                json!({"cat": ["data.", ""]}),
                json!({"var": "data.e"}),
                json!({"cat": ["data.", {"var": "data.e"}]}),
                json!({"var": "data.d"}),
            ] {
                let input = json!({"source": source, "target": "out"});
                let observed = golden.observe(function, &input, &Value::Null, &context);
                assert_eq!(observed.status, Some(200), "{function} {source}");
            }
        }
        // A key named `""` wins over the whole of `data`.
        let context = json!({"data": {"": {"only": 1}, "k": 2}});
        let input = json!({"source": "", "target": "out"});
        golden.observe("publish_json", &input, &Value::Null, &context);
    }

    #[test]
    fn a_computed_parameter_that_fails_to_evaluate_records_a_logic_error() {
        let mut golden = Golden::new(
            "publish",
            "a_computed_parameter_that_fails_to_evaluate_records_a_logic_error",
        );
        let context = json!({"data": {"word": "abc", "obj": {"a": 1}}});
        let broken = json!({"+": [{"var": "data.word"}, "x"]});
        for (function, input) in [
            ("publish_json", json!({"source": broken, "target": "out"})),
            ("publish_json", json!({"source": "obj", "target": broken})),
            ("publish_xml", json!({"source": broken, "target": "out"})),
            ("publish_xml", json!({"source": "obj", "target": broken})),
            (
                "publish_xml",
                json!({"source": "obj", "target": "out", "root_element": broken}),
            ),
        ] {
            let observed = golden.observe(function, &input, &Value::Null, &context);
            assert_eq!(observed.status, Some(500), "{function} {input}");
            assert_eq!(observed.error_code.as_deref(), Some("LOGIC_ERROR"));
            assert!(
                observed
                    .errors
                    .contains("Task t error: Logic evaluation error: "),
                "{}",
                observed.errors
            );
        }
    }
}