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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! The path fields parse and publish keep from v3: a parse
//! source, which may name the payload; a path written relative to `data`; and
//! a publish source, which is one with v3's `data.` fallback.
//!
//! Each has one resolver, which reads a literal at compile and a computed
//! value at run time, so the two can never disagree. Each takes a string or
//! JSONLogic computing one: an expression that folds to anything but a string
//! is `INPUT_PARSE`, as an `Expr<Path>` and a runtime `Expr(String)` field
//! refuse it.

use crate::base::{IssueCode, Result};
use crate::expr::{CompileCx, ConfigField, Expr, FieldKind, Path, Resolver, ValueKind, Visit};
use crate::function::SyncCtx;
use datavalue::OwnedDataValue;
use serde_json::Value;
use std::borrow::Cow;
use std::str::FromStr;

/// Compiles a path field: its expression and, when the expression folds, its
/// constant through `resolve`.
///
/// The constant must be a string. One that is not, or that `resolve` refuses,
/// is reported as `Expr<Path>` reports it: `INVALID_PATH` for a literal
/// string, `INPUT_PARSE` for anything else.
fn compile_path<T>(
    raw: &Value,
    cx: &mut CompileCx<'_>,
    resolve: fn(&str) -> Result<T>,
) -> Option<(Expr<String>, Option<T>)> {
    if matches!(raw, Value::Null | Value::Bool(_) | Value::Number(_)) {
        cx.issue(
            IssueCode::InputParse,
            format!("`{raw}` must be a path string, or JSONLogic computing one"),
        );
        return None;
    }
    let expr = <Expr<String>>::compile(raw, cx)?;
    let Some(text) = expr.constant() else {
        return Some((expr, None));
    };
    // `Expr<String>` lends a folded non-string as its JSON text, so `[1]`
    // would read as the name "[1]". A literal string is a string; an object
    // that folded is evaluated once in the arena to see whether it folded to
    // a string.
    if !raw.is_string() && !(raw.is_object() && folds_to_string(expr.logic(), cx)) {
        cx.issue(
            IssueCode::InputParse,
            format!("`{raw}` must be a path string, or JSONLogic computing one"),
        );
        return None;
    }
    match resolve(text) {
        Ok(resolved) => Some((expr, Some(resolved))),
        Err(error) => {
            let code = if raw.is_string() {
                IssueCode::InvalidPath
            } else {
                IssueCode::InputParse
            };
            cx.issue(code, error.message().to_owned());
            None
        }
    }
}

/// Whether `raw`, which compiled to `logic` and folded, folded to a string.
fn folds_to_string(logic: &datalogic_rs::Logic, cx: &CompileCx<'_>) -> bool {
    crate::expr::arena::with_arena(|arena| {
        cx.exprs()
            .logic()
            .evaluate(logic, datavalue::DataValue::Object(&[]), arena)
            .is_ok_and(|v| matches!(v, datavalue::DataValue::String(_)))
    })
}

/// Where a parse reads from, resolved: the one form the runtime reads.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum ParseFrom {
    /// `"payload"`: the whole payload.
    Payload,
    /// `"payload.<path>"`: `<path>` split by [`Path`]'s segment rules and held
    /// under the `data` root, which stands for the payload.
    PayloadPath(Path),
    /// Any other name: a context path.
    Context(Path),
}

impl ParseFrom {
    /// The one resolver: `"payload"`, `"payload.<path>"`, or a context path.
    ///
    /// # Errors
    ///
    /// A name that is none of those, such as `extra.x`, whose root is not
    /// `data`, `metadata` or `temp_data`.
    pub(crate) fn parse(s: &str) -> Result<Self> {
        // v3's empty remainder names the root it follows: `get_nested_value(x, "")` is `x`.
        if s == "payload" || s == "payload." {
            return Ok(Self::Payload);
        }
        // The same rule for every context root, so `metadata.` and
        // `temp_data.` name their root as `data.` always has.
        if let Some(root) = s.strip_suffix('.')
            && crate::base::ROOTS.contains(&root)
        {
            return Path::from_str(root).map(Self::Context);
        }
        if let Some(rest) = s.strip_prefix("payload.") {
            return Path::from_str(&format!("data.{rest}")).map(Self::PayloadPath);
        }
        Path::from_str(s).map(Self::Context)
    }

