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
use crate::walker::builder::{EdgeWalkerBuilder, VertexWalkerBuilder};
use crate::walker::{EdgeWalker, VertexWalker};
use include_doc::function_body;
impl<'graph, Mutability, Graph, Walker> VertexWalkerBuilder<'graph, Mutability, Graph, Walker>
where
Graph: crate::graph::Graph,
Walker: VertexWalker<'graph, Graph = Graph>,
{
/// # Collect Step
///
/// The `collect` step finalizes a traversal and gathers the results into a collection.
/// This is a terminal operation that ends the traversal and provides access to the traversed elements.
///
/// ## Visual Diagram
///
/// Before collect step (with elements in traversal):
/// ```text
/// [A]* --- edge1 ---> [B]* --- edge2 ---> [C]*
/// ^
/// |
/// edge3
/// |
/// [D]*
/// ```
///
/// After collect step (all elements consumed and collected):
/// ```text
/// [A] --- edge1 ---> [B] --- edge2 ---> [C]
/// ^
/// |
/// edge3
/// |
/// [D]
///
/// Collection: [A, B, C, D]
/// ```
///
/// ## Parameters
///
/// None - but the resulting collection type is determined by the type parameter provided to the collect call.
///
/// ## Return Value
///
/// Returns a collection of the traversed elements. The exact type depends on what you're collecting into, commonly:
/// - `Vec<ElementId>` for simple ID collection
/// - `Vec<(ElementId, Context)>` when context is used
/// - Custom types when implementing `FromIterator`
///
/// ## Example
///
/// ```rust
#[doc = function_body!("examples/collect.rs", example, [])]
/// ```
///
/// For more examples, see the [collect example](https://github.com/yourusername/graph-api/blob/main/graph-api-lib/examples/collect.rs).
///
/// ## Notes
///
/// - The `collect` step is a terminal operation - no further traversal steps can be added after it
/// - When collecting with context, use `map` first to format the data for collection
/// - The collect step fully consumes the traversal
/// - Most commonly used with `Vec<_>`, but can collect into any type that implements `FromIterator`
/// - Consider using `limit` before `collect` for large graphs to avoid excessive memory use
/// - For single-element queries, consider using `first()` instead of `collect` for efficiency
pub fn collect<T: FromIterator<Graph::VertexId>>(self) -> T
where
Walker: VertexWalker<'graph>,
{
self.into_iter().collect()
}
}
impl<'graph, Mutability, Graph, Walker> EdgeWalkerBuilder<'graph, Mutability, Graph, Walker>
where
Graph: crate::graph::Graph,
Walker: EdgeWalker<'graph, Graph = Graph>,
{
/// Collects all edge IDs from this traversal into a collection of type `T`.
///
/// This is a terminal operation that consumes the traversal.
///
/// See the documentation for [`VertexWalkerBuilder::collect`] for more details.
pub fn collect<T: FromIterator<Graph::EdgeId>>(self) -> T
where
Walker: EdgeWalker<'graph>,
{
self.into_iter().collect()
}
}