use super::tokenizer::TokenCounts;
#[derive(Debug, Clone)]
pub struct HalsteadMetrics {
pub distinct_operators: usize, pub distinct_operands: usize, pub total_operators: usize, pub total_operands: usize, pub vocabulary: usize, pub length: usize, pub volume: f64, pub difficulty: f64, pub effort: f64, pub bugs: f64, pub time: f64, }
pub fn compute(counts: &TokenCounts) -> Option<HalsteadMetrics> {
let n1 = counts.distinct_operators.len();
let n2 = counts.distinct_operands.len();
let big_n1 = counts.total_operators;
let big_n2 = counts.total_operands;
let vocabulary = n1 + n2;
if n1 == 0 || n2 == 0 {
return None;
}
let length = big_n1 + big_n2;
let volume = length as f64 * (vocabulary as f64).log2();
let difficulty = (n1 as f64 / 2.0) * (big_n2 as f64 / n2 as f64);
let effort = difficulty * volume;
let bugs = volume / 3000.0;
let time = effort / 18.0;
Some(HalsteadMetrics {
distinct_operators: n1,
distinct_operands: n2,
total_operators: big_n1,
total_operands: big_n2,
vocabulary,
length,
volume,
difficulty,
effort,
bugs,
time,
})
}
#[cfg(test)]
#[path = "analyzer_test.rs"]
mod tests;