graph-api-lib 0.2.1

Core library for the graph-api ecosystem - a flexible, type-safe API for working with in-memory graphs in Rust
Documentation
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use crate::walker::steps::Empty;
use crate::walker::{EdgeWalker, VertexWalker};
use std::marker::PhantomData;

/// A marker trait for types that can be mutated.
///
/// This trait is used to differentiate between mutable and immutable graph walkers.
pub trait Mutable {}

/// A marker type that indicates mutability.
///
/// Used as a type parameter in walker builders to enable mutable graph operations.
pub struct MutableMarker;

impl Mutable for MutableMarker {}

/// A marker type that indicates immutability.
///
/// Used as a type parameter in walker builders to restrict operations to immutable ones.
pub struct ImmutableMarker;

/// Manages access to a graph during traversal operations.
///
/// This enum represents the three states of graph access:
/// - Immutable: A shared reference to the graph
/// - Mutable: An exclusive reference to the graph
/// - Taken: The reference has been consumed
#[derive(Default)]
pub enum GraphAccess<'graph, Graph> {
    /// A shared reference to a graph
    Immutable(&'graph Graph),
    /// An exclusive reference to a graph
    Mutable(&'graph mut Graph),
    /// The reference has been taken and can no longer be used
    #[default]
    Taken,
}

impl<'graph, Graph> GraphAccess<'graph, Graph> {
    /// Takes the graph reference, leaving the enum in the `Taken` state.
    ///
    /// # Returns
    /// A shared reference to the graph.
    ///
    /// # Panics
    /// Panics if the graph reference has already been taken.
    pub fn take(&mut self) -> &'graph Graph {
        match std::mem::take(self) {
            GraphAccess::Immutable(graph) => graph,
            GraphAccess::Mutable(graph) => graph,
            GraphAccess::Taken => panic!("graph already taken"),
        }
    }

    /// Takes the graph reference for mutation, leaving the enum in the `Taken` state.
    ///
    /// # Returns
    /// An exclusive reference to the graph.
    ///
    /// # Panics
    /// Panics if the graph reference has already been taken or if it's an immutable reference.
    pub fn take_mut(&mut self) -> &'graph mut Graph {
        match std::mem::take(self) {
            GraphAccess::Immutable(_) => {
                panic!("graph should not have been accessed mutably")
            }
            GraphAccess::Mutable(graph) => graph,
            GraphAccess::Taken => panic!("graph already taken"),
        }
    }
}

/// A generic builder for graph walkers.
///
/// This builder is used to construct and compose graph traversal operations.
///
/// # Type Parameters
/// - `'graph`: The lifetime of the graph reference
/// - `Mutability`: A marker type indicating whether graph mutations are allowed
/// - `Graph`: The graph type being traversed
/// - `Walker`: The walker implementation that will perform the traversal
pub struct WalkerBuilder<'graph, Mutability, Graph, Walker>
where
    Walker: crate::walker::Walker<'graph>,
{
    pub(crate) _phantom: PhantomData<&'graph (Mutability, Graph, Walker)>,
    pub(crate) walker: Walker,
}

impl<'graph, Mutability, Graph, Walker> WalkerBuilder<'graph, Mutability, Graph, Walker>
where
    Walker: crate::walker::Walker<'graph>,
{
    /// Returns a mutable reference to the underlying walker.
    ///
    /// This method provides access to the walker being built, allowing for direct manipulation.
    ///
    /// # Returns
    /// A mutable reference to the walker.
    pub fn walker(&mut self) -> &mut Walker {
        &mut self.walker
    }
}

impl<'graph, Mutability, Graph, Walker> From<VertexWalkerBuilder<'graph, Mutability, Graph, Walker>>
    for WalkerBuilder<'graph, Mutability, Graph, Walker>
where
    Walker: VertexWalker<'graph, Graph = Graph>,
{
    fn from(value: VertexWalkerBuilder<'graph, Mutability, Graph, Walker>) -> Self {
        WalkerBuilder {
            _phantom: Default::default(),
            walker: value.walker,
        }
    }
}

impl<'graph, Mutability, Graph, Walker> From<EdgeWalkerBuilder<'graph, Mutability, Graph, Walker>>
    for WalkerBuilder<'graph, Mutability, Graph, Walker>
where
    Walker: EdgeWalker<'graph, Graph = Graph>,
{
    fn from(value: EdgeWalkerBuilder<'graph, Mutability, Graph, Walker>) -> Self {
        WalkerBuilder {
            _phantom: Default::default(),
            walker: value.walker,
        }
    }
}

/// A builder for vertex-focused graph traversals.
///
/// This builder constructs walkers that navigate graphs by moving from vertex to vertex.
///
/// # Type Parameters
/// - `'graph`: The lifetime of the graph reference
/// - `Mutability`: A marker type indicating whether graph mutations are allowed
/// - `Graph`: The graph type being traversed
/// - `Walker`: The vertex walker implementation that will perform the traversal
pub struct VertexWalkerBuilder<'graph, Mutability, Graph, Walker>
where
    Walker: VertexWalker<'graph, Graph = Graph>,
{
    _phantom: PhantomData<&'graph Mutability>,
    walker: Walker,
    graph: GraphAccess<'graph, Graph>,
}