    /// The name it was parsed from, as a message names the source.
    pub(crate) fn text(&self) -> String {
        match self {
            Self::Payload => "payload".to_owned(),
            Self::PayloadPath(path) => {
                let dotted = path.dotted();
                format!("payload.{}", dotted.strip_prefix("data.").unwrap_or(dotted))
            }
            Self::Context(path) => path.dotted().to_string(),
        }
    }
}

/// Where `parse_json` and `parse_xml` read from.
///
/// A literal string resolves at compile through `ParseFrom::parse`, and so
/// does JSONLogic that folds, so a constant is not parsed per call. Anything
/// else compiles as JSONLogic computing that string, which resolves through
/// the same function at run time, so a computed source can name the payload
/// too.
#[derive(Debug)]
#[non_exhaustive]
pub enum ParseSource {
    /// A literal string, resolved at compile. Its `visit` rebuilds the text.
    Literal(ParseFrom),
    /// JSONLogic that folded, resolved at compile and never again. It keeps
    /// the authored JSON, which `visit` reports.
    Folded {
        /// The authored JSONLogic.
        raw: Value,
        /// What it folded to.
        from: ParseFrom,
    },
    /// JSONLogic computing the source's name, resolved per call.
    Computed(Expr<String>),
}

impl ParseSource {
    /// The one resolver for a literal and a computed value.
    ///
    /// # Errors
    ///
    /// The evaluation's, or a computed name that is no source.
    pub(crate) fn resolve<'p>(
        &'p self,
        cx: &mut SyncCtx<'_, '_, 'p>,
    ) -> Result<Cow<'p, ParseFrom>> {
        match self {
            Self::Literal(from) | Self::Folded { from, .. } => Ok(Cow::Borrowed(from)),
            Self::Computed(expr) => ParseFrom::parse(&cx.eval(expr)?).map(Cow::Owned),
        }
    }
}

impl ConfigField for ParseSource {
    fn compile(raw: &Value, cx: &mut CompileCx<'_>) -> Option<Self> {
        let (expr, resolved) = compile_path(raw, cx, ParseFrom::parse)?;
        Some(match resolved {
            Some(from) if raw.is_string() => Self::Literal(from),
            Some(from) => Self::Folded {
                raw: raw.clone(),
                from,
            },
            None => Self::Computed(expr),
        })
    }

    /// Reports the authored JSON. A literal is rebuilt from its resolved
    /// form, which keeps the text it was parsed from.
    fn visit(&self, v: &mut Visit<'_>) {
        match self {
            Self::Literal(from) => v.expr(&Value::from(from.text())),
            Self::Folded { raw, .. } => v.expr(raw),
            Self::Computed(expr) => v.expr(expr.raw()),
        }
    }

    /// The source's name: the one resolved at compile, as its text, or the
    /// one computed for this call.
    fn resolve(&self, r: &mut dyn Resolver) -> Result<Option<OwnedDataValue>> {
        match self {
            Self::Literal(from) | Self::Folded { from, .. } => {
                Ok(Some(OwnedDataValue::String(from.text())))
            }
            Self::Computed(expr) => ConfigField::resolve(expr, r),
        }
    }

    fn kind() -> FieldKind {
        FieldKind::Expr(ValueKind::String)
    }
}

/// A path written relative to `data`: `"orders"` is `data.orders`, and
/// `"data.t"` is `data.data.t`, as v3's `target` always was.
///
/// A literal resolves at compile; JSONLogic that does not fold resolves
/// through `DataPath::resolve` at run time.
#[derive(Debug)]
pub struct DataPath {
    expr: Expr<String>,
    constant: Option<Path>,
}

impl DataPath {
    /// The path, when it is literal or folds, resolved at compile.
    #[cfg(test)]
    pub fn constant(&self) -> Option<&Path> {
        self.constant.as_ref()
    }

    /// The expression computing the name below `data`.
    #[cfg(test)]
    pub(crate) fn expr(&self) -> &Expr<String> {
        &self.expr
    }

