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/// A byte range in a source file.
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
29pub struct Span {
30    pub start: usize,
31    pub end: usize,
32}
33
34impl Span {
35    /// Build a span from two byte offsets.
36    #[must_use]
37    pub const fn new(start: usize, end: usize) -> Self {
38        Self { start, end }
39    }
40}
41
42/// One report from one rule.
43#[derive(Debug, Clone, Serialize, Deserialize, Default)]
44pub struct Finding {
45    /// Rule id, such as `GB201`.
46    pub rule: String,
47    pub severity: Severity,
48    /// Path relative to the project root.
49    pub path: PathBuf,
50    /// 1-based line of `span.start`. Zero when the finding is about the file as a whole.
51    pub line: usize,
52    /// 1-based column of `span.start`. Zero when the finding is about the file as a whole.
53    pub column: usize,
54    pub span: Span,
55    pub message: String,
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub help: Option<String>,
58    /// Exploit scenario, for reviewers and agents.
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub exploit: Option<String>,
61    /// Step-by-step remediation guidance.
62    #[serde(skip_serializing_if = "Option::is_none")]
63    pub remediation: Option<String>,
64    /// Machine-applicable edits. Empty means "suggestion only".
65    #[serde(default, skip_serializing_if = "Vec::is_empty")]
66    pub edits: Vec<TextEdit>,
67    /// How safe the edits are. Absent means review-required.
68    #[serde(skip_serializing_if = "Option::is_none")]
69    pub fix_safety: Option<FixSafety>,
70    /// Hash of the source the finding was produced from. Internal to `--fix`: it is
71    /// not part of the report schema.
72    #[serde(skip)]
73    pub source_hash: Option<String>,
74}
75
76/// How safe it is to apply the edits without a human reading the diff.
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
78#[serde(rename_all = "snake_case")]
79pub enum FixSafety {
80    /// No behaviour change by construction.
81    Safe,
82    /// A mechanical API migration, such as `wait` to `task.wait`.
83    Migration,
84    /// A patch that needs a human or an agent to review it first.
85    ReviewRequired,
86}
87
88/// A byte-range replacement suggested by a rule.
89#[derive(Debug, Clone, Default, Serialize, Deserialize)]
90pub struct TextEdit {
91    pub start: usize,
92    pub end: usize,
93    pub replacement: String,
94}
95
96/// A stable hash of a source text, used to detect a file that changed between the
97/// analysis and a fix. This is not a cryptographic hash; it only needs to change when
98/// the bytes change, and to stay the same across processes.
99#[must_use]
100pub fn source_hash(source: &str) -> String {
101    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
102
103    for byte in source.as_bytes() {
104        hash ^= u64::from(*byte);
105        hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
106    }
107
108    format!("{hash:016x}")
109}