1use crate::conversion::{FromGValue, ToGValue};
2use crate::process::traversal::step::by::ByStep;
3use crate::process::traversal::step::choose::IntoChooseStep;
4use crate::process::traversal::step::coalesce::CoalesceStep;
5use crate::process::traversal::step::dedup::DedupStep;
6use crate::process::traversal::step::from::FromStep;
7use crate::process::traversal::step::has::HasStep;
8use crate::process::traversal::step::limit::LimitStep;
9use crate::process::traversal::step::local::LocalStep;
10use crate::process::traversal::step::loops::LoopsStep;
11use crate::process::traversal::step::match_step::MatchStep;
12use crate::process::traversal::step::not::NotStep;
13use crate::process::traversal::step::or::OrStep;
14use crate::process::traversal::step::repeat::RepeatStep;
15use crate::process::traversal::step::select::SelectStep;
16use crate::process::traversal::step::side_effect::IntoSideEffectStep;
17use crate::process::traversal::step::to::ToStep;
18use crate::process::traversal::step::until::UntilStep;
19use crate::process::traversal::step::where_step::WhereStep;
20
21use crate::process::traversal::{Bytecode, Scope};
22use crate::structure::{Cardinality, GIDs, IntoPredicate, Labels};
23use crate::GValue;
24
25#[derive(Clone)]
26pub struct TraversalBuilder {
27 pub(crate) bytecode: Bytecode,
28}
29
30impl Default for TraversalBuilder {
31 fn default() -> Self {
32 TraversalBuilder {
33 bytecode: Bytecode::default(),
34 }
35 }
36}
37
38impl TraversalBuilder {
39 pub fn new(bytecode: Bytecode) -> Self {
40 TraversalBuilder { bytecode }
41 }
42 pub fn bytecode(&self) -> &Bytecode {
43 &self.bytecode
44 }
45
46 pub fn v<T>(mut self, ids: T) -> TraversalBuilder
47 where
48 T: Into<GIDs>,
49 {
50 self.bytecode.add_step(
51 String::from("V"),
52 ids.into().0.iter().map(|id| id.to_gvalue()).collect(),
53 );
54 self
55 }
56
57 pub fn e<T>(mut self, ids: T) -> TraversalBuilder
58 where
59 T: Into<GIDs>,
60 {
61 self.bytecode.add_step(
62 String::from("E"),
63 ids.into().0.iter().map(|id| id.to_gvalue()).collect(),
64 );
65 self
66 }
67
68 pub fn has_label<L>(mut self, labels: L) -> Self
69 where
70 L: Into<Labels>,
71 {
72 self.bytecode.add_step(
73 String::from("hasLabel"),
74 labels.into().0.into_iter().map(GValue::from).collect(),
75 );
76 self
77 }
78
79 pub fn add_v<A>(mut self, label: A) -> Self
80 where
81 A: Into<Labels>,
82 {
83 self.bytecode.add_step(
84 String::from("addV"),
85 label.into().0.into_iter().map(GValue::from).collect(),
86 );
87
88 self
89 }
90
91 pub fn property<K, A>(mut self, key: K, value: A) -> Self
92 where
93 K: Into<GValue>,
94 A: Into<GValue>,
95 {
96 self.bytecode
97 .add_step(String::from("property"), vec![key.into(), value.into()]);
98 self
99 }
100
101 pub fn property_many<A>(mut self, values: Vec<(String, A)>) -> Self
102 where
103 A: Into<GValue>,
104 {
105 for property in values {
106 self.bytecode.add_step(
107 String::from("property"),
108 vec![property.0.into(), property.1.into()],
109 )
110 }
111
112 self
113 }
114
115 pub fn property_with_cardinality<A>(
116 mut self,
117 cardinality: Cardinality,
118 key: &str,
119 value: A,
120 ) -> Self
121 where
122 A: Into<GValue>,
123 {
124 self.bytecode.add_step(
125 String::from("property"),
126 vec![cardinality.into(), String::from(key).into(), value.into()],
127 );
128 self
129 }
130
131 pub fn has<A>(mut self, step: A) -> Self
132 where
133 A: Into<HasStep>,
134 {
135 self.bytecode
136 .add_step(String::from("has"), step.into().into());
137 self
138 }
139
140 pub fn side_effect<A>(mut self, step: A) -> Self
141 where
142 A: IntoSideEffectStep,
143 {
144 self.bytecode
145 .add_step(String::from("sideEffect"), step.into_step());
146 self
147 }
148
149 pub fn with_side_effect<A>(mut self, step: (&'static str, A)) -> Self
150 where
151 A: Into<GValue> + FromGValue,
152 {
153 self.bytecode.add_source(
154 String::from("withSideEffect"),
155 vec![step.0.into(), step.1.into()],
156 );
157 self
158 }
159
160 pub fn has_many<A>(mut self, steps: Vec<A>) -> Self
161 where
162 A: Into<HasStep>,
163 {
164 for step in steps {
165 self.bytecode
166 .add_step(String::from("has"), step.into().into());
167 }
168 self
169 }
170
171 pub fn has_not<A>(mut self, key: A) -> Self
172 where
173 A: Into<String>,
174 {
175 self.bytecode
176 .add_step(String::from("hasNot"), vec![key.into().into()]);
177 self
178 }
179 pub fn as_<A>(mut self, alias: A) -> Self
180 where
181 A: Into<String>,
182 {
183 self.bytecode
184 .add_step(String::from("as"), vec![alias.into().into()]);
185
186 self
187 }
188
189 pub fn add_e<A>(mut self, label: A) -> Self
190 where
191 A: Into<Labels>,
192 {
193 self.bytecode.add_step(
194 String::from("addE"),
195 label.into().0.into_iter().map(GValue::from).collect(),
196 );
197
198 self
199 }
200
201 pub fn out<A>(mut self, labels: A) -> Self
202 where
203 A: Into<Labels>,
204 {
205 self.bytecode.add_step(
206 String::from("out"),
207 labels.into().0.into_iter().map(GValue::from).collect(),
208 );
209
210 self
211 }
212
213 pub fn out_e<A>(mut self, labels: A) -> Self
214 where
215 A: Into<Labels>,
216 {
217 self.bytecode.add_step(
218 String::from("outE"),
219 labels.into().0.into_iter().map(GValue::from).collect(),
220 );
221
222 self
223 }
224
225 pub fn out_v(mut self) -> Self {
226 self.bytecode.add_step(String::from("outV"), vec![]);
227
228 self
229 }
230 pub fn in_<A>(mut self, labels: A) -> Self
231 where
232 A: Into<Labels>,
233 {
234 self.bytecode.add_step(
235 String::from("in"),
236 labels.into().0.into_iter().map(GValue::from).collect(),
237 );
238
239 self
240 }
241
242 pub fn in_e<A>(mut self, labels: A) -> Self
243 where
244 A: Into<Labels>,
245 {
246 self.bytecode.add_step(
247 String::from("inE"),
248 labels.into().0.into_iter().map(GValue::from).collect(),
249 );
250
251 self
252 }
253
254 pub fn in_v(mut self) -> Self {
255 self.bytecode.add_step(String::from("inV"), vec![]);
256
257 self
258 }
259
260 pub fn both<A>(mut self, labels: A) -> Self
261 where
262 A: Into<Labels>,
263 {
264 self.bytecode.add_step(
265 String::from("both"),
266 labels.into().0.into_iter().map(GValue::from).collect(),
267 );
268
269 self
270 }
271
272 pub fn both_e<A>(mut self, labels: A) -> Self
273 where
274 A: Into<Labels>,
275 {
276 self.bytecode.add_step(
277 String::from("bothE"),
278 labels.into().0.into_iter().map(GValue::from).collect(),
279 );
280
281 self
282 }
283
284 pub fn other(mut self) -> Self {
285 self.bytecode.add_step(String::from("other"), vec![]);
286
287 self
288 }
289
290 pub fn other_v(mut self) -> Self {
291 self.bytecode.add_step(String::from("otherV"), vec![]);
292
293 self
294 }
295
296 pub fn label(mut self) -> Self {
297 self.bytecode.add_step(String::from("label"), vec![]);
298
299 self
300 }
301
302 pub fn from<A>(mut self, step: A) -> Self
303 where
304 A: Into<FromStep>,
305 {
306 self.bytecode
307 .add_step(String::from("from"), step.into().into());
308
309 self
310 }
311
312 pub fn to<A>(mut self, step: A) -> Self
313 where
314 A: Into<ToStep>,
315 {
316 self.bytecode
317 .add_step(String::from("to"), step.into().into());
318
319 self
320 }
321
322 pub fn properties<L>(mut self, labels: L) -> Self
323 where
324 L: Into<Labels>,
325 {
326 self.bytecode.add_step(
327 String::from("properties"),
328 labels.into().0.into_iter().map(GValue::from).collect(),
329 );
330 self
331 }
332
333 pub fn property_map<L>(mut self, labels: L) -> Self
334 where
335 L: Into<Labels>,
336 {
337 self.bytecode.add_step(
338 String::from("propertyMap"),
339 labels.into().0.into_iter().map(GValue::from).collect(),
340 );
341 self
342 }
343
344 pub fn values<L>(mut self, labels: L) -> Self
345 where
346 L: Into<Labels>,
347 {
348 self.bytecode.add_step(
349 String::from("values"),
350 labels.into().0.into_iter().map(GValue::from).collect(),
351 );
352 self
353 }
354
355 pub fn value_map<L>(mut self, labels: L) -> Self
356 where
357 L: Into<Labels>,
358 {
359 self.bytecode.add_step(
360 String::from("valueMap"),
361 labels.into().0.into_iter().map(GValue::from).collect(),
362 );
363 self
364 }
365
366 pub fn element_map<L>(mut self, labels: L) -> Self
367 where
368 L: Into<Labels>,
369 {
370 self.bytecode.add_step(
371 String::from("elementMap"),
372 labels.into().0.into_iter().map(GValue::from).collect(),
373 );
374 self
375 }
376
377 pub fn count(mut self) -> Self {
378 self.bytecode.add_step(String::from("count"), vec![]);
379 self
380 }
381
382 pub fn group_count(mut self, key: Option<String>) -> Self {
383 let mut params = vec![];
384
385 if let Some(k) = key {
386 params.push(k.into());
387 }
388 self.bytecode.add_step(String::from("groupCount"), params);
389 self
390 }
391
392 pub fn group(mut self, key: Option<String>) -> Self {
393 let mut params = vec![];
394
395 if let Some(k) = key {
396 params.push(k.into());
397 }
398 self.bytecode.add_step(String::from("group"), params);
399 self
400 }
401
402 pub fn by<A>(mut self, step: A) -> Self
403 where
404 A: Into<ByStep>,
405 {
406 self.bytecode
407 .add_step(String::from("by"), step.into().into());
408 self
409 }
410
411 pub fn select<A>(mut self, step: A) -> Self
412 where
413 A: Into<SelectStep>,
414 {
415 self.bytecode
416 .add_step(String::from("select"), step.into().into());
417 self
418 }
419
420 pub fn fold(mut self) -> Self {
421 self.bytecode.add_step(String::from("fold"), vec![]);
422 self
423 }
424 pub fn unfold(mut self) -> Self {
425 self.bytecode.add_step(String::from("unfold"), vec![]);
426 self
427 }
428
429 pub fn path(mut self) -> Self {
430 self.bytecode.add_step(String::from("path"), vec![]);
431 self
432 }
433
434 pub fn limit<A>(mut self, limit: A) -> Self
435 where
436 A: Into<LimitStep>,
437 {
438 self.bytecode
439 .add_step(String::from("limit"), limit.into().into());
440
441 self
442 }
443
444 pub fn dedup<A>(mut self, limit: A) -> Self
445 where
446 A: Into<DedupStep>,
447 {
448 self.bytecode
449 .add_step(String::from("dedup"), limit.into().into());
450
451 self
452 }
453
454 pub fn sum<A>(mut self, scope: A) -> Self
455 where
456 A: Into<Scope>,
457 {
458 self.bytecode
459 .add_step(String::from("sum"), vec![scope.into().into()]);
460
461 self
462 }
463
464 pub fn max<A>(mut self, scope: A) -> Self
465 where
466 A: Into<Scope>,
467 {
468 self.bytecode
469 .add_step(String::from("max"), vec![scope.into().into()]);
470
471 self
472 }
473
474 pub fn mean<A>(mut self, scope: A) -> Self
475 where
476 A: Into<Scope>,
477 {
478 self.bytecode
479 .add_step(String::from("mean"), vec![scope.into().into()]);
480
481 self
482 }
483
484 pub fn min<A>(mut self, scope: A) -> Self
485 where
486 A: Into<Scope>,
487 {
488 self.bytecode
489 .add_step(String::from("min"), vec![scope.into().into()]);
490
491 self
492 }
493
494 pub fn is<A>(mut self, val: A) -> Self
495 where
496 A: IntoPredicate,
497 {
498 self.bytecode
499 .add_step(String::from("is"), vec![val.into_predicate().into()]);
500
501 self
502 }
503
504 pub fn where_<A>(mut self, step: A) -> Self
505 where
506 A: Into<WhereStep>,
507 {
508 self.bytecode
509 .add_step(String::from("where"), step.into().into());
510 self
511 }
512
513 pub fn not<A>(mut self, step: A) -> Self
514 where
515 A: Into<NotStep>,
516 {
517 self.bytecode
518 .add_step(String::from("not"), step.into().into());
519 self
520 }
521
522 pub fn order<A>(mut self, scope: A) -> Self
523 where
524 A: Into<Scope>,
525 {
526 self.bytecode
527 .add_step(String::from("order"), vec![scope.into().into()]);
528
529 self
530 }
531
532 pub fn match_<A>(mut self, step: A) -> Self
533 where
534 A: Into<MatchStep>,
535 {
536 self.bytecode
537 .add_step(String::from("match"), step.into().into());
538 self
539 }
540
541 pub fn drop(mut self) -> Self {
542 self.bytecode.add_step(String::from("drop"), vec![]);
543 self
544 }
545
546 pub fn or<A>(mut self, step: A) -> Self
547 where
548 A: Into<OrStep>,
549 {
550 self.bytecode
551 .add_step(String::from("or"), step.into().into());
552 self
553 }
554
555 pub fn project<A>(mut self, step: A) -> Self
556 where
557 A: Into<SelectStep>,
558 {
559 self.bytecode
560 .add_step(String::from("project"), step.into().into());
561 self
562 }
563
564 pub fn map<A>(mut self, step: A) -> Self
565 where
566 A: Into<ByStep>,
567 {
568 self.bytecode
569 .add_step(String::from("map"), step.into().into());
570 self
571 }
572
573 pub fn repeat<A>(mut self, step: A) -> Self
574 where
575 A: Into<RepeatStep>,
576 {
577 self.bytecode
578 .add_step(String::from("repeat"), step.into().into());
579
580 self
581 }
582
583 pub fn until<A>(mut self, step: A) -> Self
584 where
585 A: Into<UntilStep>,
586 {
587 self.bytecode
588 .add_step(String::from("until"), step.into().into());
589
590 self
591 }
592
593 pub fn simple_path(mut self) -> Self {
594 self.bytecode.add_step(String::from("simplePath"), vec![]);
595
596 self
597 }
598
599 pub fn sample(mut self, step: i32) -> Self {
600 self.bytecode
601 .add_step(String::from("sample"), vec![step.into()]);
602 self
603 }
604
605 pub fn loops<A>(mut self, step: A) -> Self
606 where
607 A: Into<LoopsStep>,
608 {
609 self.bytecode
610 .add_step(String::from("loops"), step.into().into());
611 self
612 }
613
614 pub fn local<A>(mut self, step: A) -> Self
615 where
616 A: Into<LocalStep>,
617 {
618 self.bytecode
619 .add_step(String::from("local"), step.into().into());
620 self
621 }
622
623 pub fn aggregate<A>(mut self, alias: A) -> Self
624 where
625 A: Into<String>,
626 {
627 self.bytecode
628 .add_step(String::from("aggregate"), vec![alias.into().into()]);
629 self
630 }
631
632 pub fn value(mut self) -> Self {
633 self.bytecode.add_step(String::from("value"), vec![]);
634 self
635 }
636
637 pub fn choose<A>(mut self, step: A) -> Self
638 where
639 A: IntoChooseStep,
640 {
641 self.bytecode
642 .add_step(String::from("choose"), step.into_step());
643 self
644 }
645
646 pub fn coalesce<A>(mut self, coalesce: A) -> Self
647 where
648 A: Into<CoalesceStep>,
649 {
650 self.bytecode
651 .add_step(String::from("coalesce"), coalesce.into().into());
652
653 self
654 }
655
656 pub fn identity(mut self) -> Self {
657 self.bytecode.add_step(String::from("identity"), vec![]);
658 self
659 }
660
661 pub fn range(mut self, step: i64, step2: i64) -> Self {
662 self.bytecode
663 .add_step(String::from("range"), vec![step.into(), step2.into()]);
664 self
665 }
666
667 pub fn cap(mut self, step: &'static str) -> Self {
668 self.bytecode
669 .add_step(String::from("cap"), vec![step.into()]);
670 self
671 }
672
673 pub fn barrier(mut self) -> Self {
674 self.bytecode.add_step(String::from("barrier"), vec![]);
675 self
676 }
677
678 pub fn optional(mut self, step: TraversalBuilder) -> Self {
679 self.bytecode
680 .add_step(String::from("optional"), vec![step.bytecode.into()]);
681 self
682 }
683
684 pub fn constant<A>(mut self, value: A) -> Self
685 where
686 A: Into<GValue>,
687 {
688 self.bytecode
689 .add_step(String::from("constant"), vec![value.into()]);
690 self
691 }
692
693 pub fn emit(mut self) -> Self {
694 self.bytecode.add_step(String::from("emit"), vec![]);
695 self
696 }
697
698 pub fn id(mut self) -> Self {
699 self.bytecode.add_step(String::from("id"), vec![]);
700 self
701 }
702}