Skip to main content

cha_core/
lib.rs

1mod baseline;
2mod cache;
3pub mod config;
4pub mod graph;
5mod health;
6pub mod html_reporter;
7mod ignore;
8mod model;
9mod plugin;
10pub mod plugins;
11mod registry;
12pub mod reporter;
13mod source;
14pub mod wasm;
15
16pub use baseline::Baseline;
17pub use cache::{FileStatus, ProjectCache, env_hash, hash_content};
18pub use config::{
19    Config, DebtWeights, LanguageConfig, LayersConfig, Strictness, TierConfig,
20    builtin_language_profile,
21};
22pub use health::{Grade, HealthScore, score_files};
23pub use ignore::filter_ignored;
24pub use model::*;
25pub use plugin::*;
26pub use registry::PluginRegistry;
27pub use reporter::{JsonReporter, LlmContextReporter, Reporter, SarifReporter, TerminalReporter};
28pub use source::*;
29
30/// Helper for serde skip_serializing_if on f64 fields.
31pub fn is_zero_f64(v: &f64) -> bool {
32    *v == 0.0
33}
34
35/// Helper for serde skip_serializing_if on usize fields.
36pub fn is_zero_usize(v: &usize) -> bool {
37    *v == 0
38}
39
40/// Sort findings by priority descending (most important first).
41/// Writes the computed score back to `Finding::risk_score` so reporters can
42/// show *why* a finding ranks where it does.
43/// priority = severity_weight × overshoot × compound_factor
44pub fn prioritize_findings(findings: &mut [Finding]) {
45    let per_file: std::collections::HashMap<std::path::PathBuf, usize> = {
46        let mut m = std::collections::HashMap::new();
47        for f in findings.iter() {
48            *m.entry(f.location.path.clone()).or_default() += 1;
49        }
50        m
51    };
52    for f in findings.iter_mut() {
53        f.risk_score = Some(finding_priority(f, &per_file));
54    }
55    findings.sort_by(|a, b| {
56        let pa = a.risk_score.unwrap_or(0.0);
57        let pb = b.risk_score.unwrap_or(0.0);
58        pb.partial_cmp(&pa).unwrap_or(std::cmp::Ordering::Equal)
59    });
60}
61
62fn finding_priority(
63    f: &Finding,
64    per_file: &std::collections::HashMap<std::path::PathBuf, usize>,
65) -> f64 {
66    let sev = match f.severity {
67        Severity::Error => 3.0,
68        Severity::Warning => 2.0,
69        Severity::Hint => 1.0,
70    };
71    let overshoot = match (f.actual_value, f.threshold) {
72        (Some(a), Some(t)) if t > 0.0 => (a / t).max(1.0),
73        _ => 1.0,
74    };
75    let compound = if *per_file.get(&f.location.path).unwrap_or(&1) > 3 {
76        1.5
77    } else {
78        1.0
79    };
80    sev * overshoot * compound
81}
82
83/// Generate JSON Schema for the analysis output (list of findings).
84pub fn findings_json_schema() -> String {
85    let schema = schemars::schema_for!(Vec<Finding>);
86    serde_json::to_string_pretty(&schema).unwrap_or_default()
87}
88
89#[cfg(test)]
90mod tests;