PetCodeGraph

Struct PetCodeGraph 

Source
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

Source

pub fn new() -> Self

Create a new empty petgraph-based code graph

Source

pub fn schema_version(&self) -> &str

Get the schema version

Source

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.

Source

pub fn get_node(&self, id: &str) -> Option<&Node>

Get a node by its string ID

Source

pub fn get_node_mut(&mut self, id: &str) -> Option<&mut Node>

Get a mutable node by its string ID

Source

pub fn get_node_by_index(&self, idx: NodeIndex) -> Option<&Node>

Get a node by its NodeIndex

Source

pub fn get_node_index(&self, id: &str) -> Option<NodeIndex>

Get the NodeIndex for a node ID

Source

pub fn contains_node(&self, id: &str) -> bool

Check if the graph contains a node with the given ID

Source

pub fn remove_node(&mut self, id: &str) -> Option<Node>

Remove a node and all its incident edges

Source

pub fn node_count(&self) -> usize

Get the number of nodes

Source

pub fn iter_nodes(&self) -> impl Iterator<Item = &Node>

Iterate over all nodes

Source

pub fn nodes_by_type(&self, node_type: NodeType) -> impl Iterator<Item = &Node>

Get nodes by type

Source

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.

Source

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.

Source

pub fn add_edge_by_index( &mut self, source: NodeIndex, target: NodeIndex, data: EdgeData, ) -> EdgeIndex

Add an edge using NodeIndices directly

Source

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)

Source

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)

Source

pub fn edge_count(&self) -> usize

Get the number of edges

Source

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.

Source

pub fn edges_by_type( &self, edge_type: EdgeType, ) -> impl Iterator<Item = (&Node, &Node, &EdgeData)>

Get edges by type

Source

pub fn neighbors(&self, id: &str) -> impl Iterator<Item = &Node>

Get all neighbor nodes (both incoming and outgoing)

Source

pub fn children(&self, id: &str) -> impl Iterator<Item = &Node>

Get children (outgoing CONTAINS edges)

Source

pub fn parent(&self, id: &str) -> Option<&Node>

Get parent (incoming CONTAINS edge) - typically only one

Source

pub fn remove_file_nodes(&mut self, file_path: &str)

Remove all nodes from a file and their incident edges

Source

pub fn inner(&self) -> &StableGraph<Node, EdgeData, Directed>

Get a reference to the underlying petgraph

Source

pub fn inner_mut(&mut self) -> &mut StableGraph<Node, EdgeData, Directed>

Get a mutable reference to the underlying petgraph

Source

pub fn node_index_map(&self) -> &HashMap<String, NodeIndex>

Get a reference to the node index map

Trait Implementations§

Source§

impl Clone for PetCodeGraph

Source§

fn clone(&self) -> PetCodeGraph

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PetCodeGraph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for PetCodeGraph

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more