Skip to main content

plut_core/
finding.rs

1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5/// How loud a finding is.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)]
7#[serde(rename_all = "lowercase")]
8pub enum Severity {
9    #[default]
10    Info,
11    Warning,
12    Error,
13}
14
15impl Severity {
16    /// The lowercase name used in config and JSON.
17    #[must_use]
18    pub const fn as_str(self) -> &'static str {
19        match self {
20            Self::Info => "info",
21            Self::Warning => "warning",
22            Self::Error => "error",
23        }
24    }
25}
26
27/// How strongly the rule believes a finding is a real issue.
28#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)]
29#[serde(rename_all = "lowercase")]
30pub enum Confidence {
31    Low,
32    #[default]
33    Medium,
34    High,
35}
36
37impl Confidence {
38    /// The lowercase name used in JSON and the terminal.
39    #[must_use]
40    pub const fn as_str(self) -> &'static str {
41        match self {
42            Self::Low => "low",
43            Self::Medium => "medium",
44            Self::High => "high",
45        }
46    }
47}
48
49/// A byte range in a source file.
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
51pub struct Span {
52    pub start: usize,
53    pub end: usize,
54}
55
56impl Span {
57    /// Build a span from two byte offsets.
58    #[must_use]
59    pub const fn new(start: usize, end: usize) -> Self {
60        Self { start, end }
61    }
62}
63
64/// One report from one rule.
65#[derive(Debug, Clone, Serialize, Deserialize, Default)]
66pub struct Finding {
67    /// Rule id, such as `GB201`.
68    pub rule: String,
69    pub severity: Severity,
70    /// Path relative to the project root.
71    pub path: PathBuf,
72    /// 1-based line of `span.start`. Zero when the finding is about the file as a whole.
73    pub line: usize,
74    /// 1-based column of `span.start`. Zero when the finding is about the file as a whole.
75    pub column: usize,
76    pub span: Span,
77    pub message: String,
78    /// How strongly the rule believes the finding.
79    #[serde(default)]
80    pub confidence: Confidence,
81    /// The CWE this finding belongs to, when one applies.
82    #[serde(skip_serializing_if = "Option::is_none")]
83    pub cwe: Option<u32>,
84    #[serde(skip_serializing_if = "Option::is_none")]
85    pub help: Option<String>,
86    /// Exploit scenario, for reviewers and agents.
87    #[serde(skip_serializing_if = "Option::is_none")]
88    pub exploit: Option<String>,
89    /// Step-by-step remediation guidance.
90    #[serde(skip_serializing_if = "Option::is_none")]
91    pub remediation: Option<String>,
92    /// Machine-applicable edits. Empty means "suggestion only".
93    #[serde(default, skip_serializing_if = "Vec::is_empty")]
94    pub edits: Vec<TextEdit>,
95    /// How safe the edits are. Absent means review-required.
96    #[serde(skip_serializing_if = "Option::is_none")]
97    pub fix_safety: Option<FixSafety>,
98    /// Hash of the source the finding was produced from. Internal to `--fix`: it is
99    /// not part of the report schema.
100    #[serde(skip)]
101    pub source_hash: Option<String>,
102}
103
104/// How safe it is to apply the edits without a human reading the diff.
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
106#[serde(rename_all = "snake_case")]
107pub enum FixSafety {
108    /// No behaviour change by construction.
109    Safe,
110    /// A mechanical API migration, such as `wait` to `task.wait`.
111    Migration,
112    /// A patch that needs a human or an agent to review it first.
113    ReviewRequired,
114}
115
116/// A byte-range replacement suggested by a rule.
117#[derive(Debug, Clone, Default, Serialize, Deserialize)]
118pub struct TextEdit {
119    pub start: usize,
120    pub end: usize,
121    pub replacement: String,
122}
123
124/// A stable hash of a source text, used to detect a file that changed between the
125/// analysis and a fix. This is not a cryptographic hash; it only needs to change when
126/// the bytes change, and to stay the same across processes.
127#[must_use]
128pub fn source_hash(source: &str) -> String {
129    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
130
131    for byte in source.as_bytes() {
132        hash ^= u64::from(*byte);
133        hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
134    }
135
136    format!("{hash:016x}")
137}