graphersal 0.1.1

In-memory graph traversal engine modelled on Apache TinkerPop Gremlin, with a Rhai DSL, optimizer and GraphSON/GraphML I/O
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
//! The `P`/`TextP` predicate tokens (`ArgP`, `P.eq(..)`/`P::eq(..)`/`eq(..)`, ...).

use crate::script::*;

/// Scope marker that makes the vanilla dotted spelling `P.eq(...)`/`P.within(...)`/... resolve.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ScriptP;

/// Scope marker for the text-predicate spelling `TextP.startingWith(...)`/....
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ScriptTextP;

/// `TextP.regex(pattern)` / `TextP.notRegex(pattern)`: compiles the expression once, here; an
/// invalid expression is a script error carrying the `ValueError::InvalidRegex` diagnostic.
pub(crate) fn script_text_regex(
    pattern: &str,
    negated: bool,
) -> Result<ArgP, Box<rhai::EvalAltResult>> {
    let regex = crate::value::TextRegex::new(pattern).map_err(script_error)?;
    Ok(if negated {
        ArgP::NotRegex(regex)
    } else {
        ArgP::Regex(regex)
    })
}

pub(crate) fn script_p_within(val: Dynamic) -> ArgP {
    if val.is_array() {
        ArgP::WithIn(val.cast::<rhai::Array>())
    } else {
        ArgP::WithIn(vec![val])
    }
}

pub(crate) fn script_p_without(val: Dynamic) -> ArgP {
    if val.is_array() {
        ArgP::WithOut(val.cast::<rhai::Array>())
    } else {
        ArgP::WithOut(vec![val])
    }
}

/// The predicate as the DSL writes it (`P.gt(1)`, `TextP.containing("a")`,
/// `P.gt(1).and(P.lt(3))`): the engine predicate's canonical rendering, the same text the
/// predicate has inside a step of `.profile()`.
impl crate::value::literal::TokenLiteral for ArgP {
    fn token_literal(&self) -> String {
        crate::value::literal::TokenLiteral::token_literal(&argp_to_p(self))
    }
}

/// A script-level predicate with Rhai operands, before conversion to `P`. Its script type name
/// is `Predicate` (`type_of(P.eq(1))`, diagnostics, function metadata).
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum ArgP {
    /// Equal to.
    Eq(Dynamic),
    /// Not equal to.
    Neq(Dynamic),
    /// Less than.
    Lt(Dynamic),
    /// Greater than.
    Gt(Dynamic),
    /// Less than or equal to.
    Lte(Dynamic),
    /// Greater than or equal to.
    Gte(Dynamic),
    /// Equal to one of the given values.
    WithIn(Vec<Dynamic>),
    /// Equal to none of the given values.
    WithOut(Vec<Dynamic>),
    /// Between two bounds, exclusive.
    Inside(Dynamic, Dynamic),
    /// Outside two bounds, exclusive.
    Outside(Dynamic, Dynamic),
    /// Between two bounds, inclusive of the lower bound.
    Between(Dynamic, Dynamic),
    /// A string starting with the given prefix.
    StartingWith(Dynamic),
    /// A string not starting with the given prefix.
    NotStartingWith(Dynamic),
    /// A string ending with the given suffix.
    EndingWith(Dynamic),
    /// A string not ending with the given suffix.
    NotEndingWith(Dynamic),
    /// A string containing the given substring.
    Containing(Dynamic),
    /// A string not containing the given substring.
    NotContaining(Dynamic),
    /// A string the regular expression matches somewhere (`TextP.regex`).
    Regex(crate::value::TextRegex),
    /// A string the regular expression matches nowhere (`TextP.notRegex`).
    NotRegex(crate::value::TextRegex),
    /// Tests the type a value has at runtime.
    TypeOf(GType),
    /// Both predicates hold (`P.gt(1).and(P.lt(5))`), evaluated left to right.
    And(Box<ArgP>, Box<ArgP>),
    /// At least one predicate holds (`P.lt(1).or(P.gt(5))`), evaluated left to right.
    Or(Box<ArgP>, Box<ArgP>),
    /// The predicate does not hold (`P.not(P.within(1, 2))`, `p.negate()`).
    Not(Box<ArgP>),
}

#[export_module]
#[allow(non_snake_case, non_upper_case_globals)]
pub(crate) mod ArgPModule {
    use super::{ArgP, GType, script_p_within, script_p_without};
    use crate::value::literal::TokenLiteral;

    pub fn eq(val: Dynamic) -> ArgP {
        ArgP::Eq(val)
    }
    pub fn neq(val: Dynamic) -> ArgP {
        ArgP::Neq(val)
    }
    pub fn lt(val: Dynamic) -> ArgP {
        ArgP::Lt(val)
    }
    pub fn gt(val: Dynamic) -> ArgP {
        ArgP::Gt(val)
    }
    pub fn lte(val: Dynamic) -> ArgP {
        ArgP::Lte(val)
    }
    pub fn gte(val: Dynamic) -> ArgP {
        ArgP::Gte(val)
    }
    pub fn within(val: Dynamic) -> ArgP {
        script_p_within(val)
    }
    pub fn without(val: Dynamic) -> ArgP {
        script_p_without(val)
    }
    pub fn inside(val: Dynamic, val1: Dynamic) -> ArgP {
        ArgP::Inside(val, val1)
    }
    pub fn outside(val: Dynamic, val1: Dynamic) -> ArgP {
        ArgP::Outside(val, val1)
    }
    pub fn between(val: Dynamic, val1: Dynamic) -> ArgP {
        ArgP::Between(val, val1)
    }
    pub fn startingWith(val: Dynamic) -> ArgP {
        ArgP::StartingWith(val)
    }
    pub fn starting_with(val: Dynamic) -> ArgP {
        ArgP::StartingWith(val)
    }
    pub fn notStartingWith(val: Dynamic) -> ArgP {
        ArgP::NotStartingWith(val)
    }
    pub fn not_starting_with(val: Dynamic) -> ArgP {
        ArgP::NotStartingWith(val)
    }
    pub fn endingWith(val: Dynamic) -> ArgP {
        ArgP::EndingWith(val)
    }
    pub fn ending_with(val: Dynamic) -> ArgP {
        ArgP::EndingWith(val)
    }
    pub fn notEndingWith(val: Dynamic) -> ArgP {
        ArgP::NotEndingWith(val)
    }
    pub fn not_ending_with(val: Dynamic) -> ArgP {
        ArgP::NotEndingWith(val)
    }
    pub fn containing(val: Dynamic) -> ArgP {
        ArgP::Containing(val)
    }
    pub fn notContaining(val: Dynamic) -> ArgP {
        ArgP::NotContaining(val)
    }
    pub fn not_containing(val: Dynamic) -> ArgP {
        ArgP::NotContaining(val)
    }

    pub fn typeOf(val: GType) -> ArgP {
        ArgP::TypeOf(val)
    }

    /// Return the current variant of `ArgP`.
    #[rhai_fn(global, get = "enum_type", pure)]
    pub fn get_type(arg_p: &mut ArgP) -> String {
        match arg_p {
            ArgP::Eq(_) => "Eq",
            ArgP::Neq(_) => "Neq",
            ArgP::Lt(_) => "Lt",
            ArgP::Gt(_) => "Gt",
            ArgP::Lte(_) => "Lte",
            ArgP::Gte(_) => "Gte",
            ArgP::WithIn(_) => "WithIn",
            ArgP::WithOut(_) => "WithOut",
            ArgP::Inside(_, _) => "Inside",
            ArgP::Outside(_, _) => "Outside",
            ArgP::Between(_, _) => "Between",
            ArgP::StartingWith(_) => "StartingWith",
            ArgP::NotStartingWith(_) => "NotStartingWith",
            ArgP::EndingWith(_) => "EndingWith",
            ArgP::NotEndingWith(_) => "NotEndingWith",
            ArgP::Containing(_) => "Containing",
            ArgP::NotContaining(_) => "NotContaining",
            ArgP::Regex(_) => "Regex",
            ArgP::NotRegex(_) => "NotRegex",
            ArgP::TypeOf(_) => "TypeOf",
            ArgP::And(_, _) => "And",
            ArgP::Or(_, _) => "Or",
            ArgP::Not(_) => "Not",
        }
        .to_string()
    }

    /// Return the inner value.
    #[rhai_fn(global, get = "value", pure)]
    pub fn get_value(arg_p: &mut ArgP) -> Dynamic {
        match arg_p {
            ArgP::Eq(val)
            | ArgP::Neq(val)
            | ArgP::Lt(val)
            | ArgP::Gt(val)
            | ArgP::Lte(val)
            | ArgP::Gte(val)
            | ArgP::StartingWith(val)
            | ArgP::NotStartingWith(val)
            | ArgP::EndingWith(val)
            | ArgP::NotEndingWith(val)
            | ArgP::Containing(val)
            | ArgP::NotContaining(val) => val.clone(),

            ArgP::Inside(a_l, a_r) | ArgP::Outside(a_l, a_r) | ArgP::Between(a_l, a_r) => {
                Dynamic::from((a_l.clone(), a_r.clone()))
            }

            ArgP::WithIn(val) | ArgP::WithOut(val) => Dynamic::from_array(val.clone()),

            ArgP::TypeOf(target) => Dynamic::from(target.clone()),

            ArgP::Regex(regex) | ArgP::NotRegex(regex) => Dynamic::from(regex.as_str().to_string()),

            ArgP::And(a, b) | ArgP::Or(a, b) => {
                Dynamic::from((Dynamic::from((**a).clone()), Dynamic::from((**b).clone())))
            }

            ArgP::Not(inner) => Dynamic::from((**inner).clone()),
        }
    }

    // The `///` text is the DSL doc (`script/register/docs_tests.rs` checks it against `docs.rs`).

    /// The complement of the predicate (see the book page Predicates for the deviation from TinkerPop).
    ///
    /// Example: `g.v().has("age", P.gt(30).negate()).values("name").to_list()` returns `["marko", "vadas"]`
    #[rhai_fn(global, pure)]
    pub fn negate(arg_p: &mut ArgP) -> ArgP {
        ArgP::Not(Box::new(arg_p.clone()))
    }

    /// Printing
    #[rhai_fn(global, name = "to_string", name = "to_debug", pure)]
    pub fn to_string(arg_p: &mut ArgP) -> String {
        arg_p.token_literal()
    }
}

/// The PascalCase twins of `ArgPModule` (`P::Eq`-style), registered only in the
/// static module, never as bare globals.
#[export_module]
#[allow(non_snake_case)]
pub(crate) mod ArgPPascalModule {
    use super::{ArgP, GType, script_p_within, script_p_without};

    pub fn Eq(val: Dynamic) -> ArgP {
        ArgP::Eq(val)
    }
    pub fn Neq(val: Dynamic) -> ArgP {
        ArgP::Neq(val)
    }
    pub fn Lt(val: Dynamic) -> ArgP {
        ArgP::Lt(val)
    }
    pub fn Gt(val: Dynamic) -> ArgP {
        ArgP::Gt(val)
    }
    pub fn Lte(val: Dynamic) -> ArgP {
        ArgP::Lte(val)
    }
    pub fn Gte(val: Dynamic) -> ArgP {
        ArgP::Gte(val)
    }
    pub fn WithIn(val: Dynamic) -> ArgP {
        script_p_within(val)
    }
    pub fn WithOut(val: Dynamic) -> ArgP {
        script_p_without(val)
    }
    pub fn Inside(val: Dynamic, val1: Dynamic) -> ArgP {
        ArgP::Inside(val, val1)
    }
    pub fn Outside(val: Dynamic, val1: Dynamic) -> ArgP {
        ArgP::Outside(val, val1)
    }
    pub fn Between(val: Dynamic, val1: Dynamic) -> ArgP {
        ArgP::Between(val, val1)
    }
    pub fn StartingWith(val: Dynamic) -> ArgP {
        ArgP::StartingWith(val)
    }
    pub fn NotStartingWith(val: Dynamic) -> ArgP {
        ArgP::NotStartingWith(val)
    }
    pub fn EndingWith(val: Dynamic) -> ArgP {
        ArgP::EndingWith(val)
    }
    pub fn NotEndingWith(val: Dynamic) -> ArgP {
        ArgP::NotEndingWith(val)
    }
    pub fn Containing(val: Dynamic) -> ArgP {
        ArgP::Containing(val)
    }
    pub fn NotContaining(val: Dynamic) -> ArgP {
        ArgP::NotContaining(val)
    }
    pub fn TypeOf(val: GType) -> ArgP {
        ArgP::TypeOf(val)
    }
}

#[export_module]
#[allow(non_snake_case, non_upper_case_globals)]
pub(crate) mod TextPModule {
    use super::{ArgP, script_text_regex};

    #[rhai_fn(return_raw)]
    pub fn regex(pattern: &str) -> Result<ArgP, Box<rhai::EvalAltResult>> {
        script_text_regex(pattern, false)
    }
    #[rhai_fn(return_raw)]
    pub fn notRegex(pattern: &str) -> Result<ArgP, Box<rhai::EvalAltResult>> {
        script_text_regex(pattern, true)
    }
    #[rhai_fn(return_raw)]
    pub fn not_regex(pattern: &str) -> Result<ArgP, Box<rhai::EvalAltResult>> {
        script_text_regex(pattern, true)
    }

    pub fn startingWith(val: Dynamic) -> ArgP {
        ArgP::StartingWith(val)
    }
    pub fn starting_with(val: Dynamic) -> ArgP {
        ArgP::StartingWith(val)
    }
    pub fn notStartingWith(val: Dynamic) -> ArgP {
        ArgP::NotStartingWith(val)
    }
    pub fn not_starting_with(val: Dynamic) -> ArgP {
        ArgP::NotStartingWith(val)
    }
    pub fn endingWith(val: Dynamic) -> ArgP {
        ArgP::EndingWith(val)
    }
    pub fn ending_with(val: Dynamic) -> ArgP {
        ArgP::EndingWith(val)
    }
    pub fn notEndingWith(val: Dynamic) -> ArgP {
        ArgP::NotEndingWith(val)
    }
    pub fn not_ending_with(val: Dynamic) -> ArgP {
        ArgP::NotEndingWith(val)
    }
    pub fn containing(val: Dynamic) -> ArgP {
        ArgP::Containing(val)
    }
    pub fn notContaining(val: Dynamic) -> ArgP {
        ArgP::NotContaining(val)
    }
    pub fn not_containing(val: Dynamic) -> ArgP {
        ArgP::NotContaining(val)
    }
}

/// The PascalCase twins of `TextPModule` (`TextP::Eq`-style), registered only in the
/// static module, never as bare globals.
#[export_module]
#[allow(non_snake_case)]
pub(crate) mod TextPPascalModule {
    use super::ArgP;

    pub fn StartingWith(val: Dynamic) -> ArgP {
        ArgP::StartingWith(val)
    }
    pub fn NotStartingWith(val: Dynamic) -> ArgP {
        ArgP::NotStartingWith(val)
    }
    pub fn EndingWith(val: Dynamic) -> ArgP {
        ArgP::EndingWith(val)
    }
    pub fn NotEndingWith(val: Dynamic) -> ArgP {
        ArgP::NotEndingWith(val)
    }
    pub fn Containing(val: Dynamic) -> ArgP {
        ArgP::Containing(val)
    }
    pub fn NotContaining(val: Dynamic) -> ArgP {
        ArgP::NotContaining(val)
    }
}