1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5#[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 #[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#[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 #[must_use]
37 pub const fn new(start: usize, end: usize) -> Self {
38 Self { start, end }
39 }
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize, Default)]
44pub struct Finding {
45 pub rule: String,
47 pub severity: Severity,
48 pub path: PathBuf,
50 pub line: usize,
52 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 #[serde(skip_serializing_if = "Option::is_none")]
60 pub exploit: Option<String>,
61 #[serde(skip_serializing_if = "Option::is_none")]
63 pub remediation: Option<String>,
64 #[serde(default, skip_serializing_if = "Vec::is_empty")]
66 pub edits: Vec<TextEdit>,
67 #[serde(skip_serializing_if = "Option::is_none")]
69 pub fix_safety: Option<FixSafety>,
70 #[serde(skip)]
73 pub source_hash: Option<String>,
74}
75
76#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
78#[serde(rename_all = "snake_case")]
79pub enum FixSafety {
80 Safe,
82 Migration,
84 ReviewRequired,
86}
87
88#[derive(Debug, Clone, Default, Serialize, Deserialize)]
90pub struct TextEdit {
91 pub start: usize,
92 pub end: usize,
93 pub replacement: String,
94}
95
96#[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}