dataflow-rs 4.0.1

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
//! The context, `{data, metadata, temp_data}`, on v3's wire shape, and the
//! reader every value of a wire shape is read back with.
//!
//! The context's reads and writes over a [`Path`](crate::expr::Path) live in
//! `run`, beside the message, since `expr` sits above `base`.

use datavalue::{NumberValue, OwnedDataValue};
use serde::de::{self, MapAccess, SeqAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::collections::HashSet;
use std::fmt;

/// The three roots, in the order a context holds them.
pub(crate) const ROOTS: [&str; 3] = ["data", "metadata", "temp_data"];

/// The context: always `data`, `metadata` and `temp_data`, in that order.
/// Each root starts as an object, and every write keeps it one: `map`,
/// `Ctx::set` and [`Context::set`] refuse anything else at a root.
///
/// Read from JSON, it takes the three roots in any order and writes them in
/// that order, reads a missing one as `{}`, takes any value at each (a
/// trace's redacted root is `null`), and refuses any other key,
/// since expressions evaluate against the whole object and a fourth key
/// would be readable by `{"var": …}`. An object anywhere in it holding a key
/// twice is refused too: the engine reads the first, where a JSON parser
/// keeps the last, so the two would disagree on what the context says.
#[derive(Clone, Debug, PartialEq)]
pub struct Context {
    pub(crate) root: OwnedDataValue,
}

/// A seed for a root: an object as it is, anything else `{}` with capacity
/// `cap`.
fn slot(seed: OwnedDataValue, cap: usize) -> OwnedDataValue {
    if seed.is_object() {
        seed
    } else {
        OwnedDataValue::Object(Vec::with_capacity(cap))
    }
}

impl Context {
    /// A context seeded with `data` and `metadata`, each ignored when it is
    /// not an object, and an empty `temp_data`. `metadata` is sized for the
    /// engine's run stamps and progress object so the first run does not
    /// reallocate it.
    pub(crate) fn new(data: OwnedDataValue, metadata: OwnedDataValue) -> Self {
        Self::from_roots([
            slot(data, 0),
            slot(metadata, 4),
            OwnedDataValue::Object(Vec::new()),
        ])
    }

    /// A context over `root`, the object holding the three roots: a trace's
    /// `initial` and each context its replay rebuilds.
    pub(crate) fn from_root(root: OwnedDataValue) -> Self {
        Self { root }
    }

    /// A context over the three roots' values, in [`ROOTS`]' order. A
    /// literal, not an iterator over `ROOTS`: every message is built through
    /// it, and the zipped iterator cost about 0.5% of `micro_cond_bench`'s
    /// instructions.
    fn from_roots([data, metadata, temp_data]: [OwnedDataValue; 3]) -> Self {
        let [d, m, t] = ROOTS;
        Self::from_root(OwnedDataValue::Object(vec![
            (d.to_owned(), data),
            (m.to_owned(), metadata),
            (t.to_owned(), temp_data),
        ]))
    }

    fn root(&self, key: &str) -> &OwnedDataValue {
        &self.root[key]
    }

    /// The `data` root.
    pub fn data(&self) -> &OwnedDataValue {
        self.root("data")
    }

    /// The `metadata` root.
    pub fn metadata(&self) -> &OwnedDataValue {
        self.root("metadata")
    }

    /// The `temp_data` root.
    pub fn temp_data(&self) -> &OwnedDataValue {
        self.root("temp_data")
    }

    /// The object holding the three roots: what expressions evaluate against.
    pub fn as_value(&self) -> &OwnedDataValue {
        &self.root
    }
}

impl Serialize for Context {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        self.root.serialize(s)
    }
}

impl<'de> Deserialize<'de> for Context {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        d.deserialize_map(Roots)
    }
}