pub(crate) fn new<'graph, Mutability, Graph, Start>(
    graph: GraphAccess<'graph, Graph>,
    start: Start,
) -> VertexWalkerBuilder<'graph, Mutability, Graph, Start>
where
    Graph: crate::graph::Graph,
    Start: VertexWalker<'graph, Graph = Graph>,
{
    VertexWalkerBuilder {
        _phantom: Default::default(),
        walker: start,
        graph,
    }
}

#[allow(dead_code)]
pub(crate) fn new_mut<'graph, Graph, Start>(
    graph: &'graph mut Graph,
    start: Start,
) -> VertexWalkerBuilder<'graph, MutableMarker, Graph, Start>
where
    Graph: crate::graph::Graph,
    Start: VertexWalker<'graph, Graph = Graph>,
{
    VertexWalkerBuilder {
        _phantom: Default::default(),
        walker: start,
        graph: GraphAccess::Mutable(graph),
    }
}

impl<'graph, Mutability, Graph, Walker> VertexWalkerBuilder<'graph, Mutability, Graph, Walker>
where
    Graph: crate::graph::Graph,
    Walker: VertexWalker<'graph, Graph = Graph>,
{
    /// Returns an immutable reference to the graph being traversed.
    ///
    /// This consumes the graph reference, leaving the builder in a state
    /// where the graph reference has been taken.
    ///
    /// # Returns
    /// An immutable reference to the graph.
    pub fn graph(&mut self) -> &'graph Graph {
        self.graph.take()
    }

    /// Returns a mutable reference to the graph being traversed.
    ///
    /// This consumes the graph reference, leaving the builder in a state
    /// where the graph reference has been taken.
    ///
    /// # Returns
    /// A mutable reference to the graph.
    ///
    /// # Panics
    /// Panics if the graph was not accessed mutably or has already been taken.
    pub fn graph_mut(&mut self) -> &'graph mut Graph {
        self.graph.take_mut()
    }

    /// Consumes the builder and returns the underlying walker.
    ///
    /// This extracts the walker from the builder, allowing it to be used directly.
    ///
    /// # Returns
    /// The walker that was being built.
    pub fn walker(self) -> Walker {
        self.walker
    }

    /// Transforms the current vertex walker into an edge walker.
    ///
    /// This method allows for changing the traversal from vertex-oriented to edge-oriented
    /// by applying a transformation function to the current walker.
    ///
    /// # Type Parameters
    /// - `EdgeWalker`: The target edge walker type
    /// - `WithFn`: A function type that converts the current walker to an edge walker
    ///
    /// # Parameters
    /// - `step`: The transformation function to apply
    ///
    /// # Returns
    /// A new edge walker builder containing the transformed walker
    pub fn with_edge_walker<
        EdgeWalker: crate::walker::EdgeWalker<'graph>,
        WithFn: FnOnce(Walker) -> EdgeWalker,
    >(
        self,
        step: WithFn,
    ) -> EdgeWalkerBuilder<'graph, Mutability, Graph, EdgeWalker> {
        EdgeWalkerBuilder {
            _phantom: Default::default(),
            walker: step(self.walker),
            graph: self.graph,
        }
    }

    /// Transforms the current vertex walker into another vertex walker.
    ///
    /// This method allows for changing or extending the traversal while remaining
    /// vertex-oriented by applying a transformation function to the current walker.
    ///
    /// # Type Parameters
    /// - `VertexWalker`: The target vertex walker type
    /// - `WithFn`: A function type that converts the current walker to another vertex walker
    ///
    /// # Parameters
    /// - `step`: The transformation function to apply
    ///
    /// # Returns
    /// A new vertex walker builder containing the transformed walker
    pub fn with_vertex_walker<
        VertexWalker: crate::walker::VertexWalker<'graph, Graph = Graph>,
        WithFn: FnOnce(Walker) -> VertexWalker,
    >(
        self,
        step: WithFn,
    ) -> VertexWalkerBuilder<'graph, Mutability, Graph, VertexWalker> {
        VertexWalkerBuilder {
            _phantom: Default::default(),
            walker: step(self.walker),
            graph: self.graph,
        }
    }
}

/// A builder for edge-focused graph traversals.
///
/// This builder constructs walkers that navigate graphs by moving along edges.
///
/// # Type Parameters
/// - `'graph`: The lifetime of the graph reference
/// - `Mutability`: A marker type indicating whether graph mutations are allowed
/// - `Graph`: The graph type being traversed
/// - `Walker`: The edge walker implementation that will perform the traversal
pub struct EdgeWalkerBuilder<'graph, Mutability, Graph, Walker>
where
    Walker: EdgeWalker<'graph>,
{
    _phantom: PhantomData<&'graph Mutability>,
    walker: Walker,
    graph: GraphAccess<'graph, Graph>,
}

