From b702755720137b9b844c6580dfae9f2bd4c3fb83 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 23 Jan 2021 09:12:20 +0100 Subject: Refactor: Move package name regex building to helper function Signed-off-by: Matthias Beyer --- src/commands/find_pkg.rs | 22 +++------------------- src/commands/util.rs | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/commands/find_pkg.rs b/src/commands/find_pkg.rs index 1d7f71b..e2f7b77 100644 --- a/src/commands/find_pkg.rs +++ b/src/commands/find_pkg.rs @@ -8,9 +8,7 @@ // SPDX-License-Identifier: EPL-2.0 // -use anyhow::anyhow; use anyhow::Context; -use anyhow::Error; use anyhow::Result; use clap::ArgMatches; use log::trace; @@ -27,23 +25,9 @@ pub async fn find_pkg( ) -> Result<()> { use std::io::Write; - let package_name_regex = matches - .value_of("package_name_regex") - .map(regex::RegexBuilder::new) - .map(|mut builder| { - #[allow(clippy::identity_op)] - builder.size_limit(1 * 1024 * 1024); // max size for the regex is 1MB. Should be enough for everyone - builder - .build() - .with_context(|| { - anyhow!( - "Failed to build regex from '{}'", - matches.value_of("package_name_regex").unwrap() - ) - }) - .map_err(Error::from) - }) - .unwrap()?; // safe by clap + let package_name_regex = crate::commands::util::mk_package_name_regex({ + matches.value_of("package_name_regex").unwrap() // safe by clap + })?; let package_version_constraint = matches .value_of("package_version_constraint") diff --git a/src/commands/util.rs b/src/commands/util.rs index ec99d11..8862c05 100644 --- a/src/commands/util.rs +++ b/src/commands/util.rs @@ -10,10 +10,13 @@ use std::path::Path; -use anyhow::anyhow; +use anyhow::Error; +use anyhow::Context; use anyhow::Result; +use anyhow::anyhow; use clap::ArgMatches; use log::{error, info, trace}; +use regex::Regex; use tokio::stream::StreamExt; use crate::config::*; @@ -132,3 +135,15 @@ fn all_phases_available(pkg: &Package, available_phases: &[PhaseName]) -> Result Ok(()) } + +pub fn mk_package_name_regex(regex: &str) -> Result { + let mut builder = regex::RegexBuilder::new(regex); + + #[allow(clippy::identity_op)] + builder.size_limit(1 * 1024 * 1024); // max size for the regex is 1MB. Should be enough for everyone + + builder + .build() + .with_context(|| anyhow!("Failed to build regex from '{}'", regex)) + .map_err(Error::from) +} -- cgit v1.2.3