1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! CRC32 detection.
//!
//! Detects CRC32 checksums commonly found in anime filenames: `[ABCD1234]`.
//!
//! ## Why this lives in Rust (not `src/rules/`)
//!
//! Hex format validation (8 chars, [0-9A-Fa-f]) wraps the regex match
//! and the captured value is normalized to uppercase for output. See
//! DESIGN.md D2 decision table → "validation beyond regex" + "requires
//! type conversion".
use regex::Regex;
use crate::matcher::span::{MatchSpan, Property};
use std::sync::LazyLock;
/// Matches 8-char hex CRC32 in square brackets: [ABCD1234]
static CRC32_BRACKET: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\[(?P<crc>[0-9A-Fa-f]{8})\]").expect("CRC32 regex is valid"));
/// Scan for CRC32 checksums in brackets (e.g., `[ABCD1234]`) and return matches.
pub fn find_matches(input: &str) -> Vec<MatchSpan> {
let mut matches = Vec::new();
for cap in CRC32_BRACKET.captures_iter(input) {
if let Some(crc) = cap.name("crc") {
matches.push(
MatchSpan::new(
crc.start(),
crc.end(),
Property::Crc,
crc.as_str().to_uppercase(),
)
.with_priority(crate::priority::KEYWORD),
);
}
}
matches
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_crc32() {
let m = find_matches("[SubGroup] Anime - 01 [1080p] [ABCD1234].mkv");
assert_eq!(m.len(), 1);
assert_eq!(m[0].value, "ABCD1234");
}
#[test]
fn test_no_false_positive() {
// Non-hex chars shouldn't match
let m = find_matches("[SubGroup].mkv");
assert!(m.is_empty());
}
}