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, 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 #[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#[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 #[must_use]
59 pub const fn new(start: usize, end: usize) -> Self {
60 Self { start, end }
61 }
62}
63
64#[derive(Debug, Clone, Serialize, Deserialize, Default)]
66pub struct Finding {
67 pub rule: String,
69 pub severity: Severity,
70 pub path: PathBuf,
72 pub line: usize,
74 pub column: usize,
76 pub span: Span,
77 pub message: String,
78 #[serde(default)]
80 pub confidence: Confidence,
81 #[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 #[serde(skip_serializing_if = "Option::is_none")]
88 pub exploit: Option<String>,
89 #[serde(skip_serializing_if = "Option::is_none")]
91 pub remediation: Option<String>,
92 #[serde(default, skip_serializing_if = "Vec::is_empty")]
94 pub edits: Vec<TextEdit>,
95 #[serde(skip_serializing_if = "Option::is_none")]
97 pub fix_safety: Option<FixSafety>,
98 #[serde(skip)]
101 pub source_hash: Option<String>,
102}
103
104#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
106#[serde(rename_all = "snake_case")]
107pub enum FixSafety {
108 Safe,
110 Migration,
112 ReviewRequired,
114}
115
116#[derive(Debug, Clone, Default, Serialize, Deserialize)]
118pub struct TextEdit {
119 pub start: usize,
120 pub end: usize,
121 pub replacement: String,
122}
123
124#[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}