Skip to main content

DependencyGraph

Struct DependencyGraph 

Source
pub struct DependencyGraph {
    pub nodes: RapidMap<PathBuf, AnalysisDefFingerprint>,
    pub edges: Vec<DependencyEdge>,
    /* private fields */
}
Expand description

A dependency graph tracking relationships between source files.

The graph is directed: edges point from dependent files to their dependencies. For example, if main.rs imports utils.rs, there is an edge from main.rs to utils.rs.

The graph maintains both forward (dependencies) and reverse (dependents) adjacency lists for efficient bidirectional traversal.

§Examples

use thread_flow::incremental::graph::DependencyGraph;
use thread_flow::incremental::types::{DependencyEdge, DependencyType};
use std::path::PathBuf;
use thread_utilities::RapidSet;

let mut graph = DependencyGraph::new();

// main.rs depends on utils.rs
graph.add_edge(DependencyEdge::new(
    PathBuf::from("main.rs"),
    PathBuf::from("utils.rs"),
    DependencyType::Import,
));

// Find what main.rs depends on
let deps = graph.get_dependencies(&PathBuf::from("main.rs"));
assert_eq!(deps.len(), 1);
assert_eq!(deps[0].to, PathBuf::from("utils.rs"));

// Find what depends on utils.rs
let dependents = graph.get_dependents(&PathBuf::from("utils.rs"));
assert_eq!(dependents.len(), 1);
assert_eq!(dependents[0].from, PathBuf::from("main.rs"));

Fields§

§nodes: RapidMap<PathBuf, AnalysisDefFingerprint>

Fingerprint state for each tracked file.

§edges: Vec<DependencyEdge>

All dependency edges in the graph.

Implementations§

Source§

impl DependencyGraph

Source

pub fn new() -> Self

Creates a new empty dependency graph.

§Examples
use thread_flow::incremental::graph::DependencyGraph;

let graph = DependencyGraph::new();
assert_eq!(graph.node_count(), 0);
assert_eq!(graph.edge_count(), 0);
Source

pub fn add_node(&mut self, file: &Path)

Ensures a file node exists in the graph without adding any edges.

This is useful when a file has been processed but no dependency edges were extracted (e.g., a file with no imports, or a Go file where all imports resolve to external packages without a configured module path).

§Arguments
  • file - Path of the file to add as a node.
§Examples
use thread_flow::incremental::graph::DependencyGraph;
use std::path::Path;

let mut graph = DependencyGraph::new();
graph.add_node(Path::new("main.go"));
assert!(graph.contains_node(Path::new("main.go")));
assert_eq!(graph.node_count(), 1);
assert_eq!(graph.edge_count(), 0);
Source

pub fn add_edge(&mut self, edge: DependencyEdge)

Adds a dependency edge to the graph.

Both the source (from) and target (to) nodes are automatically registered if they do not already exist. Adjacency lists are updated for both forward and reverse lookups.

§Arguments
  • edge - The dependency edge to add.
§Examples
use thread_flow::incremental::graph::DependencyGraph;
use thread_flow::incremental::types::{DependencyEdge, DependencyType};
use std::path::PathBuf;

let mut graph = DependencyGraph::new();
graph.add_edge(DependencyEdge::new(
    PathBuf::from("a.rs"),
    PathBuf::from("b.rs"),
    DependencyType::Import,
));
assert_eq!(graph.edge_count(), 1);
assert_eq!(graph.node_count(), 2);
Source

pub fn get_dependencies(&self, file: &Path) -> Vec<&DependencyEdge>

Returns all direct dependencies of a file (files it depends on).

§Arguments
  • file - The file to query dependencies for.
§Returns

A vector of references to dependency edges where from is the given file.

Source

pub fn get_dependents(&self, file: &Path) -> Vec<&DependencyEdge>

Returns all direct dependents of a file (files that depend on it).

§Arguments
  • file - The file to query dependents for.
§Returns

A vector of references to dependency edges where to is the given file.

