From dbdecaf5a1c54a876b7ee0153d58dd39b1080f97 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 23 May 2026 23:14:52 +0900 Subject: refactor(semver): change ConstraintInterface to a closed enum Replace the dyn ConstraintInterface trait objects with an AnyConstraint enum closing over its four implementors (Simple, Multi, MatchAll, MatchNone), mirroring the earlier Rule enum conversion. Rename constraint.rs to simple_constraint.rs to match the renamed Constraint type. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../shirabe/src/package/version/version_bumper.rs | 8 +++---- .../shirabe/src/package/version/version_parser.rs | 25 ++++++++++++++-------- .../src/package/version/version_selector.rs | 25 ++++++++++++---------- 3 files changed, 34 insertions(+), 24 deletions(-) (limited to 'crates/shirabe/src/package/version') diff --git a/crates/shirabe/src/package/version/version_bumper.rs b/crates/shirabe/src/package/version/version_bumper.rs index a9b5a67..8cc4b5d 100644 --- a/crates/shirabe/src/package/version/version_bumper.rs +++ b/crates/shirabe/src/package/version/version_bumper.rs @@ -8,7 +8,7 @@ use crate::util::Platform; use anyhow::Result; use indexmap::IndexMap; use shirabe_external_packages::composer::pcre::{CaptureKey, Preg}; -use shirabe_semver::constraint::ConstraintInterface; +use shirabe_semver::constraint::AnyConstraint; use shirabe_semver::intervals::Intervals; #[derive(Debug)] @@ -17,7 +17,7 @@ pub struct VersionBumper; impl VersionBumper { pub fn bump_requirement( &self, - constraint: &dyn ConstraintInterface, + constraint: &AnyConstraint, package: &dyn PackageInterface, ) -> Result { let parser = VersionParser::new(); @@ -114,8 +114,8 @@ impl VersionBumper { } let new_constraint = parser.parse_constraints(&modified)?; - if Intervals::is_subset_of(new_constraint.as_ref(), constraint)? - && Intervals::is_subset_of(constraint, new_constraint.as_ref())? + if Intervals::is_subset_of(&new_constraint, constraint)? + && Intervals::is_subset_of(constraint, &new_constraint)? { return Ok(pretty_constraint); } diff --git a/crates/shirabe/src/package/version/version_parser.rs b/crates/shirabe/src/package/version/version_parser.rs index 46a2356..1b588bc 100644 --- a/crates/shirabe/src/package/version/version_parser.rs +++ b/crates/shirabe/src/package/version/version_parser.rs @@ -1,16 +1,16 @@ //! ref: composer/src/Composer/Package/Version/VersionParser.php use indexmap::IndexMap; -use std::sync::{Arc, LazyLock, Mutex}; +use std::sync::{LazyLock, Mutex}; use shirabe_external_packages::composer::pcre::Preg; -use shirabe_semver::constraint::ConstraintInterface; +use shirabe_semver::constraint::AnyConstraint; use shirabe_semver::semver::Semver; use shirabe_semver::version_parser::VersionParser as SemverVersionParser; use crate::repository::PlatformRepository; -static CONSTRAINTS: LazyLock>>> = +static CONSTRAINTS: LazyLock>> = LazyLock::new(|| Mutex::new(IndexMap::new())); #[derive(Debug, Clone)] @@ -21,12 +21,19 @@ pub struct VersionParser { impl VersionParser { pub const DEFAULT_BRANCH_ALIAS: &'static str = "9999999-dev"; - pub fn parse_constraints( - &self, - constraints: &str, - ) -> anyhow::Result> { - // TODO(phase-b): re-introduce a memoization cache once trait objects are Send+Sync. - self.inner.parse_constraints(constraints) + pub fn parse_constraints(&self, constraints: &str) -> anyhow::Result { + { + let cache = CONSTRAINTS.lock().unwrap(); + if let Some(cached) = cache.get(constraints) { + return Ok(cached.clone()); + } + } + let parsed = self.inner.parse_constraints(constraints)?; + CONSTRAINTS + .lock() + .unwrap() + .insert(constraints.to_string(), parsed.clone()); + Ok(parsed) } pub fn parse_name_version_pairs( diff --git a/crates/shirabe/src/package/version/version_selector.rs b/crates/shirabe/src/package/version/version_selector.rs index 832f64c..3bd0191 100644 --- a/crates/shirabe/src/package/version/version_selector.rs +++ b/crates/shirabe/src/package/version/version_selector.rs @@ -8,8 +8,8 @@ use shirabe_external_packages::composer::pcre::Preg; use shirabe_php_shim::{ PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION, strtolower, version_compare, }; -use shirabe_semver::constraint::Constraint; -use shirabe_semver::constraint::ConstraintInterface; +use shirabe_semver::constraint::AnyConstraint; +use shirabe_semver::constraint::SimpleConstraint; use crate::filter::platform_requirement_filter::IgnoreAllPlatformRequirementFilter; use crate::filter::platform_requirement_filter::IgnoreListPlatformRequirementFilter; @@ -29,7 +29,7 @@ use crate::repository::RepositorySet; #[derive(Debug)] pub struct VersionSelector { repository_set: RepositorySet, - platform_constraints: IndexMap>>, + platform_constraints: IndexMap>, parser: Option, } @@ -38,16 +38,19 @@ impl VersionSelector { repository_set: RepositorySet, platform_repo: Option<&crate::repository::PlatformRepository>, ) -> anyhow::Result { - let mut platform_constraints: IndexMap>> = - IndexMap::new(); + let mut platform_constraints: IndexMap> = IndexMap::new(); if let Some(platform_repo) = platform_repo { for package in ::get_packages(platform_repo) { - let constraint = Constraint::new("==", package.get_version()); + let constraint = SimpleConstraint::new( + "==".to_string(), + package.get_version().to_string(), + None, + ); platform_constraints .entry(package.get_name().to_string()) .or_default() - .push(Box::new(constraint)); + .push(constraint.into()); } } Ok(Self { @@ -90,7 +93,7 @@ impl VersionSelector { }; let mut candidates = self.repository_set.find_packages( &strtolower(package_name), - constraint.as_ref().map(|c| c.clone_box()), + constraint.as_ref().map(|c| c.clone()), repo_set_flags, ); @@ -142,7 +145,7 @@ impl VersionSelector { let reason; if let Some(provided_constraints) = self.platform_constraints.get(name) { for provided_constraint in provided_constraints { - if link.get_constraint().matches(provided_constraint.as_ref()) { + if link.get_constraint().matches(provided_constraint) { continue 'reqs; } let list_filter_opt = platform_requirement_filter @@ -154,10 +157,10 @@ impl VersionSelector { if list_filter.is_upper_bound_ignored(name) { let filtered_constraint = list_filter.filter_constraint( name, - link.get_constraint().clone_box(), + link.get_constraint().clone(), false, )?; - if filtered_constraint.matches(provided_constraint.as_ref()) { + if filtered_constraint.matches(provided_constraint) { continue 'reqs; } } -- cgit v1.3.1