impl<'graph, Mutability, Graph, Walker> EdgeWalkerBuilder<'graph, Mutability, Graph, Walker>
where
    Graph: crate::graph::Graph,
    Walker: EdgeWalker<'graph, Graph = Graph>,
    <Walker as crate::walker::Walker<'graph>>::Context: Clone + 'static,
{
    /// Returns an immutable reference to the graph being traversed.
    ///
    /// This consumes the graph reference, leaving the builder in a state
    /// where the graph reference has been taken.
    ///
    /// # Returns
    /// An immutable reference to the graph.
    pub fn graph(&mut self) -> &'graph Graph {
        self.graph.take()
    }

    /// Returns a mutable reference to the graph being traversed.
    ///
    /// This consumes the graph reference, leaving the builder in a state
    /// where the graph reference has been taken.
    ///
    /// # Returns
    /// A mutable reference to the graph.
    ///
    /// # Panics
    /// Panics if the graph was not accessed mutably or has already been taken.
    pub fn graph_mut(&mut self) -> &'graph mut Graph {
        self.graph.take_mut()
    }

    /// Consumes the builder and returns the underlying walker.
    ///
    /// This extracts the walker from the builder, allowing it to be used directly.
    ///
    /// # Returns
    /// The walker that was being built.
    pub fn walker(self) -> Walker {
        self.walker
    }

    /// Transforms the current walker into another edge walker.
    ///
    /// This method allows for changing or extending the traversal using
    /// a transformation function applied to the current walker.
    ///
    /// # Type Parameters
    /// - `EdgeWalker`: The target edge walker type
    /// - `WithFn`: A function type that converts the current walker
    ///
    /// # Parameters
    /// - `step`: The transformation function to apply
    ///
    /// # Returns
    /// A new edge walker builder containing the transformed walker
    pub fn with_edge_walker<
        EdgeWalker: crate::walker::EdgeWalker<'graph>,
        WithFn: FnOnce(Walker) -> EdgeWalker,
    >(
        self,
        step: WithFn,
    ) -> EdgeWalkerBuilder<'graph, Mutability, Graph, EdgeWalker> {
        EdgeWalkerBuilder {
            _phantom: Default::default(),
            walker: step(self.walker),
            graph: self.graph,
        }
    }

    /// Transforms the current walker into a vertex walker.
    ///
    /// This method allows for changing the traversal from edge-oriented to vertex-oriented
    /// by applying a transformation function to the current walker.
    ///
    /// # Type Parameters
    /// - `VertexWalker`: The target vertex walker type
    /// - `WithFn`: A function type that converts the current walker
    ///
    /// # Parameters
    /// - `step`: The transformation function to apply
    ///
    /// # Returns
    /// A new vertex walker builder containing the transformed walker
    pub fn with_vertex_walker<
        VertexWalker: crate::walker::VertexWalker<'graph, Graph = Graph>,
        WithFn: FnOnce(Walker) -> VertexWalker,
    >(
        self,
        step: WithFn,
    ) -> VertexWalkerBuilder<'graph, Mutability, Graph, VertexWalker> {
        VertexWalkerBuilder {
            _phantom: Default::default(),
            walker: step(self.walker),
            graph: self.graph,
        }
    }
}

pub(crate) fn new_start<Graph>(graph: &Graph) -> StartWalkerBuilder<'_, ImmutableMarker, Graph, ()>
where
    Graph: crate::graph::Graph,
{
    StartWalkerBuilder {
        _phantom: Default::default(),
        graph: GraphAccess::Immutable(graph),
        empty: Empty::default(),
    }
}

pub(crate) fn new_start_mut<Graph>(
    graph: &mut Graph,
) -> StartWalkerBuilder<'_, MutableMarker, Graph, ()>
where
    Graph: crate::graph::Graph,
{
    StartWalkerBuilder {
        _phantom: Default::default(),
        graph: GraphAccess::Mutable(graph),
        empty: Empty::default(),
    }
}

/// The initial builder for starting a graph traversal.
///
/// This builder is the entry point for creating graph traversals. It contains
/// a reference to the graph and an empty context, and provides methods to begin
/// traversing the graph.
///
/// # Type Parameters
/// - `'graph`: The lifetime of the graph reference
/// - `Mutability`: A marker type indicating whether graph mutations are allowed
/// - `Graph`: The graph type being traversed
/// - `Context`: The initial context type for the traversal
pub struct StartWalkerBuilder<'graph, Mutability, Graph, Context> {
    pub(crate) _phantom: PhantomData<&'graph (Mutability, Graph)>,
    pub(crate) graph: GraphAccess<'graph, Graph>,
    pub(crate) empty: Empty<Graph, Context>,
}