pub struct RuleDependencyGraph {
pub rules: HashSet<String>,
pub deps: Vec<RuleDependency>,
}Expand description
A directed graph that records dependencies between rules and can derive a safe topological evaluation order.
Nodes are rule IDs (plain Strings kept in a HashSet for O(1)
membership tests). Edges are RuleDependency values stored in a
Vec.
§Invariants
- Both endpoints of every
RuleDependencymust already be registered viaadd_rulebefore the edge can be added. - Duplicate rule IDs are rejected with
DepError::DuplicateRule.
Fields§
§rules: HashSet<String>Set of registered rule IDs.
deps: Vec<RuleDependency>All dependency edges.
Implementations§
Source§impl RuleDependencyGraph
impl RuleDependencyGraph
Sourcepub fn add_rule(&mut self, id: &str) -> Result<(), DepError>
pub fn add_rule(&mut self, id: &str) -> Result<(), DepError>
Register a new rule.
§Errors
Returns DepError::DuplicateRule if a rule with the same ID is
already present.
Sourcepub fn add_dependency(
&mut self,
from: &str,
to: &str,
dep_type: DependencyType,
) -> Result<(), DepError>
pub fn add_dependency( &mut self, from: &str, to: &str, dep_type: DependencyType, ) -> Result<(), DepError>
Add a directed dependency edge from → to (meaning from depends
on to).
§Errors
Returns DepError::DependencyEndpointMissing if either endpoint has
not been registered.
Sourcepub fn topological_sort(&self) -> Result<Vec<String>, DepError>
pub fn topological_sort(&self) -> Result<Vec<String>, DepError>
Compute a topological ordering of all registered rules using Kahn’s algorithm.
The ordering guarantees that for every dependency edge from → to,
to appears before from in the returned vector.
§Errors
Returns DepError::CycleDetected if the graph contains a cycle.
Sourcepub fn dependencies_of(&self, rule_id: &str) -> Vec<String>
pub fn dependencies_of(&self, rule_id: &str) -> Vec<String>
Return the IDs of rules that rule_id directly depends on (i.e. the
to endpoints of all edges whose from is rule_id).
Sourcepub fn dependents_of(&self, rule_id: &str) -> Vec<String>
pub fn dependents_of(&self, rule_id: &str) -> Vec<String>
Return the IDs of rules that directly depend on rule_id (i.e. the
from endpoints of all edges whose to is rule_id).
Sourcepub fn has_cycle(&self) -> bool
pub fn has_cycle(&self) -> bool
Return true if the dependency graph contains at least one cycle.
Sourcepub fn rule_count(&self) -> usize
pub fn rule_count(&self) -> usize
Return the number of registered rules.
Trait Implementations§
Source§impl Debug for RuleDependencyGraph
impl Debug for RuleDependencyGraph
Source§impl Default for RuleDependencyGraph
impl Default for RuleDependencyGraph
Source§fn default() -> RuleDependencyGraph
fn default() -> RuleDependencyGraph
Auto Trait Implementations§
impl Freeze for RuleDependencyGraph
impl RefUnwindSafe for RuleDependencyGraph
impl Send for RuleDependencyGraph
impl Sync for RuleDependencyGraph
impl Unpin for RuleDependencyGraph
impl UnsafeUnpin for RuleDependencyGraph
impl UnwindSafe for RuleDependencyGraph
Blanket Implementations§
impl<T> Allocation for T
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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