use std::collections::BTreeMap;
use serde::Serialize;
#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SourceKind {
Production,
UnitTest,
IntegrationTest,
Bench,
Example,
Build,
#[default]
Other,
}
impl SourceKind {
#[must_use]
pub(crate) const fn label(self) -> &'static str {
match self {
Self::Production => "prod",
Self::UnitTest => "unit",
Self::IntegrationTest => "integ",
Self::Bench => "bench",
Self::Example => "example",
Self::Build => "build",
Self::Other => "other",
}
}
#[must_use]
pub(crate) const fn is_test(self) -> bool {
matches!(self, Self::UnitTest | Self::IntegrationTest | Self::Bench)
}
}
#[derive(Clone, Copy, Debug, Default, Serialize)]
pub struct LineStats {
pub code: usize,
pub comments: usize,
pub blanks: usize,
}
impl LineStats {
#[must_use]
pub const fn lines(self) -> usize {
self.code + self.comments + self.blanks
}
pub(crate) fn add_assign(&mut self, other: Self) {
self.code += other.code;
self.comments += other.comments;
self.blanks += other.blanks;
}
}
#[derive(Clone, Copy, Debug, Default, Serialize)]
pub struct Churn {
pub added: u64,
pub deleted: u64,
}
impl Churn {
#[must_use]
pub const fn changed(self) -> u64 {
self.added + self.deleted
}
pub(crate) fn add(&mut self, added: u64, deleted: u64) {
self.added += added;
self.deleted += deleted;
}
}
#[derive(Clone, Debug, Default, Serialize)]
pub struct StructureMetrics {
pub control_tokens: usize,
pub max_brace_depth: usize,
pub max_function_lines: usize,
pub function_count: usize,
pub complexity_raw: f64,
pub context_cost_raw: f64,
}
#[derive(Clone, Debug, Serialize)]
pub struct FileMetrics {
pub path: String,
pub language: String,
pub crate_name: Option<String>,
pub kind: SourceKind,
pub lines: LineStats,
pub inline_test_lines: LineStats,
pub churn_short: Churn,
pub churn_long: Churn,
pub structure: StructureMetrics,
pub hotspot_score: f64,
}
impl FileMetrics {
#[must_use]
pub fn production_code(&self) -> usize {
if self.kind == SourceKind::Production {
self.lines.code.saturating_sub(self.inline_test_lines.code)
} else {
0
}
}
#[must_use]
pub fn test_code(&self) -> usize {
let whole_file = usize::from(self.kind.is_test()) * self.lines.code;
whole_file + self.inline_test_lines.code
}
}
#[derive(Clone, Debug, Default, Serialize)]
pub struct LanguageSummary {
pub files: usize,
pub lines: LineStats,
}
#[derive(Clone, Debug, Default, Serialize)]
pub struct CrateSummary {
pub name: String,
pub files: usize,
pub production_code: usize,
pub unit_test_code: usize,
pub integration_test_code: usize,
pub bench_code: usize,
pub example_code: usize,
pub build_code: usize,
pub other_code: usize,
pub churn_short: u64,
pub churn_long: u64,
}
impl CrateSummary {
#[must_use]
pub const fn test_code(&self) -> usize {
self.unit_test_code + self.integration_test_code + self.bench_code
}
#[must_use]
pub const fn total_code(&self) -> usize {
self.production_code
+ self.test_code()
+ self.example_code
+ self.build_code
+ self.other_code
}
}
#[derive(Clone, Debug, Default, Serialize)]
pub struct ThroughputSummary {
pub days: u32,
pub added: u64,
pub deleted: u64,
pub changed: u64,
pub commits: usize,
pub active_days: usize,
}
#[derive(Clone, Debug, Default, Serialize)]
pub struct DailyThroughput {
pub date: String,
pub added: u64,
pub deleted: u64,
pub changed: u64,
pub commits: usize,
}
impl DailyThroughput {
pub(crate) fn add(&mut self, added: u64, deleted: u64) {
self.added += added;
self.deleted += deleted;
self.changed += added + deleted;
}
}
#[derive(Clone, Debug, Default, Serialize)]
pub struct ProjectSummary {
pub files: usize,
pub crates: usize,
pub lines: LineStats,
pub production_code: usize,
pub test_code: usize,
}
#[derive(Clone, Debug, Serialize)]
pub struct AnalysisReport {
pub schema_version: u32,
pub project_root: String,
pub git_root: Option<String>,
pub short_days: u32,
pub long_days: u32,
pub summary: ProjectSummary,
pub throughput_short: ThroughputSummary,
pub throughput_long: ThroughputSummary,
pub daily_long: Vec<DailyThroughput>,
pub languages: BTreeMap<String, LanguageSummary>,
pub crates: Vec<CrateSummary>,
pub files: Vec<FileMetrics>,
pub warnings: Vec<String>,
}