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
use crate::walker::builder::{EdgeWalkerBuilder, VertexWalkerBuilder};
use crate::walker::steps::{EdgeProbe, VertexProbe};
use crate::walker::{EdgeWalker, VertexWalker};
use include_doc::function_body;
use std::fmt::Debug;
impl<'graph, Mutability, Graph, Walker> VertexWalkerBuilder<'graph, Mutability, Graph, Walker>
where
Graph: crate::graph::Graph,
Walker: VertexWalker<'graph, Graph = Graph>,
{
/// # Debug Step
///
/// The `dbg` step prints elements as they are traversed through the graph, making it
/// easier to debug complex traversals. Each element is tagged with the provided label.
///
/// ## Visual Diagram
///
/// Before dbg step:
/// ```text
/// [A]* --- edge1 ---> [B]* --- edge2 ---> [C]*
/// ^
/// |
/// edge3
/// |
/// [D]*
/// ```
///
/// After dbg step (elements continue in traversal, but are also printed to console):
/// ```text
/// [A]* --- edge1 ---> [B]* --- edge2 ---> [C]*
/// ^
/// |
/// edge3
/// |
/// [D]*
///
/// Console output:
/// [stage1] Vertex(A)
/// [stage1] Vertex(B)
/// [stage1] Vertex(C)
/// [stage1] Vertex(D)
/// ```
///
/// ## Parameters
///
/// - `tag`: A string label that will prefix all debug output to help identify which debug step is printing
///
/// ## Return Value
///
/// Returns the same traversal that was passed in, allowing the traversal to continue unmodified.
///
/// ## Example
///
/// ```rust
#[doc = function_body!("examples/dbg.rs", basic_example, [])]
/// ```
///
/// ## Notes
///
/// - The `dbg` step is non-destructive - it does not modify the traversal path
/// - Debug output goes to the console using the standard Debug trait implementation
/// - Remember that traversals are typically depth-first, so the output order may not be immediately intuitive
/// - For complex graphs, consider using more descriptive tags at each debug point
/// - This step is particularly useful for understanding how graph elements flow through a complex traversal
/// - The `dbg` step has minimal performance impact when not in debug mode
pub fn dbg(
self,
tag: &'static str,
) -> VertexWalkerBuilder<
'graph,
Mutability,
Graph,
VertexProbe<'graph, Walker, impl FnMut(&Graph::VertexReference<'_>, &Walker::Context)>,
>
where
Walker::Context: Debug,
{
let callback = move |vertex: &Graph::VertexReference<'_>, ctx: &Walker::Context| {
println!("{}: {:?} {:?}", tag, vertex, ctx);
};
self.probe(callback)
}
}
impl<'graph, Mutability, Graph, Walker> EdgeWalkerBuilder<'graph, Mutability, Graph, Walker>
where
Graph: crate::graph::Graph,
Walker: EdgeWalker<'graph, Graph = Graph>,
{
/// # Debug Step
///
/// Prints edges as they are traversed through the graph, making it easier to debug complex traversals.
///
/// See the documentation for [`VertexWalkerBuilder::dbg`] for more details.
pub fn dbg(
self,
tag: &'static str,
) -> EdgeWalkerBuilder<
'graph,
Mutability,
Graph,
EdgeProbe<'graph, Walker, impl FnMut(&Graph::EdgeReference<'_>, &Walker::Context)>,
>
where
Walker::Context: Debug,
{
let callback = move |edge: &Graph::EdgeReference<'_>, ctx: &Walker::Context| {
println!("{}: {:?} {:?}", tag, edge, ctx);
};
self.probe(callback)
}
}