diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-18 01:57:02 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-18 01:57:02 +0900 |
| commit | 844097edf44bf1424d28e2d5fbefda90c1c8f46c (patch) | |
| tree | 968767db86e26acb022dd6ec5ac2c602d92e3708 /crates | |
| parent | 5114a8199a87c9e5584d92848e95deba22b73e98 (diff) | |
| download | php-shirabe-844097edf44bf1424d28e2d5fbefda90c1c8f46c.tar.gz php-shirabe-844097edf44bf1424d28e2d5fbefda90c1c8f46c.tar.zst php-shirabe-844097edf44bf1424d28e2d5fbefda90c1c8f46c.zip | |
refactor(pcre): hand back the match instead of copying it out
Preg::match4 and Preg::replace_callback gave callers a
PregMatchedGroups: an IndexMap rebuilt from the match with an owned
String per group, plus a second String for a named group's name key.
That is the copy PregMatches shed when it started wrapping
regex::Captures, reinstated one layer up -- and nearly every regex call
in the tree goes through Preg rather than the shim's preg_* directly, so
almost nothing saw the borrow.
PregMatchedGroups existed only to drop the null (unmatched) groups the
old PregMatches held as Option<String> values. PregMatches::get reports
a non-participating group as None on its own, so the two read alike and
the type collapses into it. Call sites still reach groups through
get(&CaptureKey::ByIndex(N)); what changes is that the value arrives as
a &str borrowed from the subject, which the signatures now carry as a
lifetime.
Three places needed the borrow reckoned with rather than a mechanical
rewrite: PhpFileCleaner::clean and Problem::get_messages read their
groups out before mutating what the match borrows, and
Git::get_authentication_failure names the lifetime of its url argument,
which the result borrows instead of self.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates')
51 files changed, 438 insertions, 400 deletions
diff --git a/crates/shirabe-class-map-generator/src/class_map_generator.rs b/crates/shirabe-class-map-generator/src/class_map_generator.rs index 670f8448..5b745b80 100644 --- a/crates/shirabe-class-map-generator/src/class_map_generator.rs +++ b/crates/shirabe-class-map-generator/src/class_map_generator.rs @@ -353,8 +353,8 @@ impl ClassMapGenerator { ) { prefix = r#match .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); path = substr(&path, strlen(&prefix), None); } @@ -379,8 +379,8 @@ impl ClassMapGenerator { php_regex!(r"{(?:^|://)[a-z]:$}i"), |m| { m.get(&CaptureKey::ByIndex(0)) - .cloned() .unwrap_or_default() + .to_string() .to_uppercase() }, &prefix, diff --git a/crates/shirabe-class-map-generator/src/php_file_cleaner.rs b/crates/shirabe-class-map-generator/src/php_file_cleaner.rs index 5731dbd4..8b60b4db 100644 --- a/crates/shirabe-class-map-generator/src/php_file_cleaner.rs +++ b/crates/shirabe-class-map-generator/src/php_file_cleaner.rs @@ -1,7 +1,7 @@ //! ref: composer/vendor/composer/class-map-generator/src/PhpFileCleaner.php use indexmap::IndexMap; -use shirabe_pcre::{CaptureKey, Preg, PregMatchedGroups}; +use shirabe_pcre::{CaptureKey, Preg, PregMatches}; use std::sync::Mutex; #[derive(Debug, Clone)] @@ -105,16 +105,20 @@ impl PhpFileCleaner { if let Some(r#match) = self.r#match( r#"{<<<[ \t]*(?:"([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)"|'([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)'|([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*))(?:\r\n|\n|\r)}A"#, ) { - self.index += r#match.get(&CaptureKey::ByIndex(0)).map(|s| s.len()).unwrap_or(0); + let matched_len = r#match + .get(&CaptureKey::ByIndex(0)) + .map(|s| s.len()) + .unwrap_or(0); let delimiter = [1, 2, 3] .iter() .find_map(|i| { r#match .get(&CaptureKey::ByIndex(*i)) .filter(|s| !s.is_empty()) - .cloned() + .map(str::to_string) }) .unwrap_or_default(); + self.index += matched_len; self.skip_heredoc(&delimiter); clean.push_str("null"); continue; @@ -145,11 +149,7 @@ impl PhpFileCleaner { if let Some(r#match) = Preg::is_match4(&entry.pattern, &self.contents, offset) { - return clean - + r#match - .get(&CaptureKey::ByIndex(0)) - .map(|s| s.as_str()) - .unwrap_or(""); + return clean + r#match.get(&CaptureKey::ByIndex(0)).unwrap_or(""); } } } @@ -161,8 +161,8 @@ impl PhpFileCleaner { if let Some(r#match) = self.r#match(&rest_pattern) { let m0 = r#match .get(&CaptureKey::ByIndex(0)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); clean.push(char); clean.push_str(&m0); self.index += m0.len(); @@ -285,7 +285,7 @@ impl PhpFileCleaner { self.index + 1 < self.len && self.contents.as_bytes()[self.index + 1] as char == char } - fn r#match(&self, regex: &str) -> Option<PregMatchedGroups> { + fn r#match(&self, regex: &str) -> Option<PregMatches<'_>> { Preg::is_match4(regex, &self.contents, self.index) } } diff --git a/crates/shirabe-pcre/src/preg.rs b/crates/shirabe-pcre/src/preg.rs index f3bfeac0..491e5371 100644 --- a/crates/shirabe-pcre/src/preg.rs +++ b/crates/shirabe-pcre/src/preg.rs @@ -18,12 +18,6 @@ use shirabe_php_shim::{ }; preg_match_map! { - /// A single match's `$matches` as `Preg` hands it to callers: an unmatched capture group is - /// absent rather than held as a null value. - pub struct PregMatchedGroups(CaptureKey => String); -} - -preg_match_map! { /// The named capture groups of a single match, keyed by group name alone. pub struct PregNamedGroups(String => String); } @@ -32,16 +26,16 @@ preg_match_map! { pub struct Preg; impl Preg { - pub fn match3(pattern: impl PregPattern, subject: &str) -> Option<PregMatchedGroups> { + pub fn match3<'h>(pattern: impl PregPattern, subject: &'h str) -> Option<PregMatches<'h>> { Self::match4(pattern, subject, 0) } - pub fn match4( + pub fn match4<'h>( pattern: impl PregPattern, - subject: &str, + subject: &'h str, offset: usize, - ) -> Option<PregMatchedGroups> { - preg_match2(pattern, subject, offset).map(|internal| drop_null_matches(&internal)) + ) -> Option<PregMatches<'h>> { + preg_match2(pattern, subject, offset) } pub fn match_all(pattern: impl PregPattern, subject: &str) -> usize { @@ -82,12 +76,12 @@ impl Preg { preg_replace2(pattern, replacement, subject, limit, Some(count)) } - pub fn replace_callback<F: FnMut(&PregMatchedGroups) -> String>( + pub fn replace_callback<'h, F: FnMut(&PregMatches<'h>) -> String>( pattern: impl PregPattern, mut replacement: F, - subject: &str, + subject: &'h str, ) -> String { - let adapter = |internal: &PregMatches| Ok(replacement(&drop_null_matches(internal))); + let adapter = |matches: &PregMatches<'h>| Ok(replacement(matches)); preg_replace_callback(pattern, adapter, subject).expect("$replacement cannot fail") } @@ -103,15 +97,15 @@ impl Preg { Self::match4(pattern, subject, 0).is_some() } - pub fn is_match3(pattern: impl PregPattern, subject: &str) -> Option<PregMatchedGroups> { + pub fn is_match3<'h>(pattern: impl PregPattern, subject: &'h str) -> Option<PregMatches<'h>> { Self::match4(pattern, subject, 0) } - pub fn is_match4( + pub fn is_match4<'h>( pattern: impl PregPattern, - subject: &str, + subject: &'h str, offset: usize, - ) -> Option<PregMatchedGroups> { + ) -> Option<PregMatches<'h>> { Self::match4(pattern, subject, offset) } @@ -155,12 +149,3 @@ impl Preg { Self::match_all_with_offsets5(pattern, subject) } } - -// Drops `null` (unmatched) groups, mirroring how the public `string`-valued -// `matches` map represents PHP's `string|null` entries by their absence. -fn drop_null_matches(matches: &PregMatches) -> PregMatchedGroups { - matches - .iter() - .filter_map(|(key, value)| value.map(|value| (key, value.to_string()))) - .collect() -} diff --git a/crates/shirabe-symfony-finder/src/finder.rs b/crates/shirabe-symfony-finder/src/finder.rs index cc78ebe3..d3c76e3f 100644 --- a/crates/shirabe-symfony-finder/src/finder.rs +++ b/crates/shirabe-symfony-finder/src/finder.rs @@ -9,7 +9,7 @@ use crate::glob::Glob; use chrono::{NaiveDate, NaiveDateTime}; use indexmap::IndexSet; -use shirabe_pcre::{CaptureKey, Preg, PregMatchedGroups}; +use shirabe_pcre::{CaptureKey, Preg}; use shirabe_php_shim::{file_exists, glob, is_dir, php_regex, preg_quote, rtrim}; use std::path::{Path, PathBuf}; use std::time::UNIX_EPOCH; @@ -642,13 +642,12 @@ fn is_regex(str: &str) -> bool { // PHP 8.2+ available modifiers. let available_modifiers = "imsxuADUn"; - let matches = PregMatchedGroups::new(); let pattern = format!("/^(.{{3,}}?)[{available_modifiers}]*$/"); if let Some(matches) = Preg::is_match3(&pattern, str) { let group = matches .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let bytes = group.as_bytes(); let start = bytes .first() @@ -694,13 +693,13 @@ fn parse_date_comparator(test: &str) -> (String, i64) { let date = matches .get(&CaptureKey::ByIndex(2)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let target = parse_datetime_to_unix(&date); let mut operator = matches .get(&CaptureKey::ByIndex(1)) - .cloned() + .map(str::to_string) .unwrap_or_else(|| "==".to_string()); if operator == "since" || operator == "after" { operator = ">".to_string(); diff --git a/crates/shirabe/src/autoload/autoload_generator.rs b/crates/shirabe/src/autoload/autoload_generator.rs index b6835b61..7851182f 100644 --- a/crates/shirabe/src/autoload/autoload_generator.rs +++ b/crates/shirabe/src/autoload/autoload_generator.rs @@ -23,7 +23,7 @@ use crate::util::Platform; use indexmap::IndexMap; use shirabe_class_map_generator::class_map::ClassMap; use shirabe_class_map_generator::class_map_generator::ClassMapGenerator; -use shirabe_pcre::{CaptureKey, Preg, PregMatchedGroups}; +use shirabe_pcre::{CaptureKey, Preg, PregMatches}; use shirabe_php_shim::{ InvalidArgumentException, PhpMixed, array_keys, array_map, array_merge_map, array_merge_recursive, array_shift, array_slice_strs, array_unique, bin2hex, explode, @@ -562,7 +562,7 @@ return array( if let Some(matches) = Preg::match3(php_regex!("{ComposerAutoloaderInit([^:\\s]+)::}"), &content) { - suffix = matches.get(&CaptureKey::ByIndex(1)).cloned(); + suffix = matches.get(&CaptureKey::ByIndex(1)).map(str::to_string); } } @@ -1153,7 +1153,7 @@ return array( let links = array_merge_map(package.get_replaces(), package.get_provides()); for (_k, link) in &links { if let Some(matches) = Preg::match3(php_regex!("{^ext-(.+)$}iD"), link.get_target()) - && let Some(ext) = matches.get(&CaptureKey::ByIndex(1)).cloned() + && let Some(ext) = matches.get(&CaptureKey::ByIndex(1)).map(str::to_string) { extension_providers .entry(ext) @@ -1200,8 +1200,8 @@ return array( { let ext_key = matches .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); // skip extension checks if they have a valid provider/replacer if let Some(provided_list) = extension_providers.get(&ext_key) { for provided in provided_list { @@ -1939,15 +1939,12 @@ class ComposerStaticInit{} std::cell::RefCell::new(None); let p = Preg::replace_callback( php_regex!("{^((?:(?:\\\\\\.){1,2}+/)+)}"), - |matches: &PregMatchedGroups| -> String { + |matches: &PregMatches| -> String { // undo preg_quote for the matched string *updir_cell.borrow_mut() = Some(str_replace( "\\.", ".", - matches - .get(&CaptureKey::ByIndex(1)) - .map(|s| s.as_str()) - .unwrap_or(""), + matches.get(&CaptureKey::ByIndex(1)).unwrap_or(""), )); String::new() diff --git a/crates/shirabe/src/cache.rs b/crates/shirabe/src/cache.rs index 793a2a07..43aed95d 100644 --- a/crates/shirabe/src/cache.rs +++ b/crates/shirabe/src/cache.rs @@ -205,8 +205,8 @@ impl Cache { let message = format!( "<warning>Writing {} into cache failed after {} of {} bytes written, only {} bytes of free space available</warning>", temp_file_name, - m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(), - m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(), + m.get(&CaptureKey::ByIndex(1)).unwrap_or_default(), + m.get(&CaptureKey::ByIndex(2)).unwrap_or_default(), free_space, ); diff --git a/crates/shirabe/src/command/archive_command.rs b/crates/shirabe/src/command/archive_command.rs index 09066c9f..c2166b45 100644 --- a/crates/shirabe/src/command/archive_command.rs +++ b/crates/shirabe/src/command/archive_command.rs @@ -234,12 +234,12 @@ impl ArchiveCommand { { let m1 = matches .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let m0 = matches .get(&CaptureKey::ByIndex(0)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); min_stability = VersionParser::normalize_stability(&m1)?; let full_match_len = m0.len(); version = Some(version_str[..version_str.len() - full_match_len].to_string()); diff --git a/crates/shirabe/src/command/config_command.rs b/crates/shirabe/src/command/config_command.rs index 35cd2ab6..5022d09f 100644 --- a/crates/shirabe/src/command/config_command.rs +++ b/crates/shirabe/src/command/config_command.rs @@ -713,8 +713,8 @@ impl Command for ConfigCommand { } else { let repo_key = matches .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let repos = data.get("repositories").cloned(); value = match repos .as_ref() @@ -1401,7 +1401,7 @@ impl Command for ConfigCommand { .unwrap() .add_config_setting(&key, PhpMixed::Array(obj)); } else if matches!( - matches.get(&CaptureKey::ByIndex(1)).unwrap().as_str(), + matches.get(&CaptureKey::ByIndex(1)).unwrap(), "github-oauth" | "gitlab-oauth" | "gitlab-token" | "bearer" ) { if 1 != values.len() { diff --git a/crates/shirabe/src/command/create_project_command.rs b/crates/shirabe/src/command/create_project_command.rs index ae46da30..8d7b4072 100644 --- a/crates/shirabe/src/command/create_project_command.rs +++ b/crates/shirabe/src/command/create_project_command.rs @@ -543,8 +543,8 @@ impl CreateProjectCommand { stability = Some( matched .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(), + .unwrap_or_default() + .to_string(), ); } else { stability = Some(VersionParser::parse_stability( diff --git a/crates/shirabe/src/command/diagnose_command.rs b/crates/shirabe/src/command/diagnose_command.rs index ff9e0e63..448de89f 100644 --- a/crates/shirabe/src/command/diagnose_command.rs +++ b/crates/shirabe/src/command/diagnose_command.rs @@ -868,8 +868,8 @@ impl DiagnoseCommand { ) { let configure = phpinfo_match .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let configure = configure.as_str(); if configure.contains("--enable-sigchild") { diff --git a/crates/shirabe/src/command/init_command.rs b/crates/shirabe/src/command/init_command.rs index f9a504be..81e1e381 100644 --- a/crates/shirabe/src/command/init_command.rs +++ b/crates/shirabe/src/command/init_command.rs @@ -94,7 +94,9 @@ impl InitCommand { php_regex!(r#"/^(?P<name>[- .,\p{L}\p{N}\p{Mn}\'’\"()]+)(?:\s+<(?P<email>.+?)>)?$/u"#), author, ) { - let email = m.get(&CaptureKey::ByName("email".to_string())).cloned(); + let email = m + .get(&CaptureKey::ByName("email".to_string())) + .map(str::to_string); if let Some(ref email) = email && !self.is_valid_email(email) { @@ -107,8 +109,7 @@ impl InitCommand { result.insert( "name".to_string(), Some(trim( - &m.get(&CaptureKey::ByName("name".to_string())) - .cloned() + m.get(&CaptureKey::ByName("name".to_string())) .unwrap_or_default(), None, )), diff --git a/crates/shirabe/src/command/package_discovery_trait.rs b/crates/shirabe/src/command/package_discovery_trait.rs index 4745a7d5..5b53bcbd 100644 --- a/crates/shirabe/src/command/package_discovery_trait.rs +++ b/crates/shirabe/src/command/package_discovery_trait.rs @@ -334,8 +334,9 @@ pub trait PackageDiscoveryTrait: BaseCommand { php_regex!(r"{^\s*(?P<name>[\S/]+)(?:\s+(?P<version>\S+))?\s*$}"), &selection, ) { - if let Some(v) = - m.get(&CaptureKey::ByName("version".to_string())).cloned() + if let Some(v) = m + .get(&CaptureKey::ByName("version".to_string())) + .map(str::to_string) { // parsing `acme/example ~2.3` // validate version constraint @@ -344,7 +345,6 @@ pub trait PackageDiscoveryTrait: BaseCommand { return Ok(PhpMixed::String(format!( "{} {}", m.get(&CaptureKey::ByName("name".to_string())) - .cloned() .unwrap_or_default(), v, ))); @@ -353,8 +353,8 @@ pub trait PackageDiscoveryTrait: BaseCommand { // parsing `acme/example` return Ok(PhpMixed::String( m.get(&CaptureKey::ByName("name".to_string())) - .cloned() - .unwrap_or_default(), + .unwrap_or_default() + .to_string(), )); } diff --git a/crates/shirabe/src/command/show_command.rs b/crates/shirabe/src/command/show_command.rs index 419de424..b479c9a8 100644 --- a/crates/shirabe/src/command/show_command.rs +++ b/crates/shirabe/src/command/show_command.rs @@ -1380,12 +1380,12 @@ impl ShowCommand { { let zero_major = groups .get(&CaptureKey::ByName("zero_major".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let first_meaningful = groups .get(&CaptureKey::ByName("first_meaningful".to_string())) - .cloned() .unwrap_or_default() + .to_string() .parse::<i64>() .unwrap_or(0); target_version = Some(format!( diff --git a/crates/shirabe/src/config.rs b/crates/shirabe/src/config.rs index 796e23fc..dd86149c 100644 --- a/crates/shirabe/src/config.rs +++ b/crates/shirabe/src/config.rs @@ -8,7 +8,7 @@ pub use json_config_source::*; use crate::io::io_interface; use indexmap::IndexMap; -use shirabe_pcre::{CaptureKey, Preg, PregMatchedGroups}; +use shirabe_pcre::{CaptureKey, Preg, PregMatches}; use shirabe_php_shim::{ E_USER_DEPRECATED, PhpMixed, RuntimeException, array_key_exists, array_merge, array_search_mixed, array_unique, empty, filter_var_url, implode, in_array_loose, @@ -659,11 +659,11 @@ impl Config { }; let mut size = matches .get(&CaptureKey::ByIndex(1)) - .cloned() .unwrap_or_default() + .to_string() .parse::<f64>() .unwrap_or(0.0); - let unit = matches.get(&CaptureKey::ByIndex(2)).cloned(); + let unit = matches.get(&CaptureKey::ByIndex(2)).map(str::to_string); if let Some(unit) = unit { match strtolower(&unit).as_str() { "g" => { @@ -959,8 +959,11 @@ impl Config { let mut error = None; let result = Preg::replace_callback( php_regex!(r"#\{\$(.+)\}#"), - |m: &PregMatchedGroups| -> String { - let key_match = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); + |m: &PregMatches| -> String { + let key_match = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); match self.get_with_flags(&key_match, flags) { Ok(v) => php_to_string(&v), Err(e) => { diff --git a/crates/shirabe/src/console/html_output_formatter.rs b/crates/shirabe/src/console/html_output_formatter.rs index 3b4018e3..4a5583fb 100644 --- a/crates/shirabe/src/console/html_output_formatter.rs +++ b/crates/shirabe/src/console/html_output_formatter.rs @@ -1,7 +1,7 @@ //! ref: composer/src/Composer/Console/HtmlOutputFormatter.php use indexmap::IndexMap; -use shirabe_pcre::{CaptureKey, Preg, PregMatchedGroups}; +use shirabe_pcre::{CaptureKey, Preg, PregMatches}; use shirabe_symfony_console::formatter::OutputFormatter; use shirabe_symfony_console::formatter::OutputFormatterInterface; use shirabe_symfony_console::formatter::OutputFormatterStyleInterface; @@ -72,15 +72,9 @@ impl HtmlOutputFormatter { ))) } - fn format_html(&self, matches: &PregMatchedGroups) -> String { - let codes_str = matches - .get(&CaptureKey::ByIndex(1)) - .map(|s| s.as_str()) - .unwrap_or(""); - let content = matches - .get(&CaptureKey::ByIndex(2)) - .map(|s| s.as_str()) - .unwrap_or(""); + fn format_html(&self, matches: &PregMatches) -> String { + let codes_str = matches.get(&CaptureKey::ByIndex(1)).unwrap_or(""); + let content = matches.get(&CaptureKey::ByIndex(2)).unwrap_or(""); let mut out = String::from("<span style=\""); for code_str in codes_str.split(';') { diff --git a/crates/shirabe/src/dependency_resolver/lock_transaction.rs b/crates/shirabe/src/dependency_resolver/lock_transaction.rs index 80a755b5..810c19af 100644 --- a/crates/shirabe/src/dependency_resolver/lock_transaction.rs +++ b/crates/shirabe/src/dependency_resolver/lock_transaction.rs @@ -176,11 +176,11 @@ impl LockTransaction { let dist_reference = present_package.get_dist_reference().unwrap(); let new_dist_url = Preg::replace_callback( php_regex!(r"{(/|sha=)[a-f0-9]{40}(/|$)}i"), - |m: &shirabe_pcre::PregMatchedGroups| -> String { + |m: &shirabe_pcre::PregMatches| -> String { let get = |i: usize| -> String { m.get(&shirabe_pcre::CaptureKey::ByIndex(i)) - .cloned() .unwrap_or_default() + .to_string() }; format!("{}{}{}", get(1), dist_reference, get(2)) }, diff --git a/crates/shirabe/src/dependency_resolver/problem.rs b/crates/shirabe/src/dependency_resolver/problem.rs index 80f0e5db..63561e98 100644 --- a/crates/shirabe/src/dependency_resolver/problem.rs +++ b/crates/shirabe/src/dependency_resolver/problem.rs @@ -234,11 +234,17 @@ impl Problem { None }; if let Some(m) = matched { + let pkg_key = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); + let m2 = m + .get(&CaptureKey::ByIndex(2)) + .unwrap_or_default() + .to_string(); message = str_replace("%", "%%", &message); let template = Preg::replace(php_regex!(r"{^\S+ \S+ }"), "%s%s ", &message); messages.push(template.clone()); - let pkg_key = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); - let m2 = m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(); let version_key = parser.normalize(&m2, Some("")).unwrap_or_default(); templates .entry(template.clone()) diff --git a/crates/shirabe/src/downloader/git_downloader.rs b/crates/shirabe/src/downloader/git_downloader.rs index d3f4441a..83f72300 100644 --- a/crates/shirabe/src/downloader/git_downloader.rs +++ b/crates/shirabe/src/downloader/git_downloader.rs @@ -101,8 +101,8 @@ impl GitDownloader { }; let head_ref = head_match .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let branches_match = Preg::is_match_all( format!("{{^{} refs/heads/(.+)$}}mi", preg_quote(&head_ref, None)), @@ -513,16 +513,16 @@ impl GitDownloader { let protocols = self.inner.config.borrow_mut().get("github-protocols"); let m1 = match_ .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let m2 = match_ .get(&CaptureKey::ByIndex(2)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let m3 = match_ .get(&CaptureKey::ByIndex(3)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let mut push_url = format!("git@{}:{}/{}.git", m1, m2, m3); if !in_array_strict("ssh".to_string(), protocols.values()) { push_url = format!("https://{}/{}/{}.git", m1, m2, m3); @@ -1112,12 +1112,12 @@ impl VcsDownloader for GitDownloader { { let origin_url = origin_match .get(&CaptureKey::ByName("url".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let composer_url = composer_match .get(&CaptureKey::ByName("url".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); if origin_url == composer_url && Some(composer_url.as_str()) != target.get_source_url().as_deref() { diff --git a/crates/shirabe/src/downloader/svn_downloader.rs b/crates/shirabe/src/downloader/svn_downloader.rs index ce908206..d146337a 100644 --- a/crates/shirabe/src/downloader/svn_downloader.rs +++ b/crates/shirabe/src/downloader/svn_downloader.rs @@ -386,8 +386,8 @@ impl VcsDownloader for SvnDownloader { let base_url = if let Some(matches) = Preg::match3(url_pattern, &output) { matches .get(&CaptureKey::ByIndex(1)) - .cloned() .unwrap_or_default() + .to_string() } else { return Err(RuntimeException::new(format!( "Unable to determine svn url for path {}", diff --git a/crates/shirabe/src/downloader/zip_downloader.rs b/crates/shirabe/src/downloader/zip_downloader.rs index cefada15..8ecd131f 100644 --- a/crates/shirabe/src/downloader/zip_downloader.rs +++ b/crates/shirabe/src/downloader/zip_downloader.rs @@ -116,7 +116,10 @@ impl ZipDownloader { && let Some(m) = Preg::is_match3(php_regex!(r"{^\s*7-Zip(?:\s\[64\])?\s([0-9.]+)}"), &output) { - let m1 = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); + let m1 = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); if version_compare(&m1, "21.01", CmpOp::Lt) { self.inner.io.borrow().write_error(&format!( " <warning>Unzipping using {} {} may result in incorrect file permissions. Install {} 21.01+ or unzip to ensure you get correct permissions.</warning>", diff --git a/crates/shirabe/src/event_dispatcher/event_dispatcher.rs b/crates/shirabe/src/event_dispatcher/event_dispatcher.rs index 6cc1670d..8617ba8f 100644 --- a/crates/shirabe/src/event_dispatcher/event_dispatcher.rs +++ b/crates/shirabe/src/event_dispatcher/event_dispatcher.rs @@ -971,8 +971,10 @@ try {{ if let Some(m) = Preg::is_match3(php_regex!("{^[^\\'\"\\s/\\\\]+}"), &path_and_args) { - let m0 = - m.get(&CaptureKey::ByIndex(0)).cloned().unwrap_or_default(); + let m0 = m + .get(&CaptureKey::ByIndex(0)) + .unwrap_or_default() + .to_string(); if !file_exists(&m0) { let finder = ExecutableFinder::new(); if let Some(path_to_exec) = finder.find(&m0, None, &[]) { diff --git a/crates/shirabe/src/installer/binary_installer.rs b/crates/shirabe/src/installer/binary_installer.rs index 9a7a2153..b382f7c1 100644 --- a/crates/shirabe/src/installer/binary_installer.rs +++ b/crates/shirabe/src/installer/binary_installer.rs @@ -205,12 +205,7 @@ impl BinaryInstaller { php_regex!(r"{^#!/(?:usr/bin/env )?(?:[^/]+/)*(.+)$}m"), &line, ) { - return trim( - m.get(&CaptureKey::ByIndex(1)) - .map(|s| s.as_str()) - .unwrap_or(""), - None, - ); + return trim(m.get(&CaptureKey::ByIndex(1)).unwrap_or(""), None); } "php".to_string() diff --git a/crates/shirabe/src/io/buffer_io.rs b/crates/shirabe/src/io/buffer_io.rs index a3311129..ce67c7eb 100644 --- a/crates/shirabe/src/io/buffer_io.rs +++ b/crates/shirabe/src/io/buffer_io.rs @@ -76,21 +76,20 @@ impl BufferIO { loop { let next = Preg::replace_callback( php_regex!(r"{(^|\n|\x08)(.+?)(\x08+)}"), - |matches: &shirabe_pcre::PregMatchedGroups| -> String { - let empty = String::new(); + |matches: &shirabe_pcre::PregMatches| -> String { let g1 = matches .get(&shirabe_pcre::CaptureKey::ByIndex(1)) - .unwrap_or(&empty); + .unwrap_or(""); let g2 = matches .get(&shirabe_pcre::CaptureKey::ByIndex(2)) - .unwrap_or(&empty); + .unwrap_or(""); let g3 = matches .get(&shirabe_pcre::CaptureKey::ByIndex(3)) - .unwrap_or(&empty); + .unwrap_or(""); let pre = strip_tags(g2); if pre.len() == g3.len() { - return g1.clone(); + return g1.to_string(); } // TODO reverse parse the string, skipping span tags and \033\[([0-9;]+)m(.*?)\033\[0m style blobs diff --git a/crates/shirabe/src/json/json_file.rs b/crates/shirabe/src/json/json_file.rs index ca8b11e0..2d1185e8 100644 --- a/crates/shirabe/src/json/json_file.rs +++ b/crates/shirabe/src/json/json_file.rs @@ -450,11 +450,8 @@ impl JsonFile { let indent_owned = options.indent; return Ok(Preg::replace_callback( php_regex!(r"#^ {4,}#m"), - move |m: &shirabe_pcre::PregMatchedGroups| -> String { - let whole = m - .get(&shirabe_pcre::CaptureKey::ByIndex(0)) - .map(|s| s.as_str()) - .unwrap_or(""); + move |m: &shirabe_pcre::PregMatches| -> String { + let whole = m.get(&shirabe_pcre::CaptureKey::ByIndex(0)).unwrap_or(""); str_repeat(&indent_owned, (strlen(whole) / 4) as usize) }, &json, @@ -555,7 +552,10 @@ impl JsonFile { pub fn detect_indenting(json: Option<&str>) -> String { if let Some(m) = Preg::is_match3(php_regex!(r##"#^([ \t]+)"#m"##), json.unwrap_or("")) { - return m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); + return m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); } Self::INDENT_DEFAULT.to_string() diff --git a/crates/shirabe/src/json/json_manipulator.rs b/crates/shirabe/src/json/json_manipulator.rs index c58f316d..92661d41 100644 --- a/crates/shirabe/src/json/json_manipulator.rs +++ b/crates/shirabe/src/json/json_manipulator.rs @@ -117,8 +117,8 @@ impl JsonManipulator { { let groups_1 = groups .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); // link missing but non empty links links = Preg::replace( format!("{{{}$}}", preg_quote(&groups_1, None)), @@ -1325,8 +1325,8 @@ impl JsonManipulator { { let tail_match_1 = tail_match .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); self.contents = Preg::replace( format!("#{}\\}}$#", tail_match_1), &addcslashes( diff --git a/crates/shirabe/src/package/loader/root_package_loader.rs b/crates/shirabe/src/package/loader/root_package_loader.rs index b7216c62..1d068ad6 100644 --- a/crates/shirabe/src/package/loader/root_package_loader.rs +++ b/crates/shirabe/src/package/loader/root_package_loader.rs @@ -256,8 +256,14 @@ impl RootPackageLoader { php_regex!(r"{(?:^|\| *|, *)([^,\s#|]+)(?:#[^ ]+)? +as +([^,\s|]+)(?:$| *\|| *,)}"), req_version, ) { - let m1 = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); - let m2 = m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(); + let m1 = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); + let m2 = m + .get(&CaptureKey::ByIndex(2)) + .unwrap_or_default() + .to_string(); let mut alias = IndexMap::new(); alias.insert("package".to_string(), strtolower(req_name)); alias.insert( @@ -318,7 +324,10 @@ impl RootPackageLoader { for constraint in &constraints { if let Some(m) = Preg::is_match3(&pattern, constraint) { let name = strtolower(req_name); - let m1 = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); + let m1 = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); let normalized_m1 = VersionParser::normalize_stability(&m1).unwrap_or_default(); let stability = stabilities[normalized_m1.as_str()]; @@ -368,7 +377,9 @@ impl RootPackageLoader { let name = strtolower(req_name); references.insert( name, - m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(), + m.get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(), ); } } diff --git a/crates/shirabe/src/package/locker.rs b/crates/shirabe/src/package/locker.rs index 6e7145ae..f27f44a2 100644 --- a/crates/shirabe/src/package/locker.rs +++ b/crates/shirabe/src/package/locker.rs @@ -849,8 +849,8 @@ impl Locker { ) { let ts = m .get(&CaptureKey::ByIndex(1)) - .cloned() .unwrap_or_default() + .to_string() .parse::<i64>() .unwrap_or(0); datetime = chrono::DateTime::from_timestamp(ts, 0); diff --git a/crates/shirabe/src/package/package.rs b/crates/shirabe/src/package/package.rs index 2054d396..c987f2fe 100644 --- a/crates/shirabe/src/package/package.rs +++ b/crates/shirabe/src/package/package.rs @@ -433,11 +433,11 @@ impl Package { // dist URL never carries more than one SHA reference. self.set_dist_url(Some(Preg::replace_callback( php_regex!("{(/|sha=)[a-f0-9]{40}(/|$)}i"), - |m: &shirabe_pcre::PregMatchedGroups| -> String { + |m: &shirabe_pcre::PregMatches| -> String { let get = |i: usize| -> String { m.get(&shirabe_pcre::CaptureKey::ByIndex(i)) - .cloned() .unwrap_or_default() + .to_string() }; format!("{}{}{}", get(1), reference, get(2)) }, diff --git a/crates/shirabe/src/package/version/version_guesser.rs b/crates/shirabe/src/package/version/version_guesser.rs index 14fcf5f6..daa25361 100644 --- a/crates/shirabe/src/package/version/version_guesser.rs +++ b/crates/shirabe/src/package/version/version_guesser.rs @@ -236,8 +236,14 @@ impl VersionGuesser { &branch, ) { - let g1 = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); - let g2 = m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(); + let g1 = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); + let g2 = m + .get(&CaptureKey::ByIndex(2)) + .unwrap_or_default() + .to_string(); if g1 == "(no branch)" || strpos(&g1, "(detached ") == Some(0) || strpos(&g1, "(HEAD detached at") == Some(0) @@ -264,7 +270,11 @@ impl VersionGuesser { &branch, ) { - branches.push(m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default()); + branches.push( + m.get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(), + ); } } @@ -753,7 +763,7 @@ impl VersionGuesser { if let Some(m) = Preg::is_match3(php_regex!(r"{^(\d+(?:\.\d+)*)-dev$}i"), &version) { return Ok(format!( "{}.x-dev", - m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default() + m.get(&CaptureKey::ByIndex(1)).unwrap_or_default() )); } diff --git a/crates/shirabe/src/platform/version.rs b/crates/shirabe/src/platform/version.rs index b89d052b..204bdccd 100644 --- a/crates/shirabe/src/platform/version.rs +++ b/crates/shirabe/src/platform/version.rs @@ -18,16 +18,16 @@ impl Version { let version = matches .get(&CaptureKey::ByName("version".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let patch_str = matches .get(&CaptureKey::ByName("patch".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let suffix_str = matches .get(&CaptureKey::ByName("suffix".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let patch = if version_compare(&version, "3.0.0", CmpOp::Lt) { format!( @@ -58,12 +58,12 @@ impl Version { let major = matches .get(&CaptureKey::ByName("major".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let minor = matches .get(&CaptureKey::ByName("minor".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); Some(format!( "{}.{}", major, @@ -79,12 +79,12 @@ impl Version { let year = matches .get(&CaptureKey::ByName("year".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let revision = matches .get(&CaptureKey::ByName("revision".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); Some(format!( "{}.{}", year, diff --git a/crates/shirabe/src/repository/composer_repository.rs b/crates/shirabe/src/repository/composer_repository.rs index 6feb4359..d42a9778 100644 --- a/crates/shirabe/src/repository/composer_repository.rs +++ b/crates/shirabe/src/repository/composer_repository.rs @@ -251,8 +251,8 @@ impl ComposerRepository { ) { let proto = match_packagist .get(&CaptureKey::ByName("proto".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); url = format!("{}://repo.packagist.org", proto); } @@ -786,12 +786,12 @@ impl ComposerRepository { { let q = match_groups .get(&CaptureKey::ByName("query".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let vendor = match_groups .get(&CaptureKey::ByName("vendor".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let url = format!( "{}?vendor={}&filter={}", list_url, @@ -2429,10 +2429,7 @@ impl ComposerRepository { if let Some(matches) = Preg::is_match3(php_regex!(r"{^[^:]++://[^/]*+}"), &self.url) { return Ok(format!( "{}{}", - matches - .get(&CaptureKey::ByIndex(0)) - .cloned() - .unwrap_or_default(), + matches.get(&CaptureKey::ByIndex(0)).unwrap_or_default(), url )); } diff --git a/crates/shirabe/src/repository/platform_repository.rs b/crates/shirabe/src/repository/platform_repository.rs index 29f66647..94ecc015 100644 --- a/crates/shirabe/src/repository/platform_repository.rs +++ b/crates/shirabe/src/repository/platform_repository.rs @@ -323,9 +323,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-librabbitmq", name), - librabbitmq_matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + librabbitmq_matches.get(&CaptureKey::ByName("version".to_string())), Some("AMQP librabbitmq version"), &[], &[], @@ -339,8 +337,8 @@ impl PlatformRepository { ) { let version_str = protocol_matches .get(&CaptureKey::ByName("version".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); self.add_library( &mut libraries, &format!("{}-protocol", name), @@ -362,9 +360,7 @@ impl PlatformRepository { self.add_library( &mut libraries, name, - matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + matches.get(&CaptureKey::ByName("version".to_string())), None, &[], &[], @@ -392,12 +388,12 @@ impl PlatformRepository { ) { let ssl_library_raw = ssl_matches .get(&CaptureKey::ByName("library".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let ssl_version = ssl_matches .get(&CaptureKey::ByName("version".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let library = strtolower(&ssl_library_raw); if library == "openssl" { let mut is_fips = false; @@ -426,8 +422,8 @@ impl PlatformRepository { shortlib = "securetransport".to_string(); let m1 = securetransport_matches .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); ssl_lib = format!("curl-{}", m1); } else { shortlib = library.clone(); @@ -457,12 +453,12 @@ impl PlatformRepository { ) { let ssh_library = ssh_matches .get(&CaptureKey::ByName("library".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let ssh_version = ssh_matches .get(&CaptureKey::ByName("version".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); self.add_library( &mut libraries, &format!("{}-{}", name, strtolower(&ssh_library)), @@ -480,9 +476,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-zlib", name), - zlib_matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + zlib_matches.get(&CaptureKey::ByName("version".to_string())), Some("curl zlib version"), &[], &[], @@ -500,9 +494,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-timelib", name), - timelib_matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + timelib_matches.get(&CaptureKey::ByName("version".to_string())), Some("date timelib version"), &[], &[], @@ -526,8 +518,8 @@ impl PlatformRepository { ) { let zoneinfo_version = zoneinfo_matches .get(&CaptureKey::ByName("version".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); // If the timezonedb is provided by ext/timezonedb, register that version as a replacement if external && loaded_extensions.iter().any(|n| n == "timezonedb") { self.add_library( @@ -564,9 +556,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-libmagic", name), - magic_matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + magic_matches.get(&CaptureKey::ByName("version".to_string())), Some("fileinfo libmagic version"), &[], &[], @@ -597,8 +587,8 @@ impl PlatformRepository { ) { let libjpeg_version = libjpeg_matches .get(&CaptureKey::ByName("version".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let parsed = Version::parse_libjpeg(&libjpeg_version).unwrap_or_default(); self.add_library( &mut libraries, @@ -616,9 +606,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-libpng", name), - libpng_matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + libpng_matches.get(&CaptureKey::ByName("version".to_string())), Some("libpng version for gd"), &[], &[], @@ -632,9 +620,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-freetype", name), - freetype_matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + freetype_matches.get(&CaptureKey::ByName("version".to_string())), Some("freetype version for gd"), &[], &[], @@ -719,9 +705,7 @@ impl PlatformRepository { self.add_library( &mut libraries, "icu", - matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + matches.get(&CaptureKey::ByName("version".to_string())), Some(description), &[], &[], @@ -736,8 +720,8 @@ impl PlatformRepository { ) { let zi_version = zoneinfo_matches .get(&CaptureKey::ByName("version".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); if let Some(parsed) = Version::parse_zoneinfo_version(&zi_version) { self.add_library( &mut libraries, @@ -799,8 +783,8 @@ impl PlatformRepository { ) { let mut version_built = matches .get(&CaptureKey::ByName("version".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); if let Some(patch) = matches.get(&CaptureKey::ByName("patch".to_string())) { version_built = format!("{}.{}", version_built, patch); } @@ -832,8 +816,8 @@ impl PlatformRepository { let converted = Version::convert_openldap_version_id(version_id); let vendor = vendor_matches .get(&CaptureKey::ByName("vendor".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); self.add_library( &mut libraries, &format!("{}-{}", name, strtolower(&vendor)), @@ -880,9 +864,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-libmbfl", name), - libmbfl_matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + libmbfl_matches.get(&CaptureKey::ByName("version".to_string())), Some("mbstring libmbfl version"), &[], &[], @@ -916,9 +898,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-oniguruma", name), - oniguruma_matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + oniguruma_matches.get(&CaptureKey::ByName("version".to_string())), Some("mbstring oniguruma version"), &[], &[], @@ -938,9 +918,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-libmemcached", name), - matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + matches.get(&CaptureKey::ByName("version".to_string())), Some("libmemcached version"), &[], &[], @@ -961,8 +939,8 @@ impl PlatformRepository { ) { let version = matches .get(&CaptureKey::ByName("version".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let mut is_fips = false; let parsed_version = Version::parse_openssl(&version, &mut is_fips).unwrap_or_default(); @@ -1001,9 +979,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-unicode", name), - pcre_unicode_matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + pcre_unicode_matches.get(&CaptureKey::ByName("version".to_string())), Some("PCRE Unicode version support"), &[], &[], @@ -1023,9 +999,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-mysqlnd", name), - matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + matches.get(&CaptureKey::ByName("version".to_string())), Some(&format!("mysqlnd library version for {}", name)), &[], &[], @@ -1043,9 +1017,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-libmongoc", name), - libmongoc_matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + libmongoc_matches.get(&CaptureKey::ByName("version".to_string())), Some("libmongoc version of mongodb"), &[], &[], @@ -1059,9 +1031,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-libbson", name), - libbson_matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + libbson_matches.get(&CaptureKey::ByName("version".to_string())), Some("libbson version of mongodb"), &[], &[], @@ -1095,9 +1065,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-libpq", name), - matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + matches.get(&CaptureKey::ByName("version".to_string())), Some(&format!("libpq for {}", name)), &[], &[], @@ -1116,9 +1084,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-libpq", name), - matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + matches.get(&CaptureKey::ByName("version".to_string())), Some(&format!("libpq for {}", name)), &[], &[], @@ -1138,9 +1104,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-libpq", name), - matches - .get(&CaptureKey::ByName("linked".to_string())) - .map(|s| s.as_str()), + matches.get(&CaptureKey::ByName("linked".to_string())), Some(&format!("libpq for {}", name)), &[], &[], @@ -1213,9 +1177,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-sqlite", name), - matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + matches.get(&CaptureKey::ByName("version".to_string())), None, &[], &[], @@ -1232,9 +1194,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-libssh2", name), - matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + matches.get(&CaptureKey::ByName("version".to_string())), None, &[], &[], @@ -1268,9 +1228,7 @@ impl PlatformRepository { self.add_library( &mut libraries, "libxslt-libxml", - matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + matches.get(&CaptureKey::ByName("version".to_string())), Some("libxml version libxslt is compiled against"), &[], &[], @@ -1287,9 +1245,7 @@ impl PlatformRepository { self.add_library( &mut libraries, &format!("{}-libyaml", name), - matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + matches.get(&CaptureKey::ByName("version".to_string())), Some("libyaml version of yaml"), &[], &[], @@ -1342,9 +1298,7 @@ impl PlatformRepository { self.add_library( &mut libraries, name, - matches - .get(&CaptureKey::ByName("version".to_string())) - .map(|s| s.as_str()), + matches.get(&CaptureKey::ByName("version".to_string())), None, &[], &[], @@ -1540,7 +1494,10 @@ impl PlatformRepository { php_regex!("{^(\\d+\\.\\d+\\.\\d+(?:\\.\\d+)?)}"), &pretty_version, ) { - pretty_version = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); + pretty_version = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); } else { pretty_version = "0".to_string(); } diff --git a/crates/shirabe/src/repository/vcs/forgejo_driver.rs b/crates/shirabe/src/repository/vcs/forgejo_driver.rs index 9e156418..8aa72a6c 100644 --- a/crates/shirabe/src/repository/vcs/forgejo_driver.rs +++ b/crates/shirabe/src/repository/vcs/forgejo_driver.rs @@ -587,7 +587,7 @@ impl ForgejoDriver { if let Some(m) = Preg::match3(php_regex!(r#"{<(.+?)>; *rel="next"}"#), &link) && let Some(url) = m.get(&CaptureKey::ByIndex(1)) { - return Some(url.clone()); + return Some(url.to_string()); } } diff --git a/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs b/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs index cf1f5f98..e053cadf 100644 --- a/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs +++ b/crates/shirabe/src/repository/vcs/git_bitbucket_driver.rs @@ -95,8 +95,14 @@ impl GitBitbucketDriver { .into()); }; - self.owner = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); - self.repository = m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(); + self.owner = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); + self.repository = m + .get(&CaptureKey::ByIndex(2)) + .unwrap_or_default() + .to_string(); self.inner.origin_url = "bitbucket.org".to_string(); self.inner.cache = Some(Cache::new( self.inner.io.clone(), diff --git a/crates/shirabe/src/repository/vcs/git_driver.rs b/crates/shirabe/src/repository/vcs/git_driver.rs index 99275e98..bd56766b 100644 --- a/crates/shirabe/src/repository/vcs/git_driver.rs +++ b/crates/shirabe/src/repository/vcs/git_driver.rs @@ -202,7 +202,7 @@ impl GitDriver { && let Some(caps) = Preg::match3(php_regex!(r"{^\* +(\S+)}"), branch) && let Some(name) = caps.get(&CaptureKey::ByIndex(1)) { - self.root_identifier = Some(name.clone()); + self.root_identifier = Some(name.to_string()); break; } } @@ -321,7 +321,7 @@ impl GitDriver { self.tags .as_mut() .unwrap() - .insert(name.clone(), hash.clone()); + .insert(name.to_string(), hash.to_string()); } } } @@ -358,7 +358,7 @@ impl GitDriver { ) && !name.starts_with('-') { - branches.insert(name.clone(), hash.clone()); + branches.insert(name.to_string(), hash.to_string()); } } diff --git a/crates/shirabe/src/repository/vcs/github_driver.rs b/crates/shirabe/src/repository/vcs/github_driver.rs index 7cbceaf2..08561171 100644 --- a/crates/shirabe/src/repository/vcs/github_driver.rs +++ b/crates/shirabe/src/repository/vcs/github_driver.rs @@ -85,22 +85,22 @@ impl GitHubDriver { self.owner = match_ .get(&CaptureKey::ByIndex(3)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); self.repository = match_ .get(&CaptureKey::ByIndex(4)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); self.inner.origin_url = strtolower( &match_ .get(&CaptureKey::ByIndex(1)) - .cloned() .filter(|s| !s.is_empty()) + .map(str::to_string) .unwrap_or_else(|| { match_ .get(&CaptureKey::ByIndex(2)) - .cloned() .unwrap_or_default() + .to_string() }), ); if self.inner.origin_url == "www.github.com" { @@ -494,14 +494,23 @@ impl GitHubDriver { for line in preg_split(php_regex!(r"{\r?\n}"), &funding) { let line = trim(&line, None); if let Some(m) = Preg::is_match3(php_regex!(r"{^(\w+)\s*:\s*(.+)$}"), &line) { - let g1 = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); - let g2 = m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(); + let g1 = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); + let g2 = m + .get(&CaptureKey::ByIndex(2)) + .unwrap_or_default() + .to_string(); if g2 == "[" { key = Some(g1); continue; } if let Some(m2) = Preg::is_match3(php_regex!(r"{^\[(.*?)\](?:\s*#.*)?$}"), &g2) { - let inner = m2.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); + let inner = m2 + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); for item in array_map( |s: &String| trim(s, None), &preg_split(php_regex!(r#"{[\'\"]?\s*,\s*[\'\"]?}"#), &inner), @@ -522,7 +531,7 @@ impl GitHubDriver { entry.insert( "url".to_string(), PhpMixed::String(trim( - &m2.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(), + m2.get(&CaptureKey::ByIndex(1)).unwrap_or_default(), Some("\"' "), )), ); @@ -530,7 +539,11 @@ impl GitHubDriver { } key = None; } else if let Some(m) = Preg::is_match3(php_regex!(r"{^(\w+)\s*:\s*#\s*$}"), &line) { - key = Some(m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default()); + key = Some( + m.get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(), + ); } else if key.is_some() && let Some(m) = Preg::is_match3(php_regex!(r"{^-\s*(.+)(?:\s+#.*)?$}"), &line) .or_else(|| Preg::is_match3(php_regex!(r"{^(.+),(?:\s*#.*)?$}"), &line)) @@ -543,7 +556,7 @@ impl GitHubDriver { entry.insert( "url".to_string(), PhpMixed::String(trim( - &m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(), + m.get(&CaptureKey::ByIndex(1)).unwrap_or_default(), Some("\"' "), )), ); @@ -936,13 +949,13 @@ impl GitHubDriver { let origin_url = matches .get(&CaptureKey::ByIndex(2)) - .cloned() .filter(|s| !s.is_empty()) + .map(str::to_string) .unwrap_or_else(|| { matches .get(&CaptureKey::ByIndex(3)) - .cloned() .unwrap_or_default() + .to_string() }); if !in_array_loose( strtolower(&Preg::replace(php_regex!(r"{^www\.}i"), "", &origin_url)), @@ -1272,7 +1285,11 @@ impl GitHubDriver { let links = explode(",", &header); for link in &links { if let Some(m) = Preg::is_match3(php_regex!(r#"{<(.+?)>; *rel="next"}"#), link) { - return Some(m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default()); + return Some( + m.get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(), + ); } } diff --git a/crates/shirabe/src/repository/vcs/gitlab_driver.rs b/crates/shirabe/src/repository/vcs/gitlab_driver.rs index 1887ddc4..5494feed 100644 --- a/crates/shirabe/src/repository/vcs/gitlab_driver.rs +++ b/crates/shirabe/src/repository/vcs/gitlab_driver.rs @@ -91,27 +91,26 @@ impl GitLabDriver { let guessed_domain = match_ .get(&CaptureKey::ByName("domain".to_string())) - .cloned() .filter(|s| !s.is_empty()) + .map(str::to_string) .unwrap_or_else(|| { match_ .get(&CaptureKey::ByName("domain2".to_string())) - .cloned() .unwrap_or_default() + .to_string() }); let configured_domains = self.inner.config.borrow_mut().get("gitlab-domains"); let mut url_parts: Vec<String> = explode( "/", - &match_ + match_ .get(&CaptureKey::ByName("parts".to_string())) - .cloned() .unwrap_or_default(), ); let scheme_match = match_ .get(&CaptureKey::ByName("scheme".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); self.scheme = if matches!(scheme_match.as_str(), "https" | "http") { scheme_match } else if self @@ -125,7 +124,9 @@ impl GitLabDriver { } else { "https".to_string() }; - let port = match_.get(&CaptureKey::ByName("port".to_string())).cloned(); + let port = match_ + .get(&CaptureKey::ByName("port".to_string())) + .map(str::to_string); let origin = Self::determine_origin(&configured_domains, guessed_domain, &mut url_parts, port); let origin = match origin { @@ -169,9 +170,8 @@ impl GitLabDriver { self.repository = Preg::replace( php_regex!(r"#(\.git)$#"), "", - &match_ + match_ .get(&CaptureKey::ByName("repo".to_string())) - .cloned() .unwrap_or_default(), ); @@ -950,23 +950,22 @@ impl GitLabDriver { let scheme = match_ .get(&CaptureKey::ByName("scheme".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let guessed_domain = match_ .get(&CaptureKey::ByName("domain".to_string())) - .cloned() .filter(|s| !s.is_empty()) + .map(str::to_string) .unwrap_or_else(|| { match_ .get(&CaptureKey::ByName("domain2".to_string())) - .cloned() .unwrap_or_default() + .to_string() }); let mut url_parts: Vec<String> = explode( "/", - &match_ + match_ .get(&CaptureKey::ByName("parts".to_string())) - .cloned() .unwrap_or_default(), ); @@ -974,7 +973,9 @@ impl GitLabDriver { &config.borrow().get("gitlab-domains"), guessed_domain, &mut url_parts, - match_.get(&CaptureKey::ByName("port".to_string())).cloned(), + match_ + .get(&CaptureKey::ByName("port".to_string())) + .map(str::to_string), ) .is_none() { @@ -1013,8 +1014,8 @@ impl GitLabDriver { return Some( match_ .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(), + .unwrap_or_default() + .to_string(), ); } } diff --git a/crates/shirabe/src/repository/vcs/hg_driver.rs b/crates/shirabe/src/repository/vcs/hg_driver.rs index 0933a643..aae9c7cb 100644 --- a/crates/shirabe/src/repository/vcs/hg_driver.rs +++ b/crates/shirabe/src/repository/vcs/hg_driver.rs @@ -236,8 +236,12 @@ impl HgDriver { && let Some(m) = Preg::match3(php_regex!(r"(^([^\s]+)\s+\d+:(.*)$)"), &tag) { tags.insert( - m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(), - m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(), + m.get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(), + m.get(&CaptureKey::ByIndex(2)) + .unwrap_or_default() + .to_string(), ); } } @@ -265,11 +269,16 @@ impl HgDriver { && let Some(m) = Preg::match3(php_regex!(r"(^([^\s]+)\s+\d+:([a-f0-9]+))"), &branch) { - let name = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); + let name = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); if !name.starts_with('-') { branches.insert( name, - m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(), + m.get(&CaptureKey::ByIndex(2)) + .unwrap_or_default() + .to_string(), ); } } @@ -286,11 +295,16 @@ impl HgDriver { && let Some(m) = Preg::match3(php_regex!(r"(^(?:[\s*]*)([^\s]+)\s+\d+:(.*)$)"), &branch) { - let name = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); + let name = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); if !name.starts_with('-') { bookmarks.insert( name, - m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(), + m.get(&CaptureKey::ByIndex(2)) + .unwrap_or_default() + .to_string(), ); } } diff --git a/crates/shirabe/src/repository/vcs/svn_driver.rs b/crates/shirabe/src/repository/vcs/svn_driver.rs index 05380a17..9426e1f3 100644 --- a/crates/shirabe/src/repository/vcs/svn_driver.rs +++ b/crates/shirabe/src/repository/vcs/svn_driver.rs @@ -321,7 +321,10 @@ impl SvnDriver { && let Some(m) = Preg::is_match3(php_regex!(r"{^Last Changed Date: ([^(]+)}"), &line) { - let date_str = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); + let date_str = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); return Ok(shirabe_php_shim::date_create::<Utc>(date_str.trim()) .ok() .map(|d| d.fixed_offset())); @@ -353,7 +356,10 @@ impl SvnDriver { .get(&CaptureKey::ByIndex(1)) .and_then(|s| s.parse().ok()) .unwrap_or(0); - let path = m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(); + let path = m + .get(&CaptureKey::ByIndex(2)) + .unwrap_or_default() + .to_string(); if path == "./" { last_rev = rev; } else { @@ -399,7 +405,10 @@ impl SvnDriver { .get(&CaptureKey::ByIndex(1)) .and_then(|s| s.parse().ok()) .unwrap_or(0); - let path = m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(); + let path = m + .get(&CaptureKey::ByIndex(2)) + .unwrap_or_default() + .to_string(); if path == "./" { let identifier = self.build_identifier( &format!("/{}", self.trunk_path.clone().unwrap_or_default()), @@ -437,7 +446,10 @@ impl SvnDriver { .get(&CaptureKey::ByIndex(1)) .and_then(|s| s.parse().ok()) .unwrap_or(0); - let path = m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(); + let path = m + .get(&CaptureKey::ByIndex(2)) + .unwrap_or_default() + .to_string(); if path == "./" { last_rev = rev; } else { diff --git a/crates/shirabe/src/util/composer_mirror.rs b/crates/shirabe/src/util/composer_mirror.rs index 7344bfaf..5d9e8c31 100644 --- a/crates/shirabe/src/util/composer_mirror.rs +++ b/crates/shirabe/src/util/composer_mirror.rs @@ -61,14 +61,8 @@ impl ComposerMirror { ) { format!( "gh-{}/{}", - gh_matches - .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(), - gh_matches - .get(&CaptureKey::ByIndex(2)) - .cloned() - .unwrap_or_default(), + gh_matches.get(&CaptureKey::ByIndex(1)).unwrap_or_default(), + gh_matches.get(&CaptureKey::ByIndex(2)).unwrap_or_default(), ) } else if let Some(bb_matches) = Preg::match3( php_regex!(r"#^https://bitbucket\.org/([^/]+)/(.+?)(?:\.git)?/?$#"), @@ -76,14 +70,8 @@ impl ComposerMirror { ) { format!( "bb-{}/{}", - bb_matches - .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(), - bb_matches - .get(&CaptureKey::ByIndex(2)) - .cloned() - .unwrap_or_default(), + bb_matches.get(&CaptureKey::ByIndex(1)).unwrap_or_default(), + bb_matches.get(&CaptureKey::ByIndex(2)).unwrap_or_default(), ) } else { Preg::replace(php_regex!(r"{[^a-z0-9_.-]}i"), "-", url.trim_matches('/')) diff --git a/crates/shirabe/src/util/filesystem.rs b/crates/shirabe/src/util/filesystem.rs index d59df55e..6302a6d2 100644 --- a/crates/shirabe/src/util/filesystem.rs +++ b/crates/shirabe/src/util/filesystem.rs @@ -741,8 +741,8 @@ impl Filesystem { ) { prefix = prefix_match .get(&shirabe_pcre::CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); path = substr(&path, strlen(&prefix), None); } @@ -765,11 +765,11 @@ impl Filesystem { // ensure c: is normalized to C: prefix = Preg::replace_callback( php_regex!("{(^|://)[a-z]:$}i"), - |m: &shirabe_pcre::PregMatchedGroups| -> String { + |m: &shirabe_pcre::PregMatches| -> String { let s = m .get(&shirabe_pcre::CaptureKey::ByIndex(0)) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); strtoupper(&s) }, &prefix, diff --git a/crates/shirabe/src/util/forgejo_url.rs b/crates/shirabe/src/util/forgejo_url.rs index e5304f5d..7f9333e0 100644 --- a/crates/shirabe/src/util/forgejo_url.rs +++ b/crates/shirabe/src/util/forgejo_url.rs @@ -43,8 +43,8 @@ impl ForgejoUrl { .map(|i| { matches .get(&CaptureKey::ByIndex(i)) - .cloned() .unwrap_or_default() + .to_string() }) .collect(); diff --git a/crates/shirabe/src/util/git.rs b/crates/shirabe/src/util/git.rs index 2b012d4c..e7239987 100644 --- a/crates/shirabe/src/util/git.rs +++ b/crates/shirabe/src/util/git.rs @@ -14,7 +14,7 @@ use crate::util::ProcessExecutor; use crate::util::Url; use crate::util::{AuthHelper, StoreAuth}; use indexmap::IndexMap; -use shirabe_pcre::{CaptureKey, Preg, PregMatchedGroups}; +use shirabe_pcre::{CaptureKey, Preg, PregMatches}; use shirabe_php_shim::{ AnyThrowable, CmpOp, InvalidArgumentException, PHP_EOL, PhpMixed, RuntimeException, array_map, clearstatcache, explode, implode, in_array_loose, in_array_strict, is_dir, php_regex, @@ -230,13 +230,16 @@ impl Git { php_regex!(r"{^(?:composer|origin)\s+https?://(.+):(.+)@([^/]+)}im"), &output, ) { - let m3 = m.get(&CaptureKey::ByIndex(3)).cloned().unwrap_or_default(); + let m3 = m + .get(&CaptureKey::ByIndex(3)) + .unwrap_or_default() + .to_string(); if !self.io.has_authentication(&m3) { self.io.borrow_mut().set_authentication( m3, - rawurldecode(&m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default()), + rawurldecode(m.get(&CaptureKey::ByIndex(1)).unwrap_or_default()), Some(rawurldecode( - &m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(), + m.get(&CaptureKey::ByIndex(2)).unwrap_or_default(), )), ); } @@ -262,8 +265,14 @@ impl Git { _ => vec![], }; for protocol in &protocols_list { - let m1 = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); - let m2 = m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(); + let m1 = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); + let m2 = m + .get(&CaptureKey::ByIndex(2)) + .unwrap_or_default() + .to_string(); let proto_url = if protocol == "ssh" { format!("git@{}:{}", m1, m2) } else { @@ -291,7 +300,10 @@ impl Git { } // failed to checkout, first check git accessibility - let m1 = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); + let m1 = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); if !self.io.has_authentication(&m1) && !self.io.is_interactive() { self.throw_exception( &format!( @@ -357,8 +369,14 @@ impl Git { ) }); if let Some(m) = github_matched { - let m1 = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); - let m2 = m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(); + let m1 = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); + let m2 = m + .get(&CaptureKey::ByIndex(2)) + .unwrap_or_default() + .to_string(); if !self.io.has_authentication(&m1) { let mut git_hub_util = GitHub::new( self.io.clone(), @@ -421,9 +439,14 @@ impl Git { None, )?; - let domain = m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(); - let mut repo_with_git_part = - m.get(&CaptureKey::ByIndex(3)).cloned().unwrap_or_default(); + let domain = m + .get(&CaptureKey::ByIndex(2)) + .unwrap_or_default() + .to_string(); + let mut repo_with_git_part = m + .get(&CaptureKey::ByIndex(3)) + .unwrap_or_default() + .to_string(); if !repo_with_git_part.ends_with(".git") { repo_with_git_part.push_str(".git"); } @@ -565,9 +588,18 @@ impl Git { url, ) }) { - let mut m1 = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); - let m2 = m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(); - let m3 = m.get(&CaptureKey::ByIndex(3)).cloned().unwrap_or_default(); + let mut m1 = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); + let m2 = m + .get(&CaptureKey::ByIndex(2)) + .unwrap_or_default() + .to_string(); + let m3 = m + .get(&CaptureKey::ByIndex(3)) + .unwrap_or_default() + .to_string(); if m1 == "git" { m1 = "https".to_string(); } @@ -641,9 +673,18 @@ impl Git { } } else if let Some(m) = self.get_authentication_failure(url) { // private non-github/gitlab/bitbucket repo that failed to authenticate - let m1 = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); - let mut m2 = m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(); - let m3 = m.get(&CaptureKey::ByIndex(3)).cloned().unwrap_or_default(); + let m1 = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); + let mut m2 = m + .get(&CaptureKey::ByIndex(2)) + .unwrap_or_default() + .to_string(); + let m3 = m + .get(&CaptureKey::ByIndex(3)) + .unwrap_or_default() + .to_string(); let mut auth_parts: Option<String> = None; if m2.contains("@") { let parts = explode("@", &m2); @@ -1083,7 +1124,7 @@ impl Git { Ok(false) } - fn get_authentication_failure(&self, url: &str) -> Option<PregMatchedGroups> { + fn get_authentication_failure<'u>(&self, url: &'u str) -> Option<PregMatches<'u>> { let m = Preg::is_match3(php_regex!(r"{^(https?://)([^/]+)(.*)$}i"), url)?; let auth_failures = [ @@ -1172,8 +1213,8 @@ impl Git { return Ok(Some( matches .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(), + .unwrap_or_default() + .to_string(), )); } } @@ -1295,7 +1336,7 @@ impl Git { && let Some(matches) = Preg::is_match3(php_regex!(r"/^git version (\d+(?:\.\d+)+)/m"), &output) { - *version = Some(matches.get(&CaptureKey::ByIndex(1)).cloned()); + *version = Some(matches.get(&CaptureKey::ByIndex(1)).map(str::to_string)); } } version.clone().unwrap_or(None) diff --git a/crates/shirabe/src/util/github.rs b/crates/shirabe/src/util/github.rs index 9c22a2b3..28a13738 100644 --- a/crates/shirabe/src/util/github.rs +++ b/crates/shirabe/src/util/github.rs @@ -326,7 +326,9 @@ impl GitHub { continue; } if let Some(caps) = Preg::match3(php_regex!(r"{\burl=(?P<url>[^\s;]+)}"), header) { - return caps.get(&CaptureKey::ByName("url".to_string())).cloned(); + return caps + .get(&CaptureKey::ByName("url".to_string())) + .map(str::to_string); } } diff --git a/crates/shirabe/src/util/http/response.rs b/crates/shirabe/src/util/http/response.rs index ea961768..7c5f5248 100644 --- a/crates/shirabe/src/util/http/response.rs +++ b/crates/shirabe/src/util/http/response.rs @@ -68,7 +68,7 @@ impl Response { if let Some(matches) = Preg::match3(&pattern, header) && let Some(s) = matches.get(&shirabe_pcre::CaptureKey::ByIndex(1)) { - value = Some(s.clone()); + value = Some(s.to_string()); } } value diff --git a/crates/shirabe/src/util/http_downloader.rs b/crates/shirabe/src/util/http_downloader.rs index b06d7004..c6e70117 100644 --- a/crates/shirabe/src/util/http_downloader.rs +++ b/crates/shirabe/src/util/http_downloader.rs @@ -246,14 +246,14 @@ impl HttpDownloader { origin.clone(), rawurldecode( m.get(&CaptureKey::ByIndex(1)) - .cloned() .unwrap_or_default() + .to_string() .as_str(), ), Some(rawurldecode( m.get(&CaptureKey::ByIndex(2)) - .cloned() .unwrap_or_default() + .to_string() .as_str(), )), ); diff --git a/crates/shirabe/src/util/platform.rs b/crates/shirabe/src/util/platform.rs index 629854b6..f2b8aea3 100644 --- a/crates/shirabe/src/util/platform.rs +++ b/crates/shirabe/src/util/platform.rs @@ -2,7 +2,7 @@ use crate::util::ProcessExecutor; use crate::util::Silencer; -use shirabe_pcre::{Preg, PregMatchedGroups}; +use shirabe_pcre::{Preg, PregMatches}; use shirabe_php_shim::{ PHP_ENV, PHP_SERVER, PhpMixed, PhpResource, RuntimeException, defined, file_exists, file_get_contents, fstat, function_exists, getcwd, getenv, ini_get, is_readable, mb_strlen, @@ -98,15 +98,13 @@ impl Platform { // two forms are written as an explicit alternation: `$VAR` or `%VAR%`. Preg::replace_callback( php_regex!(r"#^(?:\$(?P<dvar>\w+)|%(?P<pvar>\w+)%)(?P<path>.*)#"), - |matches: &PregMatchedGroups| -> String { + |matches: &PregMatches| -> String { let var = matches .get(&CaptureKey::ByName("dvar".to_string())) .or_else(|| matches.get(&CaptureKey::ByName("pvar".to_string()))) - .map(|s| s.as_str()) .unwrap_or(""); let path_part = matches .get(&CaptureKey::ByName("path".to_string())) - .map(|s| s.as_str()) .unwrap_or(""); // Treat HOME as an alias for USERPROFILE on Windows for legacy reasons if Platform::is_windows() && var == "HOME" { diff --git a/crates/shirabe/src/util/process_executor.rs b/crates/shirabe/src/util/process_executor.rs index 48fa415f..2954ed7a 100644 --- a/crates/shirabe/src/util/process_executor.rs +++ b/crates/shirabe/src/util/process_executor.rs @@ -7,7 +7,7 @@ use crate::signal::SignalSubscription; use crate::util::GitHub; use crate::util::Platform; use indexmap::IndexMap; -use shirabe_pcre::{CaptureKey, Preg, PregMatchedGroups}; +use shirabe_pcre::{CaptureKey, Preg, PregMatches}; use shirabe_php_shim::Catch as _; use shirabe_php_shim::{ LogicException, PHP_EOL, PhpMixed, RuntimeException, array_intersect, array_map, @@ -219,7 +219,10 @@ impl ProcessExecutor { if Platform::is_windows() && let Some(m) = Preg::is_match3(php_regex!(r"{^([^:/\\]++) }"), &command_str) { - let m1 = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(); + let m1 = m + .get(&CaptureKey::ByIndex(1)) + .unwrap_or_default() + .to_string(); command_str = substr_replace( &command_str, &Self::escape(&Self::get_executable(&m1)), @@ -831,23 +834,20 @@ impl ProcessExecutor { }; let safe_command = Preg::replace_callback( php_regex!(r"{://(?P<user>[^:/\s]+):(?P<password>[^@\s/]+)@}i"), - |m: &PregMatchedGroups| -> String { + |m: &PregMatches| -> String { let user_key = CaptureKey::ByName("user".to_string()); // if the username looks like a long (12char+) hex string, or a modern github token (e.g. ghp_xxx, github_pat_xxx) we obfuscate that if Preg::is_match( GitHub::GITHUB_TOKEN_REGEX, - m.get(&user_key).cloned().unwrap_or_default().as_str(), + m.get(&user_key).unwrap_or_default(), ) { return "://***:***@".to_string(); } - if Preg::is_match( - r"{^[a-f0-9]{12,}$}", - m.get(&user_key).cloned().unwrap_or_default().as_str(), - ) { + if Preg::is_match(r"{^[a-f0-9]{12,}$}", m.get(&user_key).unwrap_or_default()) { return "://***:***@".to_string(); } - format!("://{}:***@", m.get(&user_key).cloned().unwrap_or_default()) + format!("://{}:***@", m.get(&user_key).unwrap_or_default()) }, &command_string, ); diff --git a/crates/shirabe/src/util/svn.rs b/crates/shirabe/src/util/svn.rs index 8018efa5..72ca2321 100644 --- a/crates/shirabe/src/util/svn.rs +++ b/crates/shirabe/src/util/svn.rs @@ -410,8 +410,8 @@ impl Svn { *cached = Some( matches .get(&CaptureKey::ByIndex(1)) - .cloned() - .unwrap_or_default(), + .unwrap_or_default() + .to_string(), ); } } diff --git a/crates/shirabe/src/util/url.rs b/crates/shirabe/src/util/url.rs index 0a3722eb..8b751b2a 100644 --- a/crates/shirabe/src/util/url.rs +++ b/crates/shirabe/src/util/url.rs @@ -22,9 +22,9 @@ impl Url { ) { url = format!( "https://api.github.com/repos/{}/{}/{}ball/{}", - m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(), - m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(), - m.get(&CaptureKey::ByIndex(3)).cloned().unwrap_or_default(), + m.get(&CaptureKey::ByIndex(1)).unwrap_or_default(), + m.get(&CaptureKey::ByIndex(2)).unwrap_or_default(), + m.get(&CaptureKey::ByIndex(3)).unwrap_or_default(), r#ref ); } else if let Some(m) = Preg::match3( @@ -35,9 +35,9 @@ impl Url { ) { url = format!( "https://api.github.com/repos/{}/{}/{}ball/{}", - m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(), - m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(), - m.get(&CaptureKey::ByIndex(3)).cloned().unwrap_or_default(), + m.get(&CaptureKey::ByIndex(1)).unwrap_or_default(), + m.get(&CaptureKey::ByIndex(2)).unwrap_or_default(), + m.get(&CaptureKey::ByIndex(3)).unwrap_or_default(), r#ref ); } else if let Some(m) = Preg::match3( @@ -48,9 +48,9 @@ impl Url { ) { url = format!( "https://api.github.com/repos/{}/{}/{}ball/{}", - m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(), - m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(), - m.get(&CaptureKey::ByIndex(3)).cloned().unwrap_or_default(), + m.get(&CaptureKey::ByIndex(1)).unwrap_or_default(), + m.get(&CaptureKey::ByIndex(2)).unwrap_or_default(), + m.get(&CaptureKey::ByIndex(3)).unwrap_or_default(), r#ref ); } @@ -63,10 +63,10 @@ impl Url { ) { url = format!( "https://bitbucket.org/{}/{}/get/{}.{}", - m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(), - m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(), + m.get(&CaptureKey::ByIndex(1)).unwrap_or_default(), + m.get(&CaptureKey::ByIndex(2)).unwrap_or_default(), r#ref, - m.get(&CaptureKey::ByIndex(4)).cloned().unwrap_or_default() + m.get(&CaptureKey::ByIndex(4)).unwrap_or_default() ); } } else if host == "gitlab.com" || host == "www.gitlab.com" { @@ -78,8 +78,8 @@ impl Url { ) { url = format!( "https://gitlab.com/api/v4/projects/{}/repository/archive.{}?sha={}", - m.get(&CaptureKey::ByIndex(1)).cloned().unwrap_or_default(), - m.get(&CaptureKey::ByIndex(2)).cloned().unwrap_or_default(), + m.get(&CaptureKey::ByIndex(1)).unwrap_or_default(), + m.get(&CaptureKey::ByIndex(2)).unwrap_or_default(), r#ref ); } @@ -165,12 +165,12 @@ impl Url { |m| { let user = m .get(&CaptureKey::ByName("user".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); let prefix = m .get(&CaptureKey::ByName("prefix".to_string())) - .cloned() - .unwrap_or_default(); + .unwrap_or_default() + .to_string(); // if the username looks like a long (12char+) hex string, or a modern github token (e.g. ghp_xxx, github_pat_xxx) we obfuscate that if Preg::is_match(GitHub::GITHUB_TOKEN_REGEX, &user) { format!("{}***:***@", prefix) diff --git a/crates/shirabe/tests/all_functional_test.rs b/crates/shirabe/tests/all_functional_test.rs index 8578ed43..ad075ad6 100644 --- a/crates/shirabe/tests/all_functional_test.rs +++ b/crates/shirabe/tests/all_functional_test.rs @@ -144,11 +144,11 @@ fn expect_matches(expected: &str, output: &str) { let Some(m) = Preg::is_match3(php_regex!("{%(.+?)%}"), &expected[i..]) else { panic!("Failed to match %...% in {}", &expected[i..]); }; - let regex = m.get(&CaptureKey::ByIndex(1)).cloned().unwrap(); + let regex = m.get(&CaptureKey::ByIndex(1)).map(str::to_string).unwrap(); let pattern = format!("{{{}}}", regex); if let Some(m) = Preg::is_match3(&pattern, &output[j..]) { - let full = m.get(&CaptureKey::ByIndex(0)).cloned().unwrap(); + let full = m.get(&CaptureKey::ByIndex(0)).map(str::to_string).unwrap(); i += regex.len() + 2; j += full.len(); continue; |
