use std::sync::Arc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Kind {
Redacted,
Observed,
AllowlistedValue,
LowEntropy,
ExceptedField,
}
impl Kind {
pub const ALL: [Self; 5] = [
Self::Redacted,
Self::Observed,
Self::AllowlistedValue,
Self::LowEntropy,
Self::ExceptedField,
];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Redacted => "redacted",
Self::Observed => "observed",
Self::AllowlistedValue => "allowlisted_value",
Self::LowEntropy => "low_entropy",
Self::ExceptedField => "excepted_field",
}
}
const fn slot(self) -> usize {
match self {
Self::Redacted => 0,
Self::Observed => 1,
Self::AllowlistedValue => 2,
Self::LowEntropy => 3,
Self::ExceptedField => 4,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Layer {
Secret,
Field,
Pii,
Text,
}
impl Layer {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Secret => "secret",
Self::Field => "field",
Self::Pii => "pii",
Self::Text => "text",
}
}
}
#[derive(Debug)]
pub(super) struct RuleMeta {
pub id: Box<str>,
pub token: Box<str>,
pub layer: Layer,
}
impl RuleMeta {
pub fn new(id: &str, layer: Layer) -> Self {
let token: String = id
.chars()
.map(|c| {
if c == '-' {
'_'
} else {
c.to_ascii_uppercase()
}
})
.collect();
Self {
id: id.into(),
token: token.into(),
layer,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Entry<'r> {
pub rule: &'r str,
pub layer: Layer,
pub kind: Kind,
pub count: u32,
}
#[derive(Debug, Clone)]
pub struct Report {
owner: u64,
rules: Arc<[RuleMeta]>,
counts: Vec<[u32; Kind::ALL.len()]>,
touched: Vec<usize>,
changed: bool,
}
impl Report {
pub(super) fn new(rules: Arc<[RuleMeta]>, owner: u64) -> Self {
let n = rules.len();
Self {
owner,
rules,
counts: vec![[0; Kind::ALL.len()]; n],
touched: Vec::with_capacity(n),
changed: false,
}
}
pub fn clear(&mut self) {
for &i in &self.touched {
self.counts[i] = [0; Kind::ALL.len()];
}
self.touched.clear();
self.changed = false;
}
#[must_use]
pub const fn changed(&self) -> bool {
self.changed
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.touched.is_empty()
}
pub fn entries(&self) -> impl Iterator<Item = Entry<'_>> {
self.touched.iter().flat_map(move |&i| {
let meta = &self.rules[i];
Kind::ALL.into_iter().filter_map(move |kind| {
let count = self.counts[i][kind.slot()];
(count > 0).then_some(Entry {
rule: &meta.id,
layer: meta.layer,
kind,
count,
})
})
})
}
#[must_use]
pub fn count(&self, rule: &str, kind: Kind) -> u32 {
self.rules
.iter()
.enumerate()
.filter(|(_, m)| &*m.id == rule)
.map(|(i, _)| self.counts[i][kind.slot()])
.sum()
}
pub(super) const fn owner(&self) -> u64 {
self.owner
}
pub(super) fn add(&mut self, rule: usize, kind: Kind) {
self.add_n(rule, kind, 1);
}
pub(super) fn add_n(&mut self, rule: usize, kind: Kind, n: usize) {
if n == 0 {
return;
}
let row = &mut self.counts[rule];
if row.iter().all(|&c| c == 0) {
self.touched.push(rule);
}
let n = u32::try_from(n).unwrap_or(u32::MAX);
row[kind.slot()] = row[kind.slot()].saturating_add(n);
}
pub(super) const fn set_changed(&mut self) {
self.changed = true;
}
}