graph_api_lib/walker/steps/default_context.rs
1use crate::graph::{EdgeReference, VertexReference};
2use crate::walker::builder::{EdgeWalkerBuilder, VertexWalkerBuilder};
3use crate::walker::steps::{
4 ContextRef, DefaultEdgeContext, DefaultVertexContext, EdgeContext, VertexContext,
5};
6use crate::walker::{EdgeWalker, VertexWalker};
7use include_doc::function_body;
8
9impl<'graph, Mutability, Graph, Walker> VertexWalkerBuilder<'graph, Mutability, Graph, Walker>
10where
11 Graph: crate::graph::Graph,
12 Walker: VertexWalker<'graph, Graph = Graph>,
13{
14 /// # Default Context Step
15 ///
16 /// The `push_default_context` step is a specialized version of the `push_context` step that
17 /// automatically stores the current element's ID and data in the context. This provides a
18 /// convenient way to preserve information about elements as you traverse through the graph.
19 ///
20 /// ## Visual Diagram
21 ///
22 /// Before push_default_context step (traversal with regular elements):
23 /// ```text
24 /// [Person A]* --- created ---> [Project X]*
25 /// |
26 /// knows
27 /// |
28 /// [Person B]*
29 /// ```
30 ///
31 /// After push_default_context step (elements with default context):
32 /// ```text
33 /// [Person A]* + {vertex_id: id_a, vertex: Person{name: "A", age: 30}} --- created ---> [Project X]* + {vertex_id: id_x, vertex: Project{name: "X"}}
34 /// |
35 /// knows
36 /// |
37 /// [Person B]* + {vertex_id: id_b, vertex: Person{name: "B", age: 25}}
38 /// ```
39 ///
40 /// ## Parameters
41 ///
42 /// None
43 ///
44 /// ## Return Value
45 ///
46 /// Returns a traversal with the same elements, but with each element's ID and data stored in its context.
47 ///
48 /// ## Example
49 ///
50 /// ```rust
51 #[doc = function_body!("examples/default_context.rs", basic_example, [])]
52 /// ```
53 ///
54 /// ## Notes
55 ///
56 /// - Default context for vertices includes:
57 /// - `vertex_id`: The ID of the vertex
58 /// - `vertex`: The vertex data (cloned from the graph)
59 /// - Type safety is maintained as the vertex/edge types are preserved
60 /// - This step requires that your vertex/edge types implement Clone + 'static
61 /// - More concise than manual context handling for common use cases
62 /// - Especially useful when you need to preserve information across multiple traversal steps
63 /// - Combines well with other context operations for complex data gathering
64 pub fn push_default_context(
65 self,
66 ) -> VertexWalkerBuilder<
67 'graph,
68 Mutability,
69 Graph,
70 VertexContext<
71 'graph,
72 Walker,
73 impl Fn(
74 &Graph::VertexReference<'_>,
75 &Walker::Context,
76 )
77 -> ContextRef<DefaultVertexContext<Graph::VertexId, Graph::Vertex>, Walker::Context>,
78 ContextRef<DefaultVertexContext<Graph::VertexId, Graph::Vertex>, Walker::Context>,
79 >,
80 >
81 where
82 Graph::Vertex: Clone + 'static,
83 {
84 self.push_context(|vertex, _context| DefaultVertexContext {
85 vertex_id: vertex.id(),
86 vertex: vertex.weight().clone(),
87 })
88 }
89}
90
91impl<'graph, Mutability, Graph, Walker> EdgeWalkerBuilder<'graph, Mutability, Graph, Walker>
92where
93 Graph: crate::graph::Graph,
94 Walker: EdgeWalker<'graph, Graph = Graph>,
95 <Walker as crate::walker::Walker<'graph>>::Context: Clone + 'static,
96{
97 /// # Default Context Step
98 ///
99 /// A specialized version of the `push_context` step for edges that automatically
100 /// stores the current edge's ID and data in the context.
101 ///
102 /// See the documentation for [`VertexWalkerBuilder::push_default_context`] for more details.
103 pub fn push_default_context(
104 self,
105 ) -> EdgeWalkerBuilder<
106 'graph,
107 Mutability,
108 Graph,
109 EdgeContext<
110 'graph,
111 Walker,
112 impl Fn(
113 &Graph::EdgeReference<'_>,
114 &Walker::Context,
115 )
116 -> ContextRef<DefaultEdgeContext<Graph::EdgeId, Graph::Edge>, Walker::Context>,
117 ContextRef<DefaultEdgeContext<Graph::EdgeId, Graph::Edge>, Walker::Context>,
118 >,
119 >
120 where
121 Graph::Edge: Clone + 'static,
122 {
123 self.push_context(|edge, _context| DefaultEdgeContext {
124 edge_id: edge.id(),
125 edge: edge.weight().clone(),
126 })
127 }
128}