From 53f1fb395f33e0fb8db9aebd09ea9082f650f9f1 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 28 Jun 2026 17:45:06 +0900 Subject: refactor: add linter --- crates/shirabe-semver/src/compiling_matcher.rs | 8 +-- crates/shirabe-semver/src/constraint/bound.rs | 5 +- .../src/constraint/simple_constraint.rs | 37 +++++----- crates/shirabe-semver/src/intervals.rs | 20 +++--- crates/shirabe-semver/src/semver.rs | 3 +- crates/shirabe-semver/src/version_parser.rs | 78 +++++++++++++--------- 6 files changed, 82 insertions(+), 69 deletions(-) (limited to 'crates/shirabe-semver/src') diff --git a/crates/shirabe-semver/src/compiling_matcher.rs b/crates/shirabe-semver/src/compiling_matcher.rs index 1d980e7..142fd0d 100644 --- a/crates/shirabe-semver/src/compiling_matcher.rs +++ b/crates/shirabe-semver/src/compiling_matcher.rs @@ -1,12 +1,10 @@ //! ref: composer/vendor/composer/semver/src/CompilingMatcher.php -use std::sync::Mutex; -use std::sync::OnceLock; - -use indexmap::IndexMap; - use crate::constraint::AnyConstraint; use crate::constraint::SimpleConstraint; +use indexmap::IndexMap; +use std::sync::Mutex; +use std::sync::OnceLock; static COMPILED_CHECKER_CACHE: OnceLock< Mutex bool + Send + Sync>>>, diff --git a/crates/shirabe-semver/src/constraint/bound.rs b/crates/shirabe-semver/src/constraint/bound.rs index 8e45023..2590424 100644 --- a/crates/shirabe-semver/src/constraint/bound.rs +++ b/crates/shirabe-semver/src/constraint/bound.rs @@ -2,8 +2,6 @@ use anyhow::bail; -use shirabe_php_shim as php; - #[derive(Debug, Clone, PartialEq)] pub struct Bound { version: String, @@ -43,7 +41,8 @@ impl Bound { return Ok(false); } - let compare_result = php::version_compare_2(self.get_version(), other.get_version()); + let compare_result = + shirabe_php_shim::version_compare_2(self.get_version(), other.get_version()); if compare_result != 0 { return Ok((if operator == ">" { 1 } else { -1 }) == compare_result); diff --git a/crates/shirabe-semver/src/constraint/simple_constraint.rs b/crates/shirabe-semver/src/constraint/simple_constraint.rs index 161425a..5a85382 100644 --- a/crates/shirabe-semver/src/constraint/simple_constraint.rs +++ b/crates/shirabe-semver/src/constraint/simple_constraint.rs @@ -1,9 +1,7 @@ //! ref: composer/vendor/composer/semver/src/Constraint/Constraint.php -use anyhow::bail; -use shirabe_php_shim as php; - use crate::constraint::Bound; +use anyhow::bail; /// Corresponds to PHP's `Constraint`. #[derive(Debug, Clone)] @@ -120,24 +118,33 @@ impl SimpleConstraint { return Ok(false); } - Ok(php::version_compare(a, b, operator)) + Ok(shirabe_php_shim::version_compare(a, b, operator)) } pub fn compile_constraint(&self, other_operator: i64) -> String { if self.version.starts_with("dev-") { if Self::OP_EQ == self.operator { if Self::OP_EQ == other_operator { - return format!("$b && $v === {}", php::var_export_str(&self.version, true)); + return format!( + "$b && $v === {}", + shirabe_php_shim::var_export_str(&self.version, true) + ); } if Self::OP_NE == other_operator { - return format!("!$b || $v !== {}", php::var_export_str(&self.version, true)); + return format!( + "!$b || $v !== {}", + shirabe_php_shim::var_export_str(&self.version, true) + ); } return "false".to_string(); } if Self::OP_NE == self.operator { if Self::OP_EQ == other_operator { - return format!("!$b || $v !== {}", php::var_export_str(&self.version, true)); + return format!( + "!$b || $v !== {}", + shirabe_php_shim::var_export_str(&self.version, true) + ); } if Self::OP_NE == other_operator { return "true".to_string(); @@ -152,18 +159,18 @@ impl SimpleConstraint { if Self::OP_EQ == other_operator { return format!( "\\version_compare($v, {}, '==')", - php::var_export_str(&self.version, true) + shirabe_php_shim::var_export_str(&self.version, true) ); } if Self::OP_NE == other_operator { return format!( "$b || \\version_compare($v, {}, '!=')", - php::var_export_str(&self.version, true) + shirabe_php_shim::var_export_str(&self.version, true) ); } return format!( "!$b && \\version_compare({}, $v, '{}')", - php::var_export_str(&self.version, true), + shirabe_php_shim::var_export_str(&self.version, true), Self::trans_op_int(other_operator) ); } @@ -172,7 +179,7 @@ impl SimpleConstraint { if Self::OP_EQ == other_operator { return format!( "$b || (!$b && \\version_compare($v, {}, '!='))", - php::var_export_str(&self.version, true) + shirabe_php_shim::var_export_str(&self.version, true) ); } if Self::OP_NE == other_operator { @@ -195,14 +202,14 @@ impl SimpleConstraint { let code_comparison = format!( "\\version_compare($v, {}, '{}')", - php::var_export_str(&self.version, true), + shirabe_php_shim::var_export_str(&self.version, true), Self::trans_op_int(self.operator) ); if self.operator == Self::OP_LE && other_operator == Self::OP_GT { return format!( "!$b && \\version_compare($v, {}, '!=') && {}", - php::var_export_str(&self.version, true), + shirabe_php_shim::var_export_str(&self.version, true), code_comparison ); } @@ -210,7 +217,7 @@ impl SimpleConstraint { if self.operator == Self::OP_GE && other_operator == Self::OP_LT { return format!( "!$b && \\version_compare($v, {}, '!=') && {}", - php::var_export_str(&self.version, true), + shirabe_php_shim::var_export_str(&self.version, true), code_comparison ); } @@ -273,7 +280,7 @@ impl SimpleConstraint { { return !(Self::trans_op_int(provider.operator) == provider_no_equal_op && Self::trans_op_int(self.operator) != no_equal_op - && php::version_compare(&provider.version, &self.version, "==")); + && shirabe_php_shim::version_compare(&provider.version, &self.version, "==")); } false diff --git a/crates/shirabe-semver/src/intervals.rs b/crates/shirabe-semver/src/intervals.rs index 68b23bf..862b3ad 100644 --- a/crates/shirabe-semver/src/intervals.rs +++ b/crates/shirabe-semver/src/intervals.rs @@ -1,15 +1,13 @@ //! ref: composer/vendor/composer/semver/src/Intervals.php -use std::collections::HashMap; -use std::sync::{Mutex, OnceLock}; - use crate::constraint::AnyConstraint; use crate::constraint::MatchAllConstraint; use crate::constraint::MatchNoneConstraint; use crate::constraint::MultiConstraint; use crate::constraint::SimpleConstraint; use crate::interval::{DevConstraintSet, Interval}; -use shirabe_php_shim as php; +use indexmap::IndexMap; +use std::sync::{Mutex, OnceLock}; #[derive(Debug, Clone)] pub struct IntervalCollection { @@ -17,10 +15,10 @@ pub struct IntervalCollection { pub branches: DevConstraintSet, } -static INTERVALS_CACHE: OnceLock>> = OnceLock::new(); +static INTERVALS_CACHE: OnceLock>> = OnceLock::new(); -fn intervals_cache() -> &'static Mutex> { - INTERVALS_CACHE.get_or_init(|| Mutex::new(HashMap::new())) +fn intervals_cache() -> &'static Mutex> { + INTERVALS_CACHE.get_or_init(|| Mutex::new(IndexMap::new())) } fn op_sort_order(op: &str) -> i64 { @@ -38,7 +36,7 @@ pub struct Intervals; impl Intervals { pub fn clear() { - *intervals_cache().lock().unwrap() = HashMap::new(); + *intervals_cache().lock().unwrap() = IndexMap::new(); } pub fn is_subset_of( @@ -418,7 +416,7 @@ impl Intervals { branches }; - branches.names = php::array_unique(&branches.names); + branches.names = shirabe_php_shim::array_unique(&branches.names); if numeric_groups.len() == 1 { return Ok(IntervalCollection { @@ -445,7 +443,7 @@ impl Intervals { } borders.sort_by(|a, b| { - let order = php::version_compare_2(&a.0, &b.0); + let order = shirabe_php_shim::version_compare_2(&a.0, &b.0); if order == 0 { let diff = op_sort_order(&a.1) - op_sort_order(&b.1); diff.cmp(&0) @@ -479,7 +477,7 @@ impl Intervals { } else if start.is_some() && active_intervals < activation_threshold { let start_c = start.take().unwrap(); // filter out invalid intervals like > x - <= x, or >= x - < x - if php::version_compare(start_c.get_version(), version, "=") + if shirabe_php_shim::version_compare(start_c.get_version(), version, "=") && ((start_c.get_operator() == ">" && operator == "<=") || (start_c.get_operator() == ">=" && operator == "<")) { diff --git a/crates/shirabe-semver/src/semver.rs b/crates/shirabe-semver/src/semver.rs index 4700ac4..10814d4 100644 --- a/crates/shirabe-semver/src/semver.rs +++ b/crates/shirabe-semver/src/semver.rs @@ -1,10 +1,9 @@ //! ref: composer/vendor/composer/semver/src/Semver.php -use std::sync::OnceLock; - use crate::comparator::Comparator; use crate::constraint::SimpleConstraint; use crate::version_parser::VersionParser; +use std::sync::OnceLock; pub struct Semver; diff --git a/crates/shirabe-semver/src/version_parser.rs b/crates/shirabe-semver/src/version_parser.rs index 53ddbe8..ac3aec8 100644 --- a/crates/shirabe-semver/src/version_parser.rs +++ b/crates/shirabe-semver/src/version_parser.rs @@ -4,7 +4,6 @@ use crate::constraint::AnyConstraint; use crate::constraint::MatchAllConstraint; use crate::constraint::MultiConstraint; use crate::constraint::SimpleConstraint; -use shirabe_php_shim as php; // Regex to match pre-release data (sort of). // @@ -25,16 +24,16 @@ pub struct VersionParser; impl VersionParser { pub fn parse_stability(version: &str) -> String { - let version = php::preg_replace("{#.+$}", "", version); + let version = shirabe_php_shim::preg_replace("{#.+$}", "", version); if version.starts_with("dev-") || version.ends_with("-dev") { return "dev".to_string(); } let pattern = format!("{{{}(?:\\+.*)?$}}i", MODIFIER_REGEX); - let lower = php::strtolower(&version); + let lower = shirabe_php_shim::strtolower(&version); let mut match_: Vec> = Vec::new(); - php::preg_match(&pattern, &lower, &mut match_); + shirabe_php_shim::preg_match(&pattern, &lower, &mut match_); // match_[3] = the ([.-]?dev)? capture if match_ @@ -63,7 +62,7 @@ impl VersionParser { } pub fn normalize_stability(stability: &str) -> anyhow::Result { - let stability = php::strtolower(stability); + let stability = shirabe_php_shim::strtolower(stability); if !["stable", "rc", "beta", "alpha", "dev"].contains(&stability.as_str()) { anyhow::bail!( @@ -80,21 +79,22 @@ impl VersionParser { } pub fn normalize(&self, version: &str, full_version: Option<&str>) -> anyhow::Result { - let version = php::trim(version, None); + let version = shirabe_php_shim::trim(version, None); let orig_version = version.clone(); let full_version = full_version.unwrap_or(&version).to_string(); let mut version = version; // strip off aliasing let mut match_: Vec> = Vec::new(); - if php::preg_match("{^([^,\\s]++) ++as ++([^,\\s]++)$}", &version, &mut match_) { + if shirabe_php_shim::preg_match("{^([^,\\s]++) ++as ++([^,\\s]++)$}", &version, &mut match_) + { version = match_[1].clone().unwrap_or_default(); } // strip off stability flag let stab_pattern = format!("{{@(?:{})$}}i", STABILITIES_REGEX); let mut match_: Vec> = Vec::new(); - if php::preg_match(&stab_pattern, &version, &mut match_) { + if shirabe_php_shim::preg_match(&stab_pattern, &version, &mut match_) { let match0_len = match_[0].as_deref().unwrap_or("").len(); version = version[..version.len() - match0_len].to_string(); } @@ -112,7 +112,7 @@ impl VersionParser { // strip off build metadata let mut match_: Vec> = Vec::new(); - if php::preg_match("{^([^,\\s+]++)\\+[^\\s]++$}", &version, &mut match_) { + if shirabe_php_shim::preg_match("{^([^,\\s+]++)\\+[^\\s]++$}", &version, &mut match_) { version = match_[1].clone().unwrap_or_default(); } @@ -124,7 +124,7 @@ impl VersionParser { "{{^v?(\\d{{1,5}}+)(\\.\\d++)?(\\.\\d++)?(\\.\\d++)?{}$}}i", MODIFIER_REGEX ); - if php::preg_match(&classical_pattern, &version, &mut matches) { + if shirabe_php_shim::preg_match(&classical_pattern, &version, &mut matches) { let m2 = matches[2].as_deref().unwrap_or(""); let m3 = matches[3].as_deref().unwrap_or(""); let m4 = matches[4].as_deref().unwrap_or(""); @@ -142,8 +142,12 @@ impl VersionParser { "{{^v?(\\d{{4}}(?:[.:-]?\\d{{2}}){{1,6}}(?:[.:-]?\\d{{1,3}}){{0,2}}){}$}}i", MODIFIER_REGEX ); - if php::preg_match(&datetime_pattern, &version, &mut matches) { - version = php::preg_replace("{\\D}", ".", matches[1].as_deref().unwrap_or("")); + if shirabe_php_shim::preg_match(&datetime_pattern, &version, &mut matches) { + version = shirabe_php_shim::preg_replace( + "{\\D}", + ".", + matches[1].as_deref().unwrap_or(""), + ); index = Some(2); } } @@ -185,7 +189,7 @@ impl VersionParser { // match dev branches let mut match_: Vec> = Vec::new(); - if php::preg_match("{(.*?)[.-]?dev$}i", &version, &mut match_) { + if shirabe_php_shim::preg_match("{(.*?)[.-]?dev$}i", &version, &mut match_) { let branch_name = match_[1].clone().unwrap_or_default(); // a branch ending with -dev is only valid if it is numeric // if it gets prefixed with dev- it means the branch name should @@ -197,10 +201,10 @@ impl VersionParser { } } - let extra_message = if php::preg_match( + let extra_message = if shirabe_php_shim::preg_match( &format!( "{{ +as +{}(?:@(?:{}))?$}}", - php::preg_quote(&version, None), + shirabe_php_shim::preg_quote(&version, None), STABILITIES_REGEX ), &full_version, @@ -210,10 +214,10 @@ impl VersionParser { " in \"{}\", the alias must be an exact version", full_version ) - } else if php::preg_match( + } else if shirabe_php_shim::preg_match( &format!( "{{^{}(?:@(?:{}))? +as +}}", - php::preg_quote(&version, None), + shirabe_php_shim::preg_quote(&version, None), STABILITIES_REGEX ), &full_version, @@ -238,7 +242,7 @@ impl VersionParser { pub fn parse_numeric_alias_prefix(&self, branch: &str) -> Option { let mut matches: Vec> = Vec::new(); // matches['version'] == matches[1] ((?P...) is group 1) - if php::preg_match( + if shirabe_php_shim::preg_match( "{^(?P(\\d++\\.)*\\d++)(?:\\.x)?-dev$}i", branch, &mut matches, @@ -251,13 +255,13 @@ impl VersionParser { } pub fn normalize_branch(&self, name: &str) -> anyhow::Result { - let name = php::trim(name, None); + let name = shirabe_php_shim::trim(name, None); let mut matches: Vec> = Vec::new(); // Groups: 1=major, 2=".minor"(outer), 3=minor(inner), 4=".patch"(outer), // 5=patch(inner), 6=".fourth"(outer), 7=fourth(inner). // We use the outer groups [1,2,4,6] to replicate PHP's groups [1,2,3,4]. - if php::preg_match( + if shirabe_php_shim::preg_match( "{^v?(\\d++)(\\.(\\d++|[xX*]))?(\\.(\\d++|[xX*]))?(\\.(\\d++|[xX*]))?$}i", &name, &mut matches, @@ -292,7 +296,10 @@ impl VersionParser { pub fn parse_constraints(&self, constraints: &str) -> anyhow::Result { let pretty_constraint = constraints.to_string(); - let or_constraints = php::preg_split("{\\s*\\|\\|?\\s*}", &php::trim(constraints, None)); + let or_constraints = shirabe_php_shim::preg_split( + "{\\s*\\|\\|?\\s*}", + &shirabe_php_shim::trim(constraints, None), + ); let mut or_groups: Vec = Vec::new(); @@ -330,7 +337,7 @@ impl VersionParser { // strip off aliasing let mut match_: Vec> = Vec::new(); - if php::preg_match( + if shirabe_php_shim::preg_match( "{^([^,\\s]++) ++as ++([^,\\s]++)$}", &constraint, &mut match_, @@ -342,7 +349,7 @@ impl VersionParser { let mut stability_modifier: Option = None; let mut match_: Vec> = Vec::new(); let stab_pattern = format!("{{^([^,\\s]*?)@({})$}}i", STABILITIES_REGEX); - if php::preg_match(&stab_pattern, &constraint, &mut match_) { + if shirabe_php_shim::preg_match(&stab_pattern, &constraint, &mut match_) { let m1 = match_[1].as_deref().unwrap_or(""); constraint = if !m1.is_empty() { m1.to_string() @@ -357,7 +364,7 @@ impl VersionParser { // get rid of #refs as those are used by composer only let mut match_: Vec> = Vec::new(); - if php::preg_match( + if shirabe_php_shim::preg_match( "{^(dev-[^,\\s@]+?|[^,\\s@]+?\\.x-dev)#.+$}i", &constraint, &mut match_, @@ -366,7 +373,7 @@ impl VersionParser { } let mut match_: Vec> = Vec::new(); - if php::preg_match("{^(v)?[xX*](\\.[xX*])*$}i", &constraint, &mut match_) { + if shirabe_php_shim::preg_match("{^(v)?[xX*](\\.[xX*])*$}i", &constraint, &mut match_) { let m1_nonempty = !match_ .get(1) .and_then(|o| o.as_deref()) @@ -401,7 +408,7 @@ impl VersionParser { // the current version is used instead. let mut matches: Vec> = Vec::new(); let tilde_pattern = format!("{{^~>?{}$}}i", version_regex); - if php::preg_match(&tilde_pattern, &constraint, &mut matches) { + if shirabe_php_shim::preg_match(&tilde_pattern, &constraint, &mut matches) { if constraint.starts_with("~>") { anyhow::bail!( "Could not parse version constraint {}: Invalid operator \"~>\", you probably \ @@ -464,7 +471,7 @@ impl VersionParser { // and above, patch updates for versions 0.X >=0.1.0, and no updates for versions 0.0.X let mut matches: Vec> = Vec::new(); let caret_pattern = format!("{{^\\^{}($)}}i", version_regex); - if php::preg_match(&caret_pattern, &constraint, &mut matches) { + if shirabe_php_shim::preg_match(&caret_pattern, &constraint, &mut matches) { // Work out which position in the version we are operating at let m1 = matches[1].as_deref().unwrap_or(""); let m2 = matches[2].as_deref().unwrap_or(""); @@ -512,7 +519,7 @@ impl VersionParser { // [major, minor, patch] tuple. A partial version range is treated as an X-Range, so the // special character is in fact optional. let mut matches: Vec> = Vec::new(); - if php::preg_match( + if shirabe_php_shim::preg_match( "{^v?(\\d++)(?:\\.(\\d++))?(?:\\.(\\d++))?(?:\\.[xX*])++$}", &constraint, &mut matches, @@ -562,7 +569,7 @@ impl VersionParser { "{{^(?P{}) +- +(?P{})($)}}i", version_regex, version_regex ); - if php::preg_match(&hyphen_pattern, &constraint, &mut matches) { + if shirabe_php_shim::preg_match(&hyphen_pattern, &constraint, &mut matches) { // matches[1]='from' string, matches[2..9]=from captures, matches[10]='to' string, // matches[11..18]=to captures, matches[19]='($)' // matches[6]=from stability, matches[8]=from dev, matches[9]=from wildcard-dev @@ -628,7 +635,8 @@ impl VersionParser { // Basic Comparators let mut match_: Vec> = Vec::new(); - if php::preg_match("{^(<>|!=|>=?|<=?|==?)?\\s*(.*)}", &constraint, &mut match_) { + if shirabe_php_shim::preg_match("{^(<>|!=|>=?|<=?|==?)?\\s*(.*)}", &constraint, &mut match_) + { let version_str = match_[2].clone().unwrap_or_default(); let op_str = match_[1].clone().unwrap_or_default(); @@ -639,7 +647,11 @@ impl VersionParser { // dev-foobar except if the constraint uses a known operator, in which // case it must be a parse error if version_str.ends_with("-dev") - && php::preg_match("{^[0-9a-zA-Z-./]+$}", &version_str, &mut Vec::new()) + && shirabe_php_shim::preg_match( + "{^[0-9a-zA-Z-./]+$}", + &version_str, + &mut Vec::new(), + ) { self.normalize( &format!("dev-{}", &version_str[..version_str.len() - 4]), @@ -664,9 +676,9 @@ impl VersionParser { } if op == "<" || op == ">=" { let modifier_pattern = format!("{{-{}$}}", MODIFIER_REGEX); - if !php::preg_match( + if !shirabe_php_shim::preg_match( &modifier_pattern, - &php::strtolower(&version_str), + &shirabe_php_shim::strtolower(&version_str), &mut Vec::new(), ) && !version_str.starts_with("dev-") { -- cgit v1.3.1