From 082a0bdeff662a5fc1153818ff561f6cd6fb7df5 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Sun, 5 Dec 2021 10:01:20 +0100 Subject: [PATCH] Simplify the `Regex` construction --- src/contains_regex.rs | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/src/contains_regex.rs b/src/contains_regex.rs index cee0e13..a865c4f 100644 --- a/src/contains_regex.rs +++ b/src/contains_regex.rs @@ -1,5 +1,5 @@ #![cfg(feature = "contains_regex")] -use regex::{escape, Regex}; +use regex::{escape, RegexBuilder}; use crate::helpers::{read_file, Result}; @@ -25,38 +25,25 @@ pub fn check_contains_regex( pkg_name: &str, pkg_version: &str, ) -> Result<()> { - // Expand the optional {name} and {version} placeholders in the - // template. This is almost like - // - // format!(template, name = pkg_name, version = pkg_version) - // - // but allows the user to leave out unnecessary placeholders. - let orig_regex = template + // Expand the placeholders in the template. + let pattern = template .replace("{name}", &escape(pkg_name)) .replace("{version}", &escape(pkg_version)); - - // We start by constructing a Regex from the original string. This - // ensurs that any errors refer to the string the user passed - // instead of the string we use internally. - let re = match Regex::new(&orig_regex) { - Ok(_) => { - // We now know that the regex is valid, so we can enable - // multi-line mode by prepending "(?m)". - let regex = String::from("(?m)") + &orig_regex; - Regex::new(®ex).unwrap() - } - Err(err) => return Err(format!("could not parse template: {}", err)), - }; + let mut builder = RegexBuilder::new(&pattern); + builder.multi_line(true); + let re = builder + .build() + .map_err(|err| format!("could not parse template: {}", err))?; let text = read_file(path).map_err(|err| format!("could not read {}: {}", path, err))?; - println!("Searching for \"{}\" in {}...", orig_regex, path); + println!("Searching for \"{}\" in {}...", pattern, path); match re.find(&text) { Some(m) => { let line_no = text[..m.start()].lines().count(); println!("{} (line {}) ... ok", path, line_no + 1); Ok(()) } - None => Err(format!("could not find \"{}\" in {}", orig_regex, path)), + None => Err(format!("could not find \"{}\" in {}", pattern, path)), } }