Skip to main content

Crate code_ranker_graph

Crate code_ranker_graph 

Source
Expand description

Operations over the generic property-graph model defined in code-ranker-plugin-api: cycle detection, Henry-Kafura coupling, aggregate stats, id relativization, and the serializable Snapshot artifact.

Everything here is language-agnostic. Plugins emit a pure api::Graph (structure only); this crate and the orchestrator enrich it (writing computed values into node attrs by id) and assemble the snapshot. Which edge kinds count as information flow is read from the level’s edge_kinds (EdgeKindSpec.flow), passed in as a flow_kinds set — there is no hardcoded uses/contains knowledge.

Re-exports§

pub use attrs::num_attr;
pub use attrs::round_sig3;
pub use cycles::annotate_cycles;
pub use finalize::finalize_graph;
pub use hk::annotate_coupling;
pub use level_graph::CycleGroup;
pub use level_graph::LevelGraph;
pub use level_graph::LevelUi;
pub use builtin::Views;
pub use builtin::coupling_specs;
pub use builtin::cycle_specs;
pub use builtin::metric_specs;
pub use builtin::prompt_template;
pub use builtin::prompt_template_from;
pub use builtin::stat_keys;
pub use builtin::views;
pub use builtin::write_derived;
pub use builtin::write_metrics;
pub use checks::CheckCompileError;
pub use checks::CheckDef;
pub use checks::CheckHit;
pub use checks::CompiledCheck;
pub use checks::GraphView;
pub use registry::Engine;
pub use registry::MetricDef;
pub use registry::Populations;
pub use registry::RegistryError;
pub use registry::Scope;
pub use registry::apply_to_node;
pub use relativize::relativize_graph;
pub use relativize::relativize_level;
pub use serialize::to_canonical_string;
pub use serialize::to_canonical_string_pretty;
pub use snapshot::GitInfo;
pub use snapshot::Snapshot;
pub use snapshot::StageTime;
pub use stats::compute_stats;

Modules§

attrs
Shared attribute helpers used by every enrichment pass: numeric rounding, the f64 → AttrValue bridge, typed attribute reads, and the external-node predicate. This is a leaf module — it depends only on the plugin API, never on the crate root, so the enrichment passes can pull helpers from here without creating a submodule → crate-root back-edge.
builtin
The metric catalog, read from metrics/builtin.toml: [categories.*], [ast.*] (tier-1 measured), [fields.*] (derived, each a formula_cel), and the [report] view section (+ [report.stats] aggregate formulas). The crate root re-exports the accessors below; the tier-1 input types (MetricInputs / FunctionUnit) come from code-ranker-plugin-api.
checks
Custom config-defined checks ([rules.checks.<id>]) — a config-only linter primitive.
cycles
Cycle detection over information-flow edges (Kosaraju SCC). Edges count iff their kind is in flow_kinds (derived from EdgeKindSpec.flow); structural kinds like contains are excluded, so a mod foo; parent/child pair is not flagged as a false cycle.
finalize
Canonicalize a freshly-parsed structural graph: drop self-loops, deduplicate edges on (source, target, kind), and prune external nodes that nothing references. Structural edges (e.g. contains) are preserved — they carry no flow but are kept for display and ownership.
hk
Coupling counts over flow edges. For each internal (non-external) node we count unique flow partners (fan_in / fan_out) and track outgoing edges to external libraries separately (fan_out_external). Results are written into node attrs as flat keys; zero values are omitted. The size-folding Henry–Kafura metric (hk = sloc · (fan_in · fan_out)²) is no longer computed here — it is a graph-derived [fields.hk] CEL formula evaluated by builtin::write_derived once these counts are on the node.
level_graph
Per-level payload types: LevelGraph (one analysis level’s structural graph + semantics dictionaries + computed cycles/stats/UI), LevelUi (computed UI hints), and CycleGroup (one classified SCC). These are the widely-imported model types; keeping them in their own module spreads their fan-in off the crate’s snapshot artifact.
registry
Declarative metric registry — the data-driven home for tier-2+ formulas.
relativize
Id relativization: rewrite absolute file-path node ids to portable {target}/rel / {root}/rel tokens (and follow them through edges, parents, cycle node lists, and external path attributes), so a snapshot is machine-independent.
serialize
Canonical (deterministic) JSON serialization. Object keys come out alphabetically (serde_json::Value is backed by a BTreeMap) and the nodes/edges arrays are sorted by a stable key, so unchanged input is byte-identical across runs. Generic over any Serialize — depends on nothing else in the crate.
snapshot
The serializable analysis artifact (Snapshot) and its header types (GitInfo, StageTime).
stats
Per-graph aggregate stats: the mean of each tracked numeric metric across the project’s file nodes. Zero/missing values are excluded from a metric’s average (matching the historical behavior); a metric is emitted only when its average is positive.