aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-semver/src/compiling_matcher.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-08 03:04:27 +0900
committernsfisis <nsfisis@gmail.com>2026-08-08 03:04:27 +0900
commitda0d38a8e16ebefd59ef5291b5788e6238cc78ba (patch)
treec6a4cb9f2444beeb84b2d30133053867acb46741 /crates/shirabe-semver/src/compiling_matcher.rs
parentc0879ab4a02b257cb0bd416dbdfa07784c8e3809 (diff)
downloadphp-shirabe-da0d38a8e16ebefd59ef5291b5788e6238cc78ba.tar.gz
php-shirabe-da0d38a8e16ebefd59ef5291b5788e6238cc78ba.tar.zst
php-shirabe-da0d38a8e16ebefd59ef5291b5788e6238cc78ba.zip
refactor(semver): replace Constraint's OP_*/STR_OP_* with CmpOp
PHP's version_compare takes its operator as a string, so the shim's port did too, and Constraint carried two families of operator constants plus translation tables to convert between the string form and its own int codes. Five copies of those tables had accumulated across Constraint, CompilingMatcher and the plugin value bridge. version_compare now takes a CmpOp, which makes an invalid operator unrepresentable and removes the tables' reason to exist. Constraint stores a CmpOp and keeps only the string parsing its constructor needs; getOperator, compile and CompilingMatcher::match speak CmpOp as well. PHP's OP_* numbering stays observable: a plugin reads the raw integer off the Constraint object over RPC, so get_operator_constant and its new inverse hold that 0..5 mapping. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-semver/src/compiling_matcher.rs')
-rw-r--r--crates/shirabe-semver/src/compiling_matcher.rs30
1 files changed, 11 insertions, 19 deletions
diff --git a/crates/shirabe-semver/src/compiling_matcher.rs b/crates/shirabe-semver/src/compiling_matcher.rs
index 142fd0d6..0d637037 100644
--- a/crates/shirabe-semver/src/compiling_matcher.rs
+++ b/crates/shirabe-semver/src/compiling_matcher.rs
@@ -3,25 +3,17 @@
use crate::constraint::AnyConstraint;
use crate::constraint::SimpleConstraint;
use indexmap::IndexMap;
+use shirabe_php_shim::CmpOp;
use std::sync::Mutex;
use std::sync::OnceLock;
+// Rust does not support eval(), so the compiled checker path is always disabled.
+// The COMPILED_CHECKER_CACHE is retained structurally but never populated.
static COMPILED_CHECKER_CACHE: OnceLock<
Mutex<IndexMap<String, Box<dyn Fn(String, bool) -> bool + Send + Sync>>>,
> = OnceLock::new();
static RESULT_CACHE: OnceLock<Mutex<IndexMap<String, bool>>> = OnceLock::new();
-// Rust does not support eval(), so the compiled checker path is always disabled.
-// The COMPILED_CHECKER_CACHE is retained structurally but never populated.
-static TRANS_OP_INT: &[(i64, &str)] = &[
- (SimpleConstraint::OP_EQ, SimpleConstraint::STR_OP_EQ),
- (SimpleConstraint::OP_LT, SimpleConstraint::STR_OP_LT),
- (SimpleConstraint::OP_LE, SimpleConstraint::STR_OP_LE),
- (SimpleConstraint::OP_GT, SimpleConstraint::STR_OP_GT),
- (SimpleConstraint::OP_GE, SimpleConstraint::STR_OP_GE),
- (SimpleConstraint::OP_NE, SimpleConstraint::STR_OP_NE),
-];
-
pub struct CompilingMatcher;
impl CompilingMatcher {
@@ -39,8 +31,13 @@ impl CompilingMatcher {
Self::compiled_checker_cache().lock().unwrap().clear();
}
- pub fn r#match(constraint: &AnyConstraint, operator: i64, version: String) -> bool {
- let result_cache_key = format!("{}{};{}", operator, constraint, version);
+ pub fn r#match(constraint: &AnyConstraint, operator: CmpOp, version: String) -> bool {
+ let result_cache_key = format!(
+ "{}{};{}",
+ SimpleConstraint::get_operator_constant(operator),
+ constraint,
+ version
+ );
{
let cache = Self::result_cache().lock().unwrap();
@@ -49,13 +46,8 @@ impl CompilingMatcher {
}
}
- let trans_op = TRANS_OP_INT
- .iter()
- .find(|(op, _)| *op == operator)
- .map(|(_, s)| *s)
- .expect("unknown operator");
let result =
- constraint.matches(&SimpleConstraint::new(trans_op.to_string(), version, None).into());
+ constraint.matches(&SimpleConstraint::new(operator.to_string(), version, None).into());
Self::result_cache()
.lock()