Make regex field in RegexFilter private

This commit is contained in:
Thomas Zahner 2025-07-11 16:04:23 +02:00
parent 83025998c7
commit 23fbd0b0d5
6 changed files with 34 additions and 41 deletions

View file

@ -139,7 +139,6 @@ fn write_out(writer: &mut Box<dyn Write>, out_str: &str) -> io::Result<()> {
mod tests {
use super::*;
use futures::stream;
use regex::RegexSet;
use tempfile::NamedTempFile;
#[tokio::test]
@ -178,9 +177,7 @@ mod tests {
];
let stream = stream::iter(inputs);
let excluded = &PathExcludes {
regex: RegexSet::new(["excluded"]).unwrap(),
};
let excluded = &PathExcludes::new(["excluded"]).unwrap();
let result = dump_inputs(stream, Some(&output_path), excluded).await?;
assert_eq!(result, ExitCode::Success);

View file

@ -75,7 +75,6 @@ use lychee_lib::filter::PathExcludes;
use openssl_sys as _; // required for vendored-openssl feature
use options::{HeaderMapExt, LYCHEE_CONFIG_FILE};
use regex::RegexSet;
use ring as _; // required for apple silicon
use lychee_lib::BasicAuthExtractor;
@ -331,9 +330,7 @@ async fn run(opts: &LycheeOptions) -> Result<i32> {
let exit_code = commands::dump_inputs(
sources,
opts.config.output.as_ref(),
&PathExcludes {
regex: RegexSet::new(&opts.config.exclude_path)?,
},
&PathExcludes::new(&opts.config.exclude_path)?,
)
.await?;

View file

@ -31,7 +31,7 @@ use crate::{
Base, BasicAuthCredentials, ErrorKind, Request, Response, Result, Status, Uri,
chain::RequestChain,
checker::{file::FileChecker, mail::MailChecker, website::WebsiteChecker},
filter::{Excludes, Filter, Includes},
filter::Filter,
remap::Remaps,
types::DEFAULT_ACCEPTED_STATUS_CODES,
};
@ -378,8 +378,8 @@ impl ClientBuilder {
};
let filter = Filter {
includes: self.includes.map(|regex| Includes { regex }),
excludes: self.excludes.map(|regex| Excludes { regex }),
includes: self.includes.map(std::convert::Into::into),
excludes: self.excludes.map(std::convert::Into::into),
schemes: self.schemes,
// exclude_all_private option turns on all "private" excludes,
// including private IPs, link-local IPs and loopback IPs

View file

@ -261,7 +261,6 @@ impl Filter {
#[cfg(test)]
mod tests {
use regex::RegexSet;
use reqwest::Url;
use url::Host;
@ -360,9 +359,7 @@ mod tests {
#[test]
fn test_overwrite_false_positives() {
let includes = Includes {
regex: RegexSet::new([r"http://www.w3.org/1999/xhtml"]).unwrap(),
};
let includes = Includes::new([r"http://www.w3.org/1999/xhtml"]).unwrap();
let filter = Filter {
includes: Some(includes),
..Filter::default()
@ -372,9 +369,7 @@ mod tests {
#[test]
fn test_include_regex() {
let includes = Includes {
regex: RegexSet::new([r"foo.example.com"]).unwrap(),
};
let includes = Includes::new([r"foo.example.com"]).unwrap();
let filter = Filter {
includes: Some(includes),
..Filter::default()
@ -411,9 +406,8 @@ mod tests {
#[test]
fn test_exclude_regex() {
let excludes = Excludes {
regex: RegexSet::new([r"github.com", r"[a-z]+\.(org|net)", r"@example.com"]).unwrap(),
};
let excludes =
Excludes::new([r"github.com", r"[a-z]+\.(org|net)", r"@example.com"]).unwrap();
let filter = Filter {
excludes: Some(excludes),
..Filter::default()
@ -428,12 +422,8 @@ mod tests {
}
#[test]
fn test_exclude_include_regex() {
let includes = Includes {
regex: RegexSet::new([r"foo.example.com"]).unwrap(),
};
let excludes = Excludes {
regex: RegexSet::new([r"example.com"]).unwrap(),
};
let includes = Includes::new([r"foo.example.com"]).unwrap();
let excludes = Excludes::new([r"example.com"]).unwrap();
let filter = Filter {
includes: Some(includes),
excludes: Some(excludes),

View file

@ -5,10 +5,30 @@ use regex::RegexSet;
#[derive(Clone, Debug)]
pub struct RegexFilter {
/// User-defined set of regex patterns
pub regex: RegexSet,
regex: RegexSet,
}
impl RegexFilter {
/// Create a new empty regex set.
#[must_use]
pub fn empty() -> Self {
Self {
regex: RegexSet::empty(),
}
}
/// Create a new empty regex set.
#[must_use]
pub fn new<I, S>(exprs: I) -> Result<Self, regex::Error>
where
S: AsRef<str>,
I: IntoIterator<Item = S>,
{
Ok(Self {
regex: RegexSet::new(exprs)?,
})
}
#[inline]
#[must_use]
/// Returns `true` if the given input string matches the regex set
@ -23,13 +43,6 @@ impl RegexFilter {
pub fn is_empty(&self) -> bool {
self.regex.is_empty()
}
/// Create a new empty regex set.
pub fn empty() -> Self {
Self {
regex: RegexSet::empty(),
}
}
}
impl PartialEq for RegexFilter {

View file

@ -476,9 +476,7 @@ mod tests {
fn test_excluded() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path();
let excludes = PathExcludes {
regex: RegexSet::new([path.to_string_lossy()]).unwrap(),
};
let excludes = PathExcludes::new([path.to_string_lossy()]).unwrap();
assert!(is_excluded_path(&excludes, path));
}
@ -489,9 +487,7 @@ mod tests {
let child_dir = tempfile::tempdir_in(parent).unwrap();
let child = child_dir.path();
let excludes = PathExcludes {
regex: RegexSet::new([parent.to_string_lossy()]).unwrap(),
};
let excludes = PathExcludes::new([parent.to_string_lossy()]).unwrap();
assert!(is_excluded_path(&excludes, child));
}