#[cfg(feature = "ts")]
crate::base::ts::ts_decl!(
    Context,
    "Context",
    [
        " The context: `data`, `metadata` and `temp_data`, in that order. Each",
        " root starts as an object; a trace's redacted root is `null`.",
    ],
    |_cfg| {
        use crate::base::ts::{Prop, object};
        object(&[
            Prop::new("data", "JsonValue", &[" The `data` root."]),
            Prop::new("metadata", "JsonValue", &[" The `metadata` root."]),
            Prop::new("temp_data", "JsonValue", &[" The `temp_data` root."]),
        ])
    }
);

/// [`Context`]'s reader: the three roots, by name, once each.
struct Roots;

impl<'de> Visitor<'de> for Roots {
    type Value = Context;

    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("a context: an object holding `data`, `metadata` and `temp_data`")
    }

    fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Context, A::Error> {
        let mut values: [Option<OwnedDataValue>; 3] = [None, None, None];
        while let Some(key) = map.next_key::<String>()? {
            let Some(at) = ROOTS.iter().position(|root| *root == key) else {
                return Err(de::Error::custom(format_args!(
                    "a context holds only `data`, `metadata` and `temp_data`, not `{key}`"
                )));
            };
            if values[at].is_some() {
                return Err(de::Error::duplicate_field(ROOTS[at]));
            }
            values[at] = Some(map.next_value::<ReadValue<false>>()?.0);
        }
        Ok(Context::from_roots(values.map(|value| {
            value.unwrap_or_else(|| OwnedDataValue::Object(Vec::new()))
        })))
    }
}

/// A value read back as v4 wrote it: as [`OwnedDataValue`]'s own reader
/// reads it, except that a number written with a fraction or an exponent
/// stays a float, as `OwnedDataValue::from(&serde_json::Value)` keeps one,
/// and an object holding a key twice is refused. That reader makes `2.0` the
/// integer `2`, which writes back as `2`, so a message holding a
/// whole-number float would not read back byte for byte; and it keeps both
/// pairs of a repeated key, which every path rule reads as one.
pub(crate) fn exact<'de, D: Deserializer<'de>>(d: D) -> Result<OwnedDataValue, D::Error> {
    d.deserialize_any(ValueVisitor::<false>)
}

/// A value read in document order, folding any repeated object key to its
/// first position and last value (as `serde_json::Value` with `preserve_order`
/// reads one).
pub(crate) fn in_order<'de, D: Deserializer<'de>>(d: D) -> Result<OwnedDataValue, D::Error> {
    d.deserialize_any(ValueVisitor::<true>)
}

/// A value read by [`ValueVisitor`].
struct ReadValue<const DEDUP: bool>(OwnedDataValue);

impl<'de, const DEDUP: bool> Deserialize<'de> for ReadValue<DEDUP> {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        d.deserialize_any(ValueVisitor::<DEDUP>).map(Self)
    }
}

/// [`exact`]'s and [`in_order`]'s reader: `OwnedDataValue`'s, keeping floats
/// as floats and either refusing (`DEDUP = false`) or folding (`DEDUP = true`)
/// repeated object keys.
struct ValueVisitor<const DEDUP: bool>;

