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
use crate::client::GremlinClient;
use crate::conversion::FromGValue;
use crate::process::traversal::strategies::TraversalStrategies;
use crate::process::traversal::RemoteTraversalIterator;
use crate::process::traversal::{GraphTraversal, GraphTraversalSource};
use crate::GremlinResult;

#[cfg(feature = "async_gremlin")]
use crate::aio::GremlinClient as GremlinAsyncClient;

#[cfg(feature = "async_gremlin")]
use crate::aio::process::traversal::remote::AsyncTerminator;

pub struct RemoteTraversalSource {}

impl RemoteTraversalSource {
    pub fn with_remote(&self, client: GremlinClient) -> GraphTraversalSource<SyncTerminator> {
        GraphTraversalSource::<MockTerminator>::new(MockTerminator {}).with_remote(client)
    }

    #[cfg(feature = "async_gremlin")]
    pub fn with_remote_async(
        &self,
        client: GremlinAsyncClient,
    ) -> GraphTraversalSource<AsyncTerminator> {
        GraphTraversalSource::<MockTerminator>::new(MockTerminator {}).with_remote_async(client)
    }
}

pub fn traversal() -> RemoteTraversalSource {
    RemoteTraversalSource {}
}

#[derive(Clone)]
pub struct MockTerminator {}

impl Default for MockTerminator {
    fn default() -> Self {
        MockTerminator {}
    }
}

impl MockTerminator {
    pub fn new() -> Self {
        MockTerminator {}
    }
}
impl<T: FromGValue> Terminator<T> for MockTerminator {
    type List = ();
    type Next = ();
    type HasNext = ();
    type Iter = ();

    fn to_list<S, E>(&self, _traversal: &GraphTraversal<S, T, E>) -> Self::List
    where
        E: Terminator<T>,
    {
        unimplemented!()
    }

    fn next<S, E>(&self, _traversal: &GraphTraversal<S, T, E>) -> Self::Next
    where
        E: Terminator<T>,
    {
        unimplemented!()
    }

    fn has_next<S, E>(&self, _traversal: &GraphTraversal<S, T, E>) -> Self::HasNext
    where
        E: Terminator<T>,
    {
        unimplemented!()
    }

    fn iter<S, E>(&self, _traversal: &GraphTraversal<S, T, E>) -> Self::Iter
    where
        E: Terminator<T>,
    {
        unimplemented!()
    }
}
pub trait Terminator<T: FromGValue>: Clone {
    type List;
    type Next;
    type HasNext;
    type Iter;

    fn to_list<S, E>(&self, traversal: &GraphTraversal<S, T, E>) -> Self::List
    where
        E: Terminator<T>;

    fn next<S, E>(&self, traversal: &GraphTraversal<S, T, E>) -> Self::Next
    where
        E: Terminator<T>;

    fn has_next<S, E>(&self, traversal: &GraphTraversal<S, T, E>) -> Self::HasNext
    where
        E: Terminator<T>;

    fn iter<S, E>(&self, traversal: &GraphTraversal<S, T, E>) -> Self::Iter
    where
        E: Terminator<T>;
}

#[derive(Clone)]
pub struct SyncTerminator {
    strategies: TraversalStrategies,
}

impl SyncTerminator {
    pub fn new(strategies: TraversalStrategies) -> SyncTerminator {
        SyncTerminator { strategies }
    }
}

impl<T: FromGValue> Terminator<T> for SyncTerminator {
    type List = GremlinResult<Vec<T>>;
    type Next = GremlinResult<Option<T>>;
    type HasNext = GremlinResult<bool>;
    type Iter = GremlinResult<RemoteTraversalIterator<T>>;

    fn to_list<S, E>(&self, traversal: &GraphTraversal<S, T, E>) -> Self::List
    where
        E: Terminator<T>,
    {
        self.strategies.apply(traversal)?.collect()
    }

    fn next<S, E>(&self, traversal: &GraphTraversal<S, T, E>) -> Self::Next
    where
        E: Terminator<T>,
    {
        let results: GremlinResult<Vec<T>> = self.strategies.apply(traversal)?.collect();

        Ok(results?.into_iter().next())
    }

    fn has_next<S, E>(&self, traversal: &GraphTraversal<S, T, E>) -> Self::HasNext
    where
        E: Terminator<T>,
    {
        let results: GremlinResult<Vec<T>> = self.strategies.apply(traversal)?.collect();

        Ok(results?.iter().next().is_some())
    }

    fn iter<S, E>(&self, traversal: &GraphTraversal<S, T, E>) -> Self::Iter
    where
        E: Terminator<T>,
    {
        self.strategies.apply(traversal)
    }
}