Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company 👇
Go aicertificationcloud-engineeringcybercybersecuritycybersecurity-analystcybersecurity-certificationcybersecurity-certificationscybersecurity-educationcybersecurity-jobscybersecurity-portfoliocybersecurity-projectscybersecurity-toolsgrchackinglearningpentestingproject-repositoryprojetcspython
// ©AngelaMos | 2026
// dedup.rs
use std::collections::BTreeMap;
use crate::crash::Crash;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CrashGroup<'a> {
key: &'a str,
crashes: Vec<&'a Crash>,
}
impl<'a> CrashGroup<'a> {
#[must_use]
pub fn key(&self) -> &'a str {
self.key
}
#[must_use]
pub fn crashes(&self) -> &[&'a Crash] {
&self.crashes
}
#[must_use]
pub fn len(&self) -> usize {
self.crashes.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.crashes.is_empty()
}
#[must_use]
pub fn representative(&self) -> Option<&'a Crash> {
self.crashes.first().copied()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Grouping<'a> {
groups: Vec<CrashGroup<'a>>,
unkeyed: Vec<&'a Crash>,
}
impl<'a> Grouping<'a> {
#[must_use]
pub fn groups(&self) -> &[CrashGroup<'a>] {
&self.groups
}
#[must_use]
pub fn unkeyed(&self) -> &[&'a Crash] {
&self.unkeyed
}
#[must_use]
pub fn keyed_bugs(&self) -> usize {
self.groups.len()
}
#[must_use]
pub fn unkeyed_crashes(&self) -> usize {
self.unkeyed.len()
}
}
#[must_use]
pub fn group_crashes(crashes: &[Crash]) -> Grouping<'_> {
let mut by_key: BTreeMap<&str, Vec<&Crash>> = BTreeMap::new();
let mut unkeyed = Vec::new();
for crash in crashes {
match crash.stack_hash() {
Some(key) => by_key.entry(key).or_default().push(crash),
None => unkeyed.push(crash),
}
}
let mut groups: Vec<CrashGroup<'_>> = by_key
.into_iter()
.map(|(key, crashes)| CrashGroup { key, crashes })
.collect();
groups.sort_by(|left, right| right.len().cmp(&left.len()).then(left.key.cmp(right.key)));
Grouping { groups, unkeyed }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::campaign::CampaignId;
const CAMPAIGN: CampaignId = CampaignId(1);
fn crash(input: &str, hash: Option<&str>) -> Crash {
Crash::new(
CAMPAIGN,
input.as_bytes().to_vec(),
"report",
hash.map(str::to_owned),
)
}
#[test]
fn crashes_sharing_a_key_collapse_to_one_group() {
let crashes = vec![
crash("a", Some("deadbeef")),
crash("b", Some("deadbeef")),
crash("c", Some("cafebabe")),
];
let grouping = group_crashes(&crashes);
assert_eq!(grouping.keyed_bugs(), 2);
assert_eq!(grouping.groups()[0].key(), "deadbeef");
assert_eq!(grouping.groups()[0].len(), 2);
}
#[test]
fn unkeyed_crashes_are_never_merged_with_each_other() {
let crashes = vec![
crash("a", None),
crash("b", None),
crash("c", None),
crash("d", Some("deadbeef")),
];
let grouping = group_crashes(&crashes);
assert_eq!(
grouping.keyed_bugs(),
1,
"a missing key is the absence of evidence, not evidence of sameness; bucketing NULL together would re-merge exactly what stack_hash refuses to key"
);
assert_eq!(grouping.unkeyed_crashes(), 3);
assert!(
grouping
.groups()
.iter()
.all(|group| !group.crashes().iter().any(|c| c.stack_hash().is_none())),
"no unkeyed crash may appear inside a keyed group"
);
}
#[test]
fn groups_are_ordered_largest_first_then_by_key() {
let crashes = vec![
crash("a", Some("bbbb")),
crash("b", Some("aaaa")),
crash("c", Some("aaaa")),
crash("d", Some("cccc")),
];
let grouping = group_crashes(&crashes);
let keys: Vec<&str> = grouping.groups().iter().map(CrashGroup::key).collect();
assert_eq!(keys, vec!["aaaa", "bbbb", "cccc"]);
}
#[test]
fn an_empty_crash_set_groups_to_nothing() {
let grouping = group_crashes(&[]);
assert_eq!(grouping.keyed_bugs(), 0);
assert_eq!(grouping.unkeyed_crashes(), 0);
}
#[test]
fn a_representative_is_drawn_from_the_group() {
let crashes = vec![
crash("first", Some("deadbeef")),
crash("second", Some("deadbeef")),
];
let grouping = group_crashes(&crashes);
let representative = grouping.groups()[0].representative().unwrap();
assert_eq!(representative.input(), b"first");
}
}