1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5use crate::config::Target;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)]
9#[serde(rename_all = "lowercase")]
10pub enum Severity {
11 #[default]
12 Info,
13 Warning,
14 Error,
15}
16
17impl Severity {
18 #[must_use]
20 pub const fn as_str(self) -> &'static str {
21 match self {
22 Self::Info => "info",
23 Self::Warning => "warning",
24 Self::Error => "error",
25 }
26 }
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)]
31#[serde(rename_all = "lowercase")]
32pub enum Confidence {
33 Low,
34 #[default]
35 Medium,
36 High,
37}
38
39impl Confidence {
40 #[must_use]
42 pub const fn as_str(self) -> &'static str {
43 match self {
44 Self::Low => "low",
45 Self::Medium => "medium",
46 Self::High => "high",
47 }
48 }
49}
50
51#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
53#[serde(rename_all = "lowercase")]
54pub enum Namespace {
55 #[default]
57 Gb,
58 Luneblox,
60 Lute,
62 Custom,
64}
65
66impl Namespace {
67 #[must_use]
69 pub const fn as_str(self) -> &'static str {
70 match self {
71 Self::Gb => "gb",
72 Self::Luneblox => "luneblox",
73 Self::Lute => "lute",
74 Self::Custom => "custom",
75 }
76 }
77}
78
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
81pub struct Span {
82 pub start: usize,
83 pub end: usize,
84}
85
86impl Span {
87 #[must_use]
89 pub const fn new(start: usize, end: usize) -> Self {
90 Self { start, end }
91 }
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize, Default)]
96pub struct Finding {
97 pub rule: String,
99 #[serde(default)]
101 pub namespace: Namespace,
102 #[serde(default)]
104 pub targets: Vec<Target>,
105 pub severity: Severity,
106 pub path: PathBuf,
108 pub line: usize,
110 pub column: usize,
112 pub span: Span,
113 pub message: String,
114 #[serde(default)]
116 pub confidence: Confidence,
117 #[serde(skip_serializing_if = "Option::is_none")]
119 pub cwe: Option<u32>,
120 #[serde(skip_serializing_if = "Option::is_none")]
121 pub help: Option<String>,
122 #[serde(skip_serializing_if = "Option::is_none")]
124 pub exploit: Option<String>,
125 #[serde(skip_serializing_if = "Option::is_none")]
127 pub remediation: Option<String>,
128 #[serde(default, skip_serializing_if = "Vec::is_empty")]
130 pub edits: Vec<TextEdit>,
131 #[serde(skip_serializing_if = "Option::is_none")]
133 pub fix_safety: Option<FixSafety>,
134 #[serde(skip)]
137 pub source_hash: Option<String>,
138}
139
140#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
142#[serde(rename_all = "snake_case")]
143pub enum FixSafety {
144 Safe,
146 Migration,
148 ReviewRequired,
150}
151
152#[derive(Debug, Clone, Default, Serialize, Deserialize)]
154pub struct TextEdit {
155 pub start: usize,
156 pub end: usize,
157 pub replacement: String,
158}
159
160#[must_use]
164pub fn source_hash(source: &str) -> String {
165 let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
166
167 for byte in source.as_bytes() {
168 hash ^= u64::from(*byte);
169 hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
170 }
171
172 format!("{hash:016x}")
173}