Source

pub fn find_affected_files( &self, changed_files: &RapidSet<PathBuf>, ) -> RapidSet<PathBuf>

Finds all files affected by changes to the given set of files.

Uses BFS traversal following reverse dependency edges (dependents) to discover the full set of files that need reanalysis. Only DependencyStrength::Strong edges trigger cascading invalidation.

Algorithm complexity: O(V + E) where V = files, E = dependency edges.

§Arguments
  • changed_files - Set of files that have been modified.
§Returns

Set of all affected files, including the changed files themselves.

§Examples
use thread_flow::incremental::graph::DependencyGraph;
use thread_flow::incremental::types::{DependencyEdge, DependencyType};
use std::path::PathBuf;
use thread_utilities::RapidSet;

let mut graph = DependencyGraph::new();

// A -> B -> C (A depends on B, B depends on C)
graph.add_edge(DependencyEdge::new(
    PathBuf::from("A"), PathBuf::from("B"), DependencyType::Import,
));
graph.add_edge(DependencyEdge::new(
    PathBuf::from("B"), PathBuf::from("C"), DependencyType::Import,
));

// Change C -> affects B and A
let changed = RapidSet::from([PathBuf::from("C")]);
let affected = graph.find_affected_files(&changed);
assert!(affected.contains(&PathBuf::from("A")));
assert!(affected.contains(&PathBuf::from("B")));
assert!(affected.contains(&PathBuf::from("C")));
Source

pub fn topological_sort( &self, files: &RapidSet<PathBuf>, ) -> Result<Vec<PathBuf>, GraphError>

Performs topological sort on the given subset of files.

Returns files in dependency order: dependencies appear before their dependents. This ordering ensures correct incremental reanalysis.

Detects cyclic dependencies and returns GraphError::CyclicDependency if a cycle is found.

Algorithm complexity: O(V + E) using DFS.

§Arguments
  • files - The subset of files to sort.
§Errors

Returns GraphError::CyclicDependency if a cycle is detected.

§Examples
use thread_flow::incremental::graph::DependencyGraph;
use thread_flow::incremental::types::{DependencyEdge, DependencyType};
use std::path::PathBuf;
use thread_utilities::RapidSet;

let mut graph = DependencyGraph::new();
// A depends on B, B depends on C
graph.add_edge(DependencyEdge::new(
    PathBuf::from("A"), PathBuf::from("B"), DependencyType::Import,
));
graph.add_edge(DependencyEdge::new(
    PathBuf::from("B"), PathBuf::from("C"), DependencyType::Import,
));

let files = RapidSet::from([
    PathBuf::from("A"), PathBuf::from("B"), PathBuf::from("C"),
]);
let sorted = graph.topological_sort(&files).unwrap();
// C should come before B, B before A
let pos_a = sorted.iter().position(|p| p == &PathBuf::from("A")).unwrap();
let pos_b = sorted.iter().position(|p| p == &PathBuf::from("B")).unwrap();
let pos_c = sorted.iter().position(|p| p == &PathBuf::from("C")).unwrap();
assert!(pos_c < pos_b);
assert!(pos_b < pos_a);
Source

pub fn node_count(&self) -> usize

Returns the number of nodes (files) in the graph.

Source

pub fn edge_count(&self) -> usize

Returns the number of edges in the graph.

Source

pub fn contains_node(&self, file: &Path) -> bool

Checks whether the graph contains a node for the given file.

Source

pub fn validate(&self) -> Result<(), GraphError>

Validates graph integrity.

Checks for dangling edges (edges referencing nodes not in the graph) and other structural issues.

§Returns

Ok(()) if the graph is structurally valid, or a GraphError otherwise.

Source

pub fn clear(&mut self)

Removes all edges and nodes from the graph.

Trait Implementations§

Source§

impl Clone for DependencyGraph

Source§

fn clone(&self) -> DependencyGraph

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 DependencyGraph

Source§

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

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

impl Default for DependencyGraph

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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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