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
use crate::graph::{EdgeReference, VertexReference};
use crate::walker::builder::{EdgeWalkerBuilder, VertexWalkerBuilder};
use crate::walker::steps::{
ContextRef, DefaultEdgeContext, DefaultVertexContext, EdgeContext, VertexContext,
};
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>,
{
/// # Default Context Step
///
/// The `push_default_context` step is a specialized version of the `push_context` step that
/// automatically stores the current element's ID and data in the context. This provides a
/// convenient way to preserve information about elements as you traverse through the graph.
///
/// ## Visual Diagram
///
/// Before push_default_context step (traversal with regular elements):
/// ```text
/// [Person A]* --- created ---> [Project X]*
/// |
/// knows
/// |
/// [Person B]*
/// ```
///
/// After push_default_context step (elements with default context):
/// ```text
/// [Person A]* + {vertex_id: id_a, vertex: Person{name: "A", age: 30}} --- created ---> [Project X]* + {vertex_id: id_x, vertex: Project{name: "X"}}
/// |
/// knows
/// |
/// [Person B]* + {vertex_id: id_b, vertex: Person{name: "B", age: 25}}
/// ```
///
/// ## Parameters
///
/// None
///
/// ## Return Value
///
/// Returns a traversal with the same elements, but with each element's ID and data stored in its context.
///
/// ## Example
///
/// ```rust
#[doc = function_body!("examples/default_context.rs", basic_example, [])]
/// ```
///
/// ## Notes
///
/// - Default context for vertices includes:
/// - `vertex_id`: The ID of the vertex
/// - `vertex`: The vertex data (cloned from the graph)
/// - Type safety is maintained as the vertex/edge types are preserved
/// - This step requires that your vertex/edge types implement Clone + 'static
/// - More concise than manual context handling for common use cases
/// - Especially useful when you need to preserve information across multiple traversal steps
/// - Combines well with other context operations for complex data gathering
pub fn push_default_context(
self,
) -> VertexWalkerBuilder<
'graph,
Mutability,
Graph,
VertexContext<
'graph,
Walker,
impl Fn(
&Graph::VertexReference<'_>,
&Walker::Context,
)
-> ContextRef<DefaultVertexContext<Graph::VertexId, Graph::Vertex>, Walker::Context>,
ContextRef<DefaultVertexContext<Graph::VertexId, Graph::Vertex>, Walker::Context>,
>,
>
where
Graph::Vertex: Clone + 'static,
{
self.push_context(|vertex, _context| DefaultVertexContext {
vertex_id: vertex.id(),
vertex: vertex.weight().clone(),
})
}
}
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,
{
/// # Default Context Step
///
/// A specialized version of the `push_context` step for edges that automatically
/// stores the current edge's ID and data in the context.
///
/// See the documentation for [`VertexWalkerBuilder::push_default_context`] for more details.
pub fn push_default_context(
self,
) -> EdgeWalkerBuilder<
'graph,
Mutability,
Graph,
EdgeContext<
'graph,
Walker,
impl Fn(
&Graph::EdgeReference<'_>,
&Walker::Context,
)
-> ContextRef<DefaultEdgeContext<Graph::EdgeId, Graph::Edge>, Walker::Context>,
ContextRef<DefaultEdgeContext<Graph::EdgeId, Graph::Edge>, Walker::Context>,
>,
>
where
Graph::Edge: Clone + 'static,
{
self.push_context(|edge, _context| DefaultEdgeContext {
edge_id: edge.id(),
edge: edge.weight().clone(),
})
}
}