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
use crate::ElementId;
use crate::graph::Graph;
use crate::walker::builder::{EdgeWalkerBuilder, VertexWalkerBuilder};
use crate::walker::{EdgeWalker, VertexWalker, Walker};
use include_doc::function_body;
use std::marker::PhantomData;
// ================ PROBE IMPLEMENTATION ================
pub struct VertexProbe<'graph, Parent, Callback> {
_phantom_data: PhantomData<&'graph ()>,
parent: Parent,
callback: Callback,
}
impl<Parent, Callback> VertexProbe<'_, Parent, Callback> {
pub(crate) fn new(parent: Parent, callback: Callback) -> Self {
VertexProbe {
_phantom_data: Default::default(),
parent,
callback,
}
}
}
impl<'graph, Parent, Callback> Walker<'graph> for VertexProbe<'graph, Parent, Callback>
where
Parent: VertexWalker<'graph>,
Callback: FnMut(&<Parent::Graph as Graph>::VertexReference<'_>, &Parent::Context),
{
type Graph = Parent::Graph;
type Context = Parent::Context;
fn next_element(&mut self, graph: &'graph Self::Graph) -> Option<ElementId<Self::Graph>> {
self.next(graph).map(ElementId::Vertex)
}
fn ctx(&self) -> &Self::Context {
self.parent.ctx()
}
fn ctx_mut(&mut self) -> &mut Self::Context {
self.parent.ctx_mut()
}
}
impl<'graph, Parent, Callback> VertexWalker<'graph> for VertexProbe<'graph, Parent, Callback>
where
Parent: VertexWalker<'graph>,
Callback: FnMut(&<Parent::Graph as Graph>::VertexReference<'_>, &Parent::Context),
{
fn next(&mut self, graph: &'graph Self::Graph) -> Option<<Self::Graph as Graph>::VertexId> {
let next = self.parent.next(graph);
if let Some(id) = next {
if let Some(vertex) = graph.vertex(id) {
(self.callback)(&vertex, self.parent.ctx());
}
}
next
}
}
pub struct EdgeProbe<'graph, Parent, Callback> {
_phantom_data: PhantomData<&'graph ()>,
parent: Parent,
callback: Callback,
}
impl<Parent, Callback> EdgeProbe<'_, Parent, Callback> {
pub(crate) fn new(parent: Parent, callback: Callback) -> Self {
EdgeProbe {
_phantom_data: Default::default(),
parent,
callback,
}
}
}
impl<'graph, Parent, Callback> Walker<'graph> for EdgeProbe<'graph, Parent, Callback>
where
Parent: EdgeWalker<'graph>,
Callback: FnMut(&<Parent::Graph as Graph>::EdgeReference<'_>, &Parent::Context),
{
type Graph = Parent::Graph;
type Context = Parent::Context;
fn next_element(&mut self, graph: &'graph Self::Graph) -> Option<ElementId<Self::Graph>> {
self.next(graph).map(ElementId::Edge)
}
fn ctx(&self) -> &Self::Context {
self.parent.ctx()
}
fn ctx_mut(&mut self) -> &mut Self::Context {
self.parent.ctx_mut()
}
}
impl<'graph, Parent, Callback> EdgeWalker<'graph> for EdgeProbe<'graph, Parent, Callback>
where
Parent: EdgeWalker<'graph>,
Callback: FnMut(&<Parent::Graph as Graph>::EdgeReference<'_>, &Parent::Context),
{
fn next(&mut self, graph: &'graph Self::Graph) -> Option<<Self::Graph as Graph>::EdgeId> {
let next = self.parent.next(graph);
if let Some(next) = next {
let edge = graph.edge(next).expect("edge must exist");
(self.callback)(&edge, self.parent.ctx());
}
next
}
}
impl<'graph, Mutability, Graph, Walker> VertexWalkerBuilder<'graph, Mutability, Graph, Walker>
where
Graph: crate::graph::Graph,
Walker: VertexWalker<'graph, Graph = Graph>,
{
/// # Probe Step
///
/// The `probe` step allows you to execute a callback function for each vertex in the traversal
/// without altering the traversal itself. This is useful for debugging, logging, or collecting
/// information during a traversal.
///
/// ## Visual Diagram
///
/// Before probe step:
/// ```text
/// [A]* --- edge1 ---> [B]* --- edge2 ---> [C]*
/// ^
/// |
/// edge3
/// |
/// [D]*
/// ```
///
/// After probe step (unchanged, but callback executed for each vertex *):
/// ```text
/// [A]* --- edge1 ---> [B]* --- edge2 ---> [C]*
/// ^
/// |
/// edge3
/// |
/// [D]*
/// ```
///
/// ## Parameters
///
/// - `callback`: A function that takes a reference to the current vertex being traversed,
/// and optionally the current context.
/// The function signature can be either:
/// - `FnMut(&Graph::VertexReference<'_>, &Context)` - Probe with access to current context
///
/// ## Return Value
///
/// A walker of the same type as the input with the probe operation added to the pipeline,
/// allowing for further chaining of operations.
///
/// ## Example
///
/// ```rust
#[doc = function_body!("examples/probe.rs", vertex_example, [])]
/// ```
///
/// ## Notes
///
/// - The `probe` step does not modify the traversal path or elements
/// - The callback function is executed for each vertex as it's traversed
/// - When using the context variant, you can access traversal context data during probing
/// - Useful for debugging, logging, or gathering statistics about your graph
/// - Side effects in the callback function (like printing or counting) do not affect the traversal
/// - Can be used at multiple points in a traversal to monitor the flow at different stages
/// - Consider using pattern matching in the callback to work with specific vertex types
/// - Context access is especially useful when combined with `push_context` steps earlier in the traversal
pub fn probe<Callback>(
self,
callback: Callback,
) -> VertexWalkerBuilder<'graph, Mutability, Graph, VertexProbe<'graph, Walker, Callback>>
where
Callback: FnMut(&Graph::VertexReference<'_>, &Walker::Context),
{
self.with_vertex_walker(|walker| VertexProbe::new(walker, callback))
}
}
impl<'graph, Mutability, Graph, Walker> EdgeWalkerBuilder<'graph, Mutability, Graph, Walker>
where
Graph: crate::graph::Graph,
Walker: EdgeWalker<'graph, Graph = Graph>,
{
/// # Probe Step
///
/// The `probe` step allows you to execute a callback function for each edge in the traversal
/// without altering the traversal itself. This is useful for debugging, analyzing connections,
/// or collecting edge statistics during a traversal.
///
/// ## Visual Diagram
///
/// Before probe step:
/// ```text
/// [Person A] --- knows* ---> [Person B] --- created* ---> [Project]
/// ^
/// |
/// owns*
/// |
/// [Company]
/// ```
///
/// After probe step (unchanged, but callback executed for each edge *):
/// ```text
/// [Person A] --- knows* ---> [Person B] --- created* ---> [Project]
/// ^
/// |
/// owns*
/// |
/// [Company]
/// ```
///
/// ## Parameters
///
/// - `callback`: A function that takes a reference to the current edge being traversed,
/// and optionally the current context.
/// The function signature can be either:
/// - `FnMut(&Graph::EdgeReference<'_>, &Context)` - Probe with access to current context
///
/// ## Return Value
///
/// A walker of the same type as the input with the probe operation added to the pipeline,
/// allowing for further chaining of operations.
///
/// ## Example
///
/// ```rust
#[doc = function_body!("examples/probe.rs", edge_example, [])]
/// ```
///
/// ## Notes
///
/// - The `probe` step does not modify the traversal path or edges
/// - The callback function is executed for each edge as it's traversed
/// - When using the context variant, you can access traversal context data during probing
/// - Useful for analyzing connection patterns without modifying the traversal
/// - Consider using pattern matching in the callback to handle different edge types
/// - You can use endpoint accessors like `tail()` and `head()` to inspect connected vertices
/// - Context access is especially useful when combined with `push_context` steps earlier in the traversal
pub fn probe<Callback>(
self,
callback: Callback,
) -> EdgeWalkerBuilder<'graph, Mutability, Graph, EdgeProbe<'graph, Walker, Callback>>
where
Callback: FnMut(&Graph::EdgeReference<'_>, &Walker::Context),
{
self.with_edge_walker(|walker| EdgeProbe::new(walker, callback))
}
}