1use crate::graph::{AvgCoupling, Graph, GraphStats, Halstead, Loc, Maintainability, Node};
2
3pub fn annotate_stats(graph: &mut Graph) {
4 fn avg<F>(nodes: &[Node], f: F) -> f64
5 where
6 F: Fn(&Node) -> Option<f64>,
7 {
8 let vals: Vec<f64> = nodes
9 .iter()
10 .filter_map(f)
11 .filter(|v| v.is_finite() && *v > 0.0)
12 .collect();
13 if vals.is_empty() {
14 return 0.0;
15 }
16 vals.iter().sum::<f64>() / vals.len() as f64
17 }
18
19 let nodes = &graph.nodes;
20
21 let cyclomatic = avg(nodes, |n| n.complexity.as_ref().map(|c| c.cyclomatic));
22 let cognitive = avg(nodes, |n| n.complexity.as_ref().map(|c| c.cognitive));
23
24 let fan_in = avg(nodes, |n| {
25 n.complexity
26 .as_ref()
27 .and_then(|c| c.coupling.as_ref())
28 .map(|c| c.fan_in as f64)
29 });
30 let fan_out = avg(nodes, |n| {
31 n.complexity
32 .as_ref()
33 .and_then(|c| c.coupling.as_ref())
34 .map(|c| c.fan_out as f64)
35 });
36 let hk = avg(nodes, |n| {
37 n.complexity
38 .as_ref()
39 .and_then(|c| c.coupling.as_ref())
40 .map(|c| c.hk)
41 });
42 let coupling = (fan_in > 0.0 || fan_out > 0.0 || hk > 0.0).then_some(AvgCoupling {
43 fan_in,
44 fan_out,
45 hk,
46 });
47
48 let mi = avg(nodes, |n| {
49 n.complexity
50 .as_ref()
51 .and_then(|c| c.maintainability.as_ref())
52 .map(|m| m.mi)
53 });
54 let mi_sei = avg(nodes, |n| {
55 n.complexity
56 .as_ref()
57 .and_then(|c| c.maintainability.as_ref())
58 .map(|m| m.mi_sei)
59 });
60 let maintainability = (mi > 0.0).then_some(Maintainability { mi, mi_sei });
61
62 let loc_source = avg(nodes, |n| {
63 n.complexity
64 .as_ref()
65 .and_then(|c| c.loc.as_ref())
66 .map(|l| l.source)
67 });
68 let loc_comments = avg(nodes, |n| {
69 n.complexity
70 .as_ref()
71 .and_then(|c| c.loc.as_ref())
72 .map(|l| l.comments)
73 });
74 let loc_blank = avg(nodes, |n| {
75 n.complexity
76 .as_ref()
77 .and_then(|c| c.loc.as_ref())
78 .map(|l| l.blank)
79 });
80 let loc = (loc_source > 0.0).then_some(Loc {
81 source: loc_source,
82 logical: 0.0,
83 comments: loc_comments,
84 blank: loc_blank,
85 });
86
87 let h_length = avg(nodes, |n| {
88 n.complexity
89 .as_ref()
90 .and_then(|c| c.halstead.as_ref())
91 .map(|h| h.length)
92 });
93 let h_vocabulary = avg(nodes, |n| {
94 n.complexity
95 .as_ref()
96 .and_then(|c| c.halstead.as_ref())
97 .map(|h| h.vocabulary)
98 });
99 let h_volume = avg(nodes, |n| {
100 n.complexity
101 .as_ref()
102 .and_then(|c| c.halstead.as_ref())
103 .map(|h| h.volume)
104 });
105 let h_effort = avg(nodes, |n| {
106 n.complexity
107 .as_ref()
108 .and_then(|c| c.halstead.as_ref())
109 .map(|h| h.effort)
110 });
111 let h_time = avg(nodes, |n| {
112 n.complexity
113 .as_ref()
114 .and_then(|c| c.halstead.as_ref())
115 .map(|h| h.time)
116 });
117 let h_bugs = avg(nodes, |n| {
118 n.complexity
119 .as_ref()
120 .and_then(|c| c.halstead.as_ref())
121 .map(|h| h.bugs)
122 });
123 let halstead = (h_volume > 0.0).then_some(Halstead {
124 length: h_length,
125 vocabulary: h_vocabulary,
126 volume: h_volume,
127 effort: h_effort,
128 time: h_time,
129 bugs: h_bugs,
130 });
131
132 if cyclomatic == 0.0
133 && cognitive == 0.0
134 && coupling.is_none()
135 && maintainability.is_none()
136 && loc.is_none()
137 && halstead.is_none()
138 {
139 return;
140 }
141
142 graph.stats = Some(GraphStats {
143 cyclomatic,
144 cognitive,
145 coupling,
146 maintainability,
147 loc,
148 halstead,
149 });
150}