use std::path::PathBuf;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ChurnLevel {
Low,
Medium,
High,
}
impl ChurnLevel {
pub fn label(self) -> &'static str {
match self {
ChurnLevel::Low => "LOW",
ChurnLevel::Medium => "MEDIUM",
ChurnLevel::High => "HIGH",
}
}
}
pub struct FileChurn {
pub path: PathBuf,
pub language: String,
pub commits: usize,
pub rate: f64,
pub first_commit: i64,
pub last_commit: i64,
pub level: ChurnLevel,
}
const SECS_PER_MONTH: f64 = 30.0 * 24.0 * 3600.0;
pub fn classify(
path: PathBuf,
language: &str,
commits: usize,
first_commit: i64,
last_commit: i64,
) -> FileChurn {
let span_secs = (last_commit - first_commit).max(0) as f64;
let months = (span_secs / SECS_PER_MONTH).max(1.0);
let rate = commits as f64 / months;
let level = if rate > 4.0 {
ChurnLevel::High
} else if rate >= 1.0 {
ChurnLevel::Medium
} else {
ChurnLevel::Low
};
FileChurn {
path,
language: language.to_string(),
commits,
rate,
first_commit,
last_commit,
level,
}
}
#[cfg(test)]
#[path = "analyzer_test.rs"]
mod tests;