pub struct PetCodeGraph { /* private fields */ }Expand description
A petgraph-based code graph for efficient traversal and graph algorithms.
This implementation uses petgraph::StableGraph which:
- Supports O(1) neighbor access via adjacency lists
- Provides stable indices (node/edge removal doesn’t invalidate others)
- Enables built-in graph algorithms (BFS, DFS, topological sort, etc.)
Use this for runtime operations that require graph traversal.
Implementations§
Source§impl PetCodeGraph
impl PetCodeGraph
Sourcepub fn schema_version(&self) -> &str
pub fn schema_version(&self) -> &str
Get the schema version
Sourcepub fn add_node(&mut self, node: Node) -> NodeIndex
pub fn add_node(&mut self, node: Node) -> NodeIndex
Add a node to the graph, returning its NodeIndex.
If a node with the same ID already exists, it will be replaced.
Sourcepub fn get_node_mut(&mut self, id: &str) -> Option<&mut Node>
pub fn get_node_mut(&mut self, id: &str) -> Option<&mut Node>
Get a mutable node by its string ID
Sourcepub fn get_node_by_index(&self, idx: NodeIndex) -> Option<&Node>
pub fn get_node_by_index(&self, idx: NodeIndex) -> Option<&Node>
Get a node by its NodeIndex
Sourcepub fn get_node_index(&self, id: &str) -> Option<NodeIndex>
pub fn get_node_index(&self, id: &str) -> Option<NodeIndex>
Get the NodeIndex for a node ID
Sourcepub fn contains_node(&self, id: &str) -> bool
pub fn contains_node(&self, id: &str) -> bool
Check if the graph contains a node with the given ID
Sourcepub fn remove_node(&mut self, id: &str) -> Option<Node>
pub fn remove_node(&mut self, id: &str) -> Option<Node>
Remove a node and all its incident edges
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Get the number of nodes
Sourcepub fn iter_nodes(&self) -> impl Iterator<Item = &Node>
pub fn iter_nodes(&self) -> impl Iterator<Item = &Node>
Iterate over all nodes
Sourcepub fn nodes_by_type(&self, node_type: NodeType) -> impl Iterator<Item = &Node>
pub fn nodes_by_type(&self, node_type: NodeType) -> impl Iterator<Item = &Node>
Get nodes by type
Sourcepub fn add_edge(
&mut self,
source_id: &str,
target_id: &str,
data: EdgeData,
) -> Option<EdgeIndex>
pub fn add_edge( &mut self, source_id: &str, target_id: &str, data: EdgeData, ) -> Option<EdgeIndex>
Add an edge between two nodes by their string IDs.
Returns Some(EdgeIndex) if both nodes exist, None otherwise.
Sourcepub fn add_edge_from_struct(&mut self, edge: &Edge) -> Option<EdgeIndex>
pub fn add_edge_from_struct(&mut self, edge: &Edge) -> Option<EdgeIndex>
Add an edge using an Edge struct.
Returns Some(EdgeIndex) if both nodes exist, None otherwise.
Sourcepub fn add_edge_by_index(
&mut self,
source: NodeIndex,
target: NodeIndex,
data: EdgeData,
) -> EdgeIndex
pub fn add_edge_by_index( &mut self, source: NodeIndex, target: NodeIndex, data: EdgeData, ) -> EdgeIndex
Add an edge using NodeIndices directly
Sourcepub fn incoming_edges(
&self,
id: &str,
) -> impl Iterator<Item = (&Node, &EdgeData)>
pub fn incoming_edges( &self, id: &str, ) -> impl Iterator<Item = (&Node, &EdgeData)>
Get all incoming edges for a node (edges where this node is the target)
Sourcepub fn outgoing_edges(
&self,
id: &str,
) -> impl Iterator<Item = (&Node, &EdgeData)>
pub fn outgoing_edges( &self, id: &str, ) -> impl Iterator<Item = (&Node, &EdgeData)>
Get all outgoing edges from a node (edges where this node is the source)
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Get the number of edges
Sourcepub fn iter_edges(&self) -> impl Iterator<Item = Edge> + '_
pub fn iter_edges(&self) -> impl Iterator<Item = Edge> + '_
Iterate over all edges, returning Edge structs.
Note: This creates Edge structs on-the-fly. For performance-critical code,
consider using edges_by_type() or outgoing_edges() instead.
Sourcepub fn edges_by_type(
&self,
edge_type: EdgeType,
) -> impl Iterator<Item = (&Node, &Node, &EdgeData)>
pub fn edges_by_type( &self, edge_type: EdgeType, ) -> impl Iterator<Item = (&Node, &Node, &EdgeData)>
Get edges by type
Sourcepub fn neighbors(&self, id: &str) -> impl Iterator<Item = &Node>
pub fn neighbors(&self, id: &str) -> impl Iterator<Item = &Node>
Get all neighbor nodes (both incoming and outgoing)
Sourcepub fn children(&self, id: &str) -> impl Iterator<Item = &Node>
pub fn children(&self, id: &str) -> impl Iterator<Item = &Node>
Get children (outgoing CONTAINS edges)
Sourcepub fn parent(&self, id: &str) -> Option<&Node>
pub fn parent(&self, id: &str) -> Option<&Node>
Get parent (incoming CONTAINS edge) - typically only one
Sourcepub fn remove_file_nodes(&mut self, file_path: &str)
pub fn remove_file_nodes(&mut self, file_path: &str)
Remove all nodes from a file and their incident edges
Sourcepub fn inner(&self) -> &StableGraph<Node, EdgeData, Directed>
pub fn inner(&self) -> &StableGraph<Node, EdgeData, Directed>
Get a reference to the underlying petgraph
Sourcepub fn inner_mut(&mut self) -> &mut StableGraph<Node, EdgeData, Directed>
pub fn inner_mut(&mut self) -> &mut StableGraph<Node, EdgeData, Directed>
Get a mutable reference to the underlying petgraph
Sourcepub fn node_index_map(&self) -> &HashMap<String, NodeIndex>
pub fn node_index_map(&self) -> &HashMap<String, NodeIndex>
Get a reference to the node index map
Trait Implementations§
Source§impl Clone for PetCodeGraph
impl Clone for PetCodeGraph
Source§fn clone(&self) -> PetCodeGraph
fn clone(&self) -> PetCodeGraph
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for PetCodeGraph
impl Debug for PetCodeGraph
Auto Trait Implementations§
impl Freeze for PetCodeGraph
impl RefUnwindSafe for PetCodeGraph
impl Send for PetCodeGraph
impl Sync for PetCodeGraph
impl Unpin for PetCodeGraph
impl UnwindSafe for PetCodeGraph
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more