diff --git a/lychee-bin/src/commands/dump.rs b/lychee-bin/src/commands/dump.rs index 3b246f7..9890fe7 100644 --- a/lychee-bin/src/commands/dump.rs +++ b/lychee-bin/src/commands/dump.rs @@ -139,7 +139,6 @@ fn write_out(writer: &mut Box, 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); diff --git a/lychee-bin/src/main.rs b/lychee-bin/src/main.rs index 33197d3..a5784b6 100644 --- a/lychee-bin/src/main.rs +++ b/lychee-bin/src/main.rs @@ -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 { 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?; diff --git a/lychee-lib/src/client.rs b/lychee-lib/src/client.rs index b7858e9..6cfd34c 100644 --- a/lychee-lib/src/client.rs +++ b/lychee-lib/src/client.rs @@ -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 diff --git a/lychee-lib/src/filter/mod.rs b/lychee-lib/src/filter/mod.rs index 39bb3a0..6f767c0 100644 --- a/lychee-lib/src/filter/mod.rs +++ b/lychee-lib/src/filter/mod.rs @@ -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), diff --git a/lychee-lib/src/filter/regex_filter.rs b/lychee-lib/src/filter/regex_filter.rs index 700cff7..9ff950d 100644 --- a/lychee-lib/src/filter/regex_filter.rs +++ b/lychee-lib/src/filter/regex_filter.rs @@ -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(exprs: I) -> Result + where + S: AsRef, + I: IntoIterator, + { + 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 { diff --git a/lychee-lib/src/types/input.rs b/lychee-lib/src/types/input.rs index 18b3670..ac32e5c 100644 --- a/lychee-lib/src/types/input.rs +++ b/lychee-lib/src/types/input.rs @@ -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)); }