    /// The path for this call: the one resolved at compile, or the computed
    /// name through [`Self::resolve`].
    ///
    /// # Errors
    ///
    /// The evaluation's.
    pub(crate) fn path<'p>(&'p self, cx: &mut SyncCtx<'_, '_, 'p>) -> Result<Cow<'p, Path>> {
        match &self.constant {
            Some(path) => Ok(Cow::Borrowed(path)),
            None => Self::resolve(&cx.eval(&self.expr)?).map(Cow::Owned),
        }
    }

    /// The one resolver, for a literal at compile and a computed value at run
    /// time: `data.<s>`.
    ///
    /// # Errors
    ///
    /// `data.<s>` that is not a [`Path`]. Every name under `data` is one, as
    /// every name was in v3, so none is refused today.
    pub(crate) fn resolve(s: &str) -> Result<Path> {
        Path::from_str(&format!("data.{s}"))
    }
}

impl ConfigField for DataPath {
    fn compile(raw: &Value, cx: &mut CompileCx<'_>) -> Option<Self> {
        let (expr, constant) = compile_path(raw, cx, Self::resolve)?;
        Some(Self { expr, constant })
    }

    fn visit(&self, v: &mut Visit<'_>) {
        v.expr(self.expr.raw());
    }

    /// The name below `data`, as written or as computed for this call.
    fn resolve(&self, r: &mut dyn Resolver) -> Result<Option<OwnedDataValue>> {
        ConfigField::resolve(&self.expr, r)
    }

    fn kind() -> FieldKind {
        FieldKind::Expr(ValueKind::String)
    }
}

/// A publish source: rooted at `data`, as a [`DataPath`] is, plus v3's
/// fallback. A source beginning with `data.` also names the source without
/// that prefix, read when `data.<source>` is missing: `"data.obj"` reads
/// `data.data.obj`, else `data.obj`.
///
/// A literal resolves at compile; JSONLogic that does not fold resolves
/// through `PublishSource::resolve` at run time.
#[derive(Debug)]
pub struct PublishSource {
    path: DataPath,
    fallback: Option<Path>,
}

impl PublishSource {
    /// The primary path, when it is literal or folds, resolved at compile.
    #[cfg(test)]
    pub fn constant(&self) -> Option<&Path> {
        self.path.constant()
    }

    /// The fallback, when the source is literal (or folds) and begins with
    /// `data.`.
    #[cfg(test)]
    pub fn fallback(&self) -> Option<&Path> {
        self.fallback.as_ref()
    }

    /// The expression computing the name below `data`.
    #[cfg(test)]
    pub(crate) fn expr(&self) -> &Expr<String> {
        self.path.expr()
    }

    /// The primary path and the fallback for this call: the ones resolved at
    /// compile, or the computed name through [`Self::resolve`].
    ///
    /// # Errors
    ///
    /// The evaluation's.
    pub(crate) fn paths<'p>(
        &'p self,
        cx: &mut SyncCtx<'_, '_, 'p>,
    ) -> Result<(Cow<'p, Path>, Option<Cow<'p, Path>>)> {
        match &self.path.constant {
            Some(path) => Ok((
                Cow::Borrowed(path),
                self.fallback.as_ref().map(Cow::Borrowed),
            )),
            None => {
                let (path, fallback) = Self::resolve(&cx.eval(&self.path.expr)?)?;
                Ok((Cow::Owned(path), fallback.map(Cow::Owned)))
            }
        }
    }

    /// The one resolver, for a literal at compile and a computed value at run
    /// time: the primary path, `DataPath::resolve(s)`, and the fallback when
    /// `s` begins with `data.`.
    ///
    /// The fallback is the rest of `s` read as a [`DataPath`] is, which is
    /// where v3's lookup of the stripped name read: `data.<rest>`. A rest that
    /// is not a path gives no fallback and no error, as v3's lookup simply
    /// missed; none is refused today.
    ///
    /// # Errors
    ///
    /// The primary's, as `DataPath::resolve`'s.
    pub(crate) fn resolve(s: &str) -> Result<(Path, Option<Path>)> {
        let primary = DataPath::resolve(s)?;
        // v3's empty remainder is `data` itself (`get_nested_value(data, "")`),
        // so a source of `""` or `"data."` falls back to the whole of `data`.
        let fallback = match s.strip_prefix("data.") {
            _ if s.is_empty() => Some(primary.prefix(1)),
            Some("") => Some(primary.prefix(1)),
            Some(rest) => DataPath::resolve(rest).ok(),
            None => None,
        };
        Ok((primary, fallback))
    }
}

impl ConfigField for PublishSource {
    fn compile(raw: &Value, cx: &mut CompileCx<'_>) -> Option<Self> {
        let (expr, resolved) = compile_path(raw, cx, Self::resolve)?;
        let (constant, fallback) =
            resolved.map_or((None, None), |(path, fallback)| (Some(path), fallback));
        Some(Self {
            path: DataPath { expr, constant },
            fallback,
        })
    }

    fn visit(&self, v: &mut Visit<'_>) {
        self.path.visit(v);
    }

    /// The source's name, as a [`DataPath`]'s.
    fn resolve(&self, r: &mut dyn Resolver) -> Result<Option<OwnedDataValue>> {
        ConfigField::resolve(&self.path, r)
    }

    fn kind() -> FieldKind {
        FieldKind::Expr(ValueKind::String)
    }
}

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

    /// Compiles `raw` as one field `T`, with no issue.
    fn field<T: ConfigField>(raw: &Value) -> T {
        let engine = ExprEngine::default();
        let mut issues = Vec::new();
        let value = {
            let mut cx = CompileCx::new(&engine, &mut issues);
            T::compile(raw, &mut cx)
        };
        assert!(issues.is_empty(), "{raw}: {issues:?}");
        value.expect("compiles")
    }

    /// What `visit` reports for `field`.
    fn reported(field: &impl ConfigField) -> Vec<Value> {
        struct Seen(Vec<Value>);
        impl crate::expr::ExprVisitor for Seen {
            fn expr(&mut self, _: &crate::base::AuthoredPath, raw: &Value, _: bool) {
                self.0.push(raw.clone());
            }
        }
        let mut seen = Seen(Vec::new());
        field.visit(&mut Visit::new(
            &mut seen,
            crate::base::AuthoredPath::root(),
        ));
        seen.0
    }

    #[test]
    fn every_context_roots_trailing_dot_names_the_root() {
        // v3's "empty remainder names the root" rule, for all three roots:
        // before CR-7 only `data.` was special-cased.
        for (text, root) in [
            ("data.", "data"),
            ("metadata.", "metadata"),
            ("temp_data.", "temp_data"),
        ] {
            assert!(
                matches!(
                    ParseFrom::parse(text).unwrap(),
                    ParseFrom::Context(path) if path.to_string() == root
                ),
                "{text}"
            );
        }
        assert!(matches!(
            ParseFrom::parse("payload.").unwrap(),
            ParseFrom::Payload
        ));
    }

    #[test]
    fn a_folded_computed_path_resolves_at_compile() {
        // JSONLogic that folds; none of it reads the message.
        let source = json!({"cat": ["payload.", "x.y"]});
        let parse = field::<ParseSource>(&source);
        assert!(
            matches!(
                &parse,
                ParseSource::Folded { raw, from: ParseFrom::PayloadPath(p) }
                    if raw == &source && p.to_string() == "data.x.y"
            ),
            "{parse:?}"
        );
        assert_eq!(reported(&parse), [source]);

        let target = json!({"cat": ["or", "ders"]});
        let data = field::<DataPath>(&target);
        assert_eq!(
            data.constant().map(ToString::to_string).as_deref(),
            Some("data.orders")
        );
        assert_eq!(reported(&data), [target]);

        let origin = json!({"cat": ["data.", "obj"]});
        let publish = field::<PublishSource>(&origin);
        assert_eq!(
            publish.constant().map(ToString::to_string).as_deref(),
            Some("data.data.obj")
        );
        assert_eq!(
            publish.fallback().map(ToString::to_string).as_deref(),
            Some("data.obj")
        );
        assert_eq!(reported(&publish), [origin]);

        // One that reads the message is not folded, and stays computed.
        let computed = json!({"cat": ["payload.", {"var": "data.k"}]});
        let parse = field::<ParseSource>(&computed);
        assert!(matches!(parse, ParseSource::Computed(_)), "{parse:?}");
        assert_eq!(reported(&parse), std::slice::from_ref(&computed));
        assert!(field::<DataPath>(&computed).constant().is_none());
        assert!(field::<PublishSource>(&computed).constant().is_none());
    }
}