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