code_ranker_graph/level_graph.rs
1//! Per-level payload types: [`LevelGraph`] (one analysis level's structural
2//! graph + semantics dictionaries + computed cycles/stats/UI), [`LevelUi`]
3//! (computed UI hints), and [`CycleGroup`] (one classified SCC). These are the
4//! widely-imported model types; keeping them in their own module spreads their
5//! fan-in off the crate's `snapshot` artifact.
6
7use code_ranker_plugin_api::{
8 attrs::AttrValue,
9 edge::Edge,
10 level::{AttributeGroup, AttributeSpec, CycleKindSpec, EdgeKindSpec, Grouping, NodeKindSpec},
11 node::{Node, NodeId},
12};
13use serde::{Deserialize, Serialize};
14use std::collections::BTreeMap;
15
16/// UI hints for a level: which metrics to offer as table columns, summary rows,
17/// sort/size keys, and the default sort — computed by the orchestrator from the
18/// attributes actually present, so the viewer hardcodes none of it.
19#[derive(Debug, Clone, Default, Serialize, Deserialize)]
20pub struct LevelUi {
21 #[serde(default, skip_serializing_if = "Option::is_none")]
22 pub default_sort: Option<String>,
23 pub sort: Vec<String>,
24 /// Metrics the SVG map offers as circle-size modes, beyond the built-in
25 /// `loc` / `hk` (from a `[report] size = …` override). Default empty.
26 pub size: Vec<String>,
27 /// Metrics the SVG map offers as on/off node filters (keep only nodes where
28 /// the metric has signal), beyond the built-in `cycle` (from `[report] filter`).
29 #[serde(default)]
30 pub filter: Vec<String>,
31 pub card: Vec<String>,
32 pub columns: Vec<String>,
33 pub summary: Vec<String>,
34 /// How the viewer should cluster nodes (group by attribute `key`, or a named
35 /// `function`). Carried through from the plugin's level spec, pruned to a
36 /// valid attribute. Absent → the viewer uses its default `dir` grouper.
37 #[serde(default, skip_serializing_if = "Option::is_none")]
38 pub grouping: Option<Grouping>,
39}
40
41/// One strongly-connected component with ≥ 2 nodes, plus its classification
42/// (`"mutual"` for a 2-node SCC, `"chain"` for 3+). Node ids match the level graph.
43#[derive(Debug, Clone, Serialize, Deserialize)]
44pub struct CycleGroup {
45 pub kind: String,
46 pub nodes: Vec<NodeId>,
47}
48
49/// Everything for one analysis level: the structural graph, the semantics
50/// dictionaries that describe its vocabulary, and the computed cycles + stats.
51#[derive(Debug, Clone, Default, Serialize, Deserialize)]
52pub struct LevelGraph {
53 /// Edge kinds present at this level (keyed by kind), with `flow` semantics.
54 pub edge_kinds: BTreeMap<String, EdgeKindSpec>,
55 /// Node attribute dictionary (structural keys + appended computed metrics).
56 pub node_attributes: BTreeMap<String, AttributeSpec>,
57 /// Edge attribute dictionary.
58 pub edge_attributes: BTreeMap<String, AttributeSpec>,
59 /// Attribute group definitions referenced by `AttributeSpec.group`.
60 pub attribute_groups: BTreeMap<String, AttributeGroup>,
61 /// Node-kind vocabulary (label/colour/external).
62 pub node_kinds: BTreeMap<String, NodeKindSpec>,
63 /// Cycle-kind vocabulary.
64 pub cycle_kinds: BTreeMap<String, CycleKindSpec>,
65 pub nodes: Vec<Node>,
66 pub edges: Vec<Edge>,
67 /// SCCs with ≥ 2 members, classified by kind.
68 pub cycles: Vec<CycleGroup>,
69 /// Per-graph averages of numeric node attributes (flat, keyed by attr name).
70 pub stats: BTreeMap<String, AttrValue>,
71 /// Computed UI hints (column/sort/size/card ordering).
72 pub ui: LevelUi,
73}