impl<'de, const DEDUP: bool> Visitor<'de> for ValueVisitor<DEDUP> {
    type Value = OwnedDataValue;

    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("any valid JSON value")
    }

    fn visit_unit<E: de::Error>(self) -> Result<OwnedDataValue, E> {
        Ok(OwnedDataValue::Null)
    }

    fn visit_none<E: de::Error>(self) -> Result<OwnedDataValue, E> {
        Ok(OwnedDataValue::Null)
    }

    fn visit_some<D: Deserializer<'de>>(self, d: D) -> Result<OwnedDataValue, D::Error> {
        d.deserialize_any(Self)
    }

    fn visit_bool<E: de::Error>(self, v: bool) -> Result<OwnedDataValue, E> {
        Ok(OwnedDataValue::Bool(v))
    }

    fn visit_i64<E: de::Error>(self, v: i64) -> Result<OwnedDataValue, E> {
        Ok(OwnedDataValue::Number(NumberValue::Integer(v)))
    }

    fn visit_u64<E: de::Error>(self, v: u64) -> Result<OwnedDataValue, E> {
        Ok(OwnedDataValue::Number(NumberValue::from_u64(v)))
    }

    fn visit_i128<E: de::Error>(self, v: i128) -> Result<OwnedDataValue, E> {
        Ok(OwnedDataValue::Number(match i64::try_from(v) {
            Ok(i) => NumberValue::Integer(i),
            Err(_) => NumberValue::Float(v as f64),
        }))
    }

    fn visit_u128<E: de::Error>(self, v: u128) -> Result<OwnedDataValue, E> {
        Ok(OwnedDataValue::Number(match i64::try_from(v) {
            Ok(i) => NumberValue::Integer(i),
            Err(_) => NumberValue::Float(v as f64),
        }))
    }

    fn visit_f64<E: de::Error>(self, v: f64) -> Result<OwnedDataValue, E> {
        Ok(if v.is_finite() {
            OwnedDataValue::Number(NumberValue::Float(v))
        } else {
            OwnedDataValue::Null
        })
    }

    fn visit_str<E: de::Error>(self, v: &str) -> Result<OwnedDataValue, E> {
        Ok(OwnedDataValue::String(v.to_owned()))
    }

    fn visit_string<E: de::Error>(self, v: String) -> Result<OwnedDataValue, E> {
        Ok(OwnedDataValue::String(v))
    }

    fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<OwnedDataValue, A::Error> {
        let mut items = Vec::with_capacity(seq.size_hint().unwrap_or(0));
        while let Some(ReadValue(item)) = seq.next_element::<ReadValue<DEDUP>>()? {
            items.push(item);
        }
        Ok(OwnedDataValue::Array(items))
    }

    fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<OwnedDataValue, A::Error> {
        let mut pairs = Vec::with_capacity(map.size_hint().unwrap_or(0));
        while let Some((key, ReadValue(value))) = map.next_entry::<String, ReadValue<DEDUP>>()? {
            pairs.push((key, value));
        }
        if DEDUP {
            dedup_object(&mut pairs);
        } else if let Some(key) = repeated_key(&pairs) {
            return Err(de::Error::custom(format_args!(
                "an object holds the key `{key}` twice"
            )));
        }
        Ok(OwnedDataValue::Object(pairs))
    }
}

/// Above this many keys, an object finds a repeated key through a set
/// rather than a scan.
const SCAN: usize = 8;

/// The first key `pairs` holds a second time, if any.
fn repeated_key(pairs: &[(String, OwnedDataValue)]) -> Option<&str> {
    if pairs.len() <= SCAN {
        return pairs
            .iter()
            .enumerate()
            .find(|(i, (key, _))| pairs[..*i].iter().any(|(k, _)| k == key))
            .map(|(_, (key, _))| key.as_str());
    }
    let mut seen = HashSet::with_capacity(pairs.len());
    pairs
        .iter()
        .map(|(key, _)| key.as_str())
        .find(|key| !seen.insert(*key))
}

/// Ensures `pairs` holds each key once: a repeated key keeps its first
/// position and its last value, as `serde_json::Value` reads one.
fn dedup_object(pairs: &mut Vec<(String, OwnedDataValue)>) {
    if repeated_key(pairs).is_some() {
        fold_repeats(pairs);
    }
}

/// `value` with every object in it holding each key once: a repeated key
/// keeps its first position and its last value, as `serde_json::Value`
/// reads one.
pub(crate) fn keep_last(value: &mut OwnedDataValue) {
    match value {
        OwnedDataValue::Object(pairs) => {
            dedup_object(pairs);
            pairs.iter_mut().for_each(|(_, v)| keep_last(v));
        }
        OwnedDataValue::Array(items) => items.iter_mut().for_each(keep_last),
        _ => {}
    }
}

/// [`keep_last`] on one object known to hold a key twice.
#[cold]
fn fold_repeats(pairs: &mut Vec<(String, OwnedDataValue)>) {
    let mut len = 0;
    for i in 0..pairs.len() {
        let (key, value) = std::mem::take(&mut pairs[i]);
        if let Some((_, slot)) = pairs[..len].iter_mut().find(|(k, _)| *k == key) {
            *slot = value;
        } else {
            pairs[len] = (key, value);
            len += 1;
        }
    }
    pairs.truncate(len);
}

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

    fn text(c: &Context) -> String {
        serde_json::to_string(c).unwrap()
    }

    #[test]
    fn a_seed_that_is_not_an_object_is_ignored() {
        let c = Context::new(
            OwnedDataValue::from(&json!([1])),
            OwnedDataValue::from(&json!({"m": 1})),
        );
        assert_eq!(text(&c), r#"{"data":{},"metadata":{"m":1},"temp_data":{}}"#);
    }

    #[test]
    fn a_context_reads_what_it_writes() {
        let wire = r#"{"data":{"b":1,"a":[null]},"metadata":null,"temp_data":"t"}"#;
        let c: Context = serde_json::from_str(wire).unwrap();
        assert_eq!(text(&c), wire);
        assert_eq!(c.metadata(), &OwnedDataValue::Null);
        assert_eq!(
            c,
            Context::from_root(OwnedDataValue::from(&json!({
                "data": {"b": 1, "a": [null]}, "metadata": null, "temp_data": "t"
            })))
        );
    }

    #[test]
    fn a_value_reads_back_as_written_and_as_the_json_bridge_builds_it() {
        let wire =
            r#"[2.0,-3.0,2.5,1e+300,7,-7,9223372036854775807,"s",true,null,{"b":[1.0],"a":{}}]"#;
        let mut de = serde_json::Deserializer::from_str(wire);
        let read = exact(&mut de).unwrap();
        assert_eq!(serde_json::to_string(&read).unwrap(), wire);
        let bridged: serde_json::Value = serde_json::from_str(wire).unwrap();
        assert_eq!(read, OwnedDataValue::from(&bridged));
        // `OwnedDataValue`'s own reader makes the whole floats integers.
        let collapsed: OwnedDataValue = serde_json::from_str(wire).unwrap();
        assert!(
            serde_json::to_string(&collapsed)
                .unwrap()
                .starts_with("[2,-3,2.5,")
        );
        // Above `i64::MAX`, an integer is a float, as the bridge makes it.
        let big = exact(&mut serde_json::Deserializer::from_str(
            "18446744073709551615",
        ))
        .unwrap();
        assert_eq!(big, OwnedDataValue::from(&json!(u64::MAX)));
    }
    #[test]
    fn a_repeated_key_keeps_its_first_position_and_its_last_value() {
        let fold = |text: &str| {
            let mut value = OwnedDataValue::from_json(text).unwrap();
            keep_last(&mut value);
            serde_json::to_string(&value).unwrap()
        };
        assert_eq!(
            fold(r#"{"a":1,"b":2,"a":3,"c":4,"b":5}"#),
            r#"{"a":3,"b":5,"c":4}"#
        );
        // Inside arrays and objects, and inside a repeated key's last value.
        assert_eq!(
            fold(r#"[{"x":{"y":1,"y":2}},{"a":{"q":1},"a":{"q":2,"q":3}}]"#),
            r#"[{"x":{"y":2}},{"a":{"q":3}}]"#
        );
        // Past the scan, through the set.
        let wide: String = (0..12).map(|i| format!(r#""k{i}":{i},"#)).collect();
        let wanted: String = (0..12)
            .map(|i| format!(r#""k{i}":{},"#, if i == 3 { 99 } else { i }))
            .collect();
        assert_eq!(
            fold(&format!(r#"{{{wide}"k3":99}}"#)),
            format!("{{{}}}", wanted.trim_end_matches(','))
        );
        // Nothing repeated, nothing moves.
        let plain = r#"{"b":1,"a":{"b":2},"c":[{"b":3}]}"#;
        assert_eq!(fold(plain), plain);
    }
}