use crate::time::Day;
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
use std::fmt;
use std::path::PathBuf;
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, clap::ValueEnum)]
#[serde(rename_all = "lowercase")]
pub enum Agent {
Claude,
Gemini,
Copilot,
Antigravity,
}
impl Agent {
pub const ALL: [Agent; 4] = [Agent::Claude, Agent::Gemini, Agent::Copilot, Agent::Antigravity];
pub fn name(self) -> &'static str {
match self {
Agent::Claude => "claude",
Agent::Gemini => "gemini",
Agent::Copilot => "copilot",
Agent::Antigravity => "antigravity",
}
}
}
impl fmt::Display for Agent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Severity {
High,
#[default]
Medium,
Low,
}
impl Severity {
pub fn name(self) -> &'static str {
match self {
Severity::High => "high",
Severity::Medium => "medium",
Severity::Low => "low",
}
}
pub fn glyph(self) -> &'static str {
match self {
Severity::High => "▲",
Severity::Medium => "■",
Severity::Low => "·",
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Repo {
pub name: String,
pub path: String,
pub remote: Option<String>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Location {
pub agent: Agent,
pub file: PathBuf,
pub line: usize,
pub repo: Option<Repo>,
pub branch: Option<String>,
pub day: Day,
}
impl Location {
pub fn repo_name(&self) -> &str {
self.repo.as_ref().map_or("(no repository)", |r| r.name.as_str())
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
pub struct Progress {
pub rotated: bool,
pub masked: bool,
pub ignored: bool,
}
#[derive(Clone, Debug, Serialize)]
pub struct Secret {
pub id: String,
pub rule: String,
pub severity: Severity,
pub preview: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub value: Option<String>,
pub location: Location,
pub files: BTreeSet<PathBuf>,
pub occurrences: usize,
pub new: bool,
pub progress: Progress,
}
impl Secret {
pub fn is_open(&self) -> bool {
!self.progress.ignored && !(self.progress.rotated && self.progress.masked)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Verdict {
Flagged,
Unknown,
Allowed,
}
impl Verdict {
pub fn name(self) -> &'static str {
match self {
Verdict::Flagged => "flagged",
Verdict::Unknown => "unknown",
Verdict::Allowed => "allowed",
}
}
pub fn glyph(self) -> &'static str {
match self {
Verdict::Flagged => "✗",
Verdict::Unknown => "?",
Verdict::Allowed => "✓",
}
}
}
#[derive(Clone, Debug, Serialize)]
pub struct Host {
pub host: String,
pub verdict: Verdict,
pub reason: String,
pub occurrences: usize,
pub locations: Vec<Location>,
pub last_seen: Day,
pub new: bool,
}
impl Host {
pub fn repo_name(&self) -> &str {
self.locations.first().map_or("(no repository)", Location::repo_name)
}
}
#[derive(Clone, Debug, Default, Serialize)]
pub struct Report {
pub scanned_files: usize,
pub agents: Vec<Agent>,
pub today: Day,
pub secrets: Vec<Secret>,
pub hosts: Vec<Host>,
}
impl Report {
pub fn open_secrets(&self) -> impl Iterator<Item = &Secret> {
self.secrets.iter().filter(|s| s.is_open())
}
pub fn flagged_hosts(&self) -> impl Iterator<Item = &Host> {
self.hosts.iter().filter(|h| h.verdict == Verdict::Flagged)
}
pub fn has_new_findings(&self) -> bool {
self.open_secrets().any(|s| s.new) || self.flagged_hosts().any(|h| h.new)
}
}