use serde::{Deserialize, Serialize};
use sqlx::FromRow;
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct VulnerabilityRecord {
#[serde(skip_serializing_if = "Option::is_none")]
pub vuln_id: Option<i64>,
pub scan_id: i64,
pub vulnerability_type: String, pub severity: String, pub description: Option<String>,
pub cve_id: Option<String>,
pub affected_component: Option<String>,
}
impl VulnerabilityRecord {
pub fn new(scan_id: i64, vulnerability_type: String, severity: String) -> Self {
Self {
vuln_id: None,
scan_id,
vulnerability_type,
severity,
description: None,
cve_id: None,
affected_component: None,
}
}
pub fn with_description(mut self, description: String) -> Self {
self.description = Some(description);
self
}
pub fn with_cve(mut self, cve_id: String) -> Self {
self.cve_id = Some(cve_id);
self
}
pub fn with_component(mut self, component: String) -> Self {
self.affected_component = Some(component);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_vulnerability_record_creation() {
let vuln = VulnerabilityRecord::new(1, "Heartbleed".to_string(), "critical".to_string())
.with_cve("CVE-2014-0160".to_string())
.with_description("OpenSSL heartbeat vulnerability".to_string());
assert_eq!(vuln.scan_id, 1);
assert_eq!(vuln.vulnerability_type, "Heartbleed");
assert_eq!(vuln.severity, "critical");
assert_eq!(vuln.cve_id, Some("CVE-2014-0160".to_string()));
}
}