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/// Sort findings by priority descending (most important first).
36/// priority = severity_weight × overshoot × compound_factor
37pub fn prioritize_findings(findings: &mut [Finding]) {
38    let per_file: std::collections::HashMap<std::path::PathBuf, usize> = {
39        let mut m = std::collections::HashMap::new();
40        for f in findings.iter() {
41            *m.entry(f.location.path.clone()).or_default() += 1;
42        }
43        m
44    };
45    findings.sort_by(|a, b| {
46        let pa = finding_priority(a, &per_file);
47        let pb = finding_priority(b, &per_file);
48        pb.partial_cmp(&pa).unwrap_or(std::cmp::Ordering::Equal)
49    });
50}
51
52fn finding_priority(
53    f: &Finding,
54    per_file: &std::collections::HashMap<std::path::PathBuf, usize>,
55) -> f64 {
56    let sev = match f.severity {
57        Severity::Error => 3.0,
58        Severity::Warning => 2.0,
59        Severity::Hint => 1.0,
60    };
61    let overshoot = match (f.actual_value, f.threshold) {
62        (Some(a), Some(t)) if t > 0.0 => (a / t).max(1.0),
63        _ => 1.0,
64    };
65    let compound = if *per_file.get(&f.location.path).unwrap_or(&1) > 3 {
66        1.5
67    } else {
68        1.0
69    };
70    sev * overshoot * compound
71}
72
73/// Generate JSON Schema for the analysis output (list of findings).
74pub fn findings_json_schema() -> String {
75    let schema = schemars::schema_for!(Vec<Finding>);
76    serde_json::to_string_pretty(&schema).unwrap_or_default()
77}
78
79#[cfg(test)]
80mod tests;