gremlin_client/process/traversal/
remote.rs

1use crate::client::GremlinClient;
2use crate::conversion::FromGValue;
3use crate::process::traversal::strategies::TraversalStrategies;
4use crate::process::traversal::RemoteTraversalIterator;
5use crate::process::traversal::{GraphTraversal, GraphTraversalSource};
6use crate::GremlinResult;
7
8#[cfg(feature = "async_gremlin")]
9use crate::aio::GremlinClient as GremlinAsyncClient;
10
11#[cfg(feature = "async_gremlin")]
12use crate::aio::process::traversal::remote::AsyncTerminator;
13
14pub struct RemoteTraversalSource {}
15
16impl RemoteTraversalSource {
17    pub fn with_remote(&self, client: GremlinClient) -> GraphTraversalSource<SyncTerminator> {
18        GraphTraversalSource::<MockTerminator>::new(MockTerminator {}).with_remote(client)
19    }
20
21    pub fn empty(&self) -> GraphTraversalSource<MockTerminator> {
22        GraphTraversalSource::<MockTerminator>::new(MockTerminator {})
23    }
24
25    #[cfg(feature = "async_gremlin")]
26    pub fn with_remote_async(
27        &self,
28        client: GremlinAsyncClient,
29    ) -> GraphTraversalSource<AsyncTerminator> {
30        GraphTraversalSource::<MockTerminator>::new(MockTerminator {}).with_remote_async(client)
31    }
32}
33
34pub fn traversal() -> RemoteTraversalSource {
35    RemoteTraversalSource {}
36}
37
38#[derive(Clone)]
39pub struct MockTerminator {}
40
41impl Default for MockTerminator {
42    fn default() -> Self {
43        MockTerminator {}
44    }
45}
46
47impl MockTerminator {
48    pub fn new() -> Self {
49        MockTerminator {}
50    }
51}
52impl<T: FromGValue> Terminator<T> for MockTerminator {
53    type List = ();
54    type Next = ();
55    type HasNext = ();
56    type Iter = ();
57
58    fn to_list<S, E>(&self, _traversal: &GraphTraversal<S, T, E>) -> Self::List
59    where
60        E: Terminator<T>,
61    {
62        unimplemented!()
63    }
64
65    fn next<S, E>(&self, _traversal: &GraphTraversal<S, T, E>) -> Self::Next
66    where
67        E: Terminator<T>,
68    {
69        unimplemented!()
70    }
71
72    fn has_next<S, E>(&self, _traversal: &GraphTraversal<S, T, E>) -> Self::HasNext
73    where
74        E: Terminator<T>,
75    {
76        unimplemented!()
77    }
78
79    fn iter<S, E>(&self, _traversal: &GraphTraversal<S, T, E>) -> Self::Iter
80    where
81        E: Terminator<T>,
82    {
83        unimplemented!()
84    }
85}
86pub trait Terminator<T: FromGValue>: Clone {
87    type List;
88    type Next;
89    type HasNext;
90    type Iter;
91
92    fn to_list<S, E>(&self, traversal: &GraphTraversal<S, T, E>) -> Self::List
93    where
94        E: Terminator<T>;
95
96    fn next<S, E>(&self, traversal: &GraphTraversal<S, T, E>) -> Self::Next
97    where
98        E: Terminator<T>;
99
100    fn has_next<S, E>(&self, traversal: &GraphTraversal<S, T, E>) -> Self::HasNext
101    where
102        E: Terminator<T>;
103
104    fn iter<S, E>(&self, traversal: &GraphTraversal<S, T, E>) -> Self::Iter
105    where
106        E: Terminator<T>;
107}
108
109#[derive(Clone)]
110pub struct SyncTerminator {
111    strategies: TraversalStrategies,
112}
113
114impl SyncTerminator {
115    pub fn new(strategies: TraversalStrategies) -> SyncTerminator {
116        SyncTerminator { strategies }
117    }
118}
119
120impl<T: FromGValue> Terminator<T> for SyncTerminator {
121    type List = GremlinResult<Vec<T>>;
122    type Next = GremlinResult<Option<T>>;
123    type HasNext = GremlinResult<bool>;
124    type Iter = GremlinResult<RemoteTraversalIterator<T>>;
125
126    fn to_list<S, E>(&self, traversal: &GraphTraversal<S, T, E>) -> Self::List
127    where
128        E: Terminator<T>,
129    {
130        self.strategies.apply(traversal)?.collect()
131    }
132
133    fn next<S, E>(&self, traversal: &GraphTraversal<S, T, E>) -> Self::Next
134    where
135        E: Terminator<T>,
136    {
137        let results: GremlinResult<Vec<T>> = self.strategies.apply(traversal)?.collect();
138
139        Ok(results?.into_iter().next())
140    }
141
142    fn has_next<S, E>(&self, traversal: &GraphTraversal<S, T, E>) -> Self::HasNext
143    where
144        E: Terminator<T>,
145    {
146        let results: GremlinResult<Vec<T>> = self.strategies.apply(traversal)?.collect();
147
148        Ok(results?.iter().next().is_some())
149    }
150
151    fn iter<S, E>(&self, traversal: &GraphTraversal<S, T, E>) -> Self::Iter
152    where
153        E: Terminator<T>,
154    {
155        self.strategies.apply(traversal)
156    }
157}