use crate::graph::GraphStorage;
use crate::*;
impl<'g, S: GraphStorage + 'g> GraphTraversalSource<'g, S> {
pub fn to_direction<T: Into<ArgStrings>>(self, direction: Direction, labels: T) -> Self {
match direction {
Direction::Out => self.out(labels),
Direction::In => self.in_(labels),
Direction::Both => self.both(labels),
}
}
pub fn to_e<T: Into<ArgStrings>>(self, direction: Direction, labels: T) -> Self {
match direction {
Direction::Out => self.out_e(labels),
Direction::In => self.in_e(labels),
Direction::Both => self.both_e(labels),
}
}
pub fn to_v(self, direction: Direction) -> Self {
match direction {
Direction::Out => self.out_v(),
Direction::In => self.in_v(),
Direction::Both => self.both_v(),
}
}
}
impl __ {
pub fn to_direction<'g, S: GraphStorage + 'g, T: Into<ArgStrings>>(
direction: Direction,
labels: T,
) -> AnonymousTraversal<'g, S> {
AnonymousTraversal::new().to_direction(direction, labels)
}
pub fn to_e<'g, S: GraphStorage + 'g, T: Into<ArgStrings>>(
direction: Direction,
labels: T,
) -> AnonymousTraversal<'g, S> {
AnonymousTraversal::new().to_e(direction, labels)
}
pub fn to_v<'g, S: GraphStorage + 'g>(direction: Direction) -> AnonymousTraversal<'g, S> {
AnonymousTraversal::new().to_v(direction)
}
}
impl<'g, S: GraphStorage + 'g> AnonymousTraversal<'g, S> {
pub fn to_direction<T: Into<ArgStrings>>(self, direction: Direction, labels: T) -> Self {
match direction {
Direction::Out => self.out(labels),
Direction::In => self.in_(labels),
Direction::Both => self.both(labels),
}
}
pub fn to_e<T: Into<ArgStrings>>(self, direction: Direction, labels: T) -> Self {
match direction {
Direction::Out => self.out_e(labels),
Direction::In => self.in_e(labels),
Direction::Both => self.both_e(labels),
}
}
pub fn to_v(self, direction: Direction) -> Self {
match direction {
Direction::Out => self.out_v(),
Direction::In => self.in_v(),
Direction::Both => self.both_v(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn names(res: &[ElementProperty]) -> Vec<&str> {
let mut names: Vec<&str> = res.iter().filter_map(|v| v.as_str()).collect();
names.sort_unstable();
names
}
#[test]
fn anonymous_to_direction_in_a_child_traversal() {
test_dual!(
graph: GraphSource::tinkerpop_modern(),
rust: |g| g
.v(None)
.where_t(__::to_direction(Direction::Out, "created"))
.values("name")
.to_list(),
rhai: [
r#"g.V().where(__.to(Direction.OUT, "created")).values("name")"#,
r#"g.v().where(__.to(Direction::OUT, "created")).values("name")"#,
],
assert: |res| assert_eq!(names(res), ["josh", "marko", "peter"]),
);
test_dual!(
graph: GraphSource::tinkerpop_modern(),
rust: |g| g
.v(None)
.where_t(__::to_direction(Direction::In, "knows"))
.values("name")
.to_list(),
rhai: [r#"g.V().where(__.to(Direction.IN, "knows")).values("name")"#],
assert: |res| assert_eq!(names(res), ["josh", "vadas"]),
);
test_dual!(
graph: GraphSource::tinkerpop_modern(),
rust: |g| g
.v(None)
.where_t(__::to_direction(Direction::Both, None).count().is(1))
.values("name")
.to_list(),
rhai: [r#"g.V().where(__.to(Direction.BOTH).count().is(1)).values("name")"#],
assert: |res| assert_eq!(names(res), ["peter", "ripple", "vadas"]),
);
}
#[test]
fn anonymous_to_e_in_a_child_traversal() {
for (direction, rhai, expected) in [
(
Direction::Out,
r#"g.V().where(__.toE(Direction.OUT, "knows")).values("name")"#,
vec!["marko"],
),
(
Direction::In,
r#"g.V().where(__.toE(Direction.IN, "knows")).values("name")"#,
vec!["josh", "vadas"],
),
(
Direction::Both,
r#"g.V().where(__.to_e(Direction::BOTH, "knows")).values("name")"#,
vec!["josh", "marko", "vadas"],
),
] {
test_dual!(
graph: GraphSource::tinkerpop_modern(),
rust: |g| g
.v(None)
.where_t(__::to_e(direction, "knows"))
.values("name")
.to_list(),
rhai: [rhai],
assert: |res| assert_eq!(names(res), expected),
);
}
}
#[test]
fn anonymous_to_v_in_a_child_traversal() {
for (direction, rhai, expected) in [
(
Direction::Out,
r#"g.V("1").outE().where(__.toV(Direction.OUT).has("name", "vadas")).count()"#,
0,
),
(
Direction::In,
r#"g.V("1").outE().where(__.toV(Direction.IN).has("name", "vadas")).count()"#,
1,
),
(
Direction::Both,
r#"g.v("1").out_e().where(__.to_v(Direction::BOTH).has("name", "marko")).count()"#,
3,
),
] {
test_dual!(
graph: GraphSource::tinkerpop_modern(),
rust: |g| g
.v("1")
.out_e(None)
.where_t(__::to_v(direction).has(
"name",
if direction == Direction::Both { "marko" } else { "vadas" },
))
.count()
.to_list(),
rhai: [rhai],
assert: |res| assert_eq!(res[0].as_i64(), Some(expected)),
);
}
}
}