pub struct ScanPattern {
pub min_length: usize,
/* private fields */
}Expand description
A pattern rule defining what to scan for and how to categorize matches.
Wraps a compiled regex::bytes::Regex with a Category for
replacement lookups and a human-readable label for reporting.
Both regex and literal patterns are supported. Literal patterns keep their original text and are matched by the scanner’s Aho-Corasick automaton for fast multi-literal scanning.
Fields§
§min_length: usizeMinimum window size (bytes) required to produce a match.
For literal patterns this equals the byte length of the literal itself.
For regex patterns this is 0 (no guaranteed minimum).
Used to skip captures_iter when the window is provably too short.
Implementations§
Source§impl ScanPattern
impl ScanPattern
Sourcepub fn from_regex(
pattern: &str,
category: Category,
label: impl Into<String>,
) -> Result<Self>
pub fn from_regex( pattern: &str, category: Category, label: impl Into<String>, ) -> Result<Self>
Create a pattern from a regex string.
§Capture group 1 — partial replacement
If the regex contains a capture group 1 ((...)), only the bytes
matched by that group are replaced; the bytes before and after it
within the full match are emitted verbatim. This lets you write
context-anchored patterns without redacting the prefix/suffix:
pattern: glpat-([A-Za-z0-9_-]{20})
^^^^^^ prefix preserved
^^^^^^^^^^^^^^^^^^^^ group 1 → replacedPatterns without a capture group replace the entire match.
§Errors
Returns SanitizeError::PatternCompileError if the regex is invalid.
§Examples
use sanitize_engine::scanner::ScanPattern;
use sanitize_engine::category::Category;
// No capture group — full match replaced:
let email = ScanPattern::from_regex(
r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}",
Category::Email,
"email_address",
).unwrap();
// Capture group 1 — prefix preserved, only the token value replaced:
let token = ScanPattern::from_regex(
r"glpat-([A-Za-z0-9_-]{20})",
Category::AuthToken,
"gitlab_pat",
).unwrap();Sourcepub fn from_literal(
literal: &str,
category: Category,
label: impl Into<String>,
) -> Result<Self>
pub fn from_literal( literal: &str, category: Category, label: impl Into<String>, ) -> Result<Self>
Create a pattern from a literal string.
The literal is escaped so that regex metacharacters are matched verbatim.
§Errors
Returns SanitizeError::PatternCompileError if regex compilation fails.
§Examples
use sanitize_engine::scanner::ScanPattern;
use sanitize_engine::category::Category;
let pat = ScanPattern::from_literal(
"sk-proj-abc123secret",
Category::Custom("api_key".into()),
"openai_key",
).unwrap();Sourcepub fn regex_pattern(&self) -> &str
pub fn regex_pattern(&self) -> &str
Return the raw regex pattern string for RegexSet construction.
Trait Implementations§
Source§impl Clone for ScanPattern
impl Clone for ScanPattern
Auto Trait Implementations§
impl Freeze for ScanPattern
impl RefUnwindSafe for ScanPattern
impl Send for ScanPattern
impl Sync for ScanPattern
impl Unpin for ScanPattern
impl UnsafeUnpin for ScanPattern
impl UnwindSafe for ScanPattern
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more