Skip to main content

diff_risk_core/
lib.rs

1//! Core risk-scoring engine for `diff-risk`.
2//!
3//! Parses unified diffs, runs a pipeline of [`Detector`]s against the
4//! changed regions, and aggregates findings into a [`RiskReport`] with
5//! an overall 0.0–10.0 score.
6//!
7//! The CLI front-end lives in the `diff-risk-cli` crate; this crate is
8//! deliberately I/O-free so it can be embedded in editors, pre-commit
9//! hooks, and CI tooling.
10
11pub mod detectors;
12pub mod diff;
13pub mod report;
14pub mod scoring;
15
16pub use detectors::{async_boundary::AsyncBoundaryDetector, auth::AuthDetector, Detector};
17pub use diff::{parse_unified_diff, ChangedHunk, Diff, DiffError};
18pub use report::{Finding, RiskCategory, RiskReport, Severity};
19pub use scoring::score_findings;
20
21/// Run the default detector suite against a parsed [`Diff`] and return a [`RiskReport`].
22#[must_use]
23pub fn analyze(diff: &Diff) -> RiskReport {
24    analyze_with(diff, &default_detectors())
25}
26
27/// Run an explicit set of detectors against a parsed [`Diff`].
28#[must_use]
29pub fn analyze_with(diff: &Diff, detectors: &[Box<dyn Detector>]) -> RiskReport {
30    let mut findings = Vec::new();
31    for detector in detectors {
32        findings.extend(detector.detect(diff));
33    }
34    let score = score_findings(&findings);
35    RiskReport { findings, score }
36}
37
38/// The detector suite applied by [`analyze`].
39#[must_use]
40pub fn default_detectors() -> Vec<Box<dyn Detector>> {
41    vec![
42        Box::new(AuthDetector::new()),
43        Box::new(AsyncBoundaryDetector::new()),
44    ]
45}