From da0d38a8e16ebefd59ef5291b5788e6238cc78ba Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 8 Aug 2026 03:04:27 +0900 Subject: 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) --- crates/shirabe/src/plugin/php_plugin_value.rs | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) (limited to 'crates/shirabe/src/plugin/php_plugin_value.rs') diff --git a/crates/shirabe/src/plugin/php_plugin_value.rs b/crates/shirabe/src/plugin/php_plugin_value.rs index cdef2a79..a61d4873 100644 --- a/crates/shirabe/src/plugin/php_plugin_value.rs +++ b/crates/shirabe/src/plugin/php_plugin_value.rs @@ -68,20 +68,6 @@ fn required_string(context: &str, value: Option<&PluginValue>) -> Result Option<&'static str> { - Some(match code { - 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, - _ => return None, - }) -} - fn constraint_to_wire(constraint: &AnyConstraint) -> PluginValue { // Whether the pretty string was ever set is observable (`getPrettyString()` falls back to the // string form), so an unset one crosses as null rather than as an absent property. @@ -144,11 +130,12 @@ fn constraint_from_wire(value: &PluginValue) -> Result Ok(match object.class.as_str() { CONSTRAINT_CLASS => { let operator = match object.protected("operator") { - Some(PluginValue::Int(code)) => operator_from_code(*code).ok_or_else(|| { - throw(format!( - "a semver constraint has an unknown operator: {code}" - )) - })?, + Some(PluginValue::Int(code)) => SimpleConstraint::from_operator_constant(*code) + .ok_or_else(|| { + throw(format!( + "a semver constraint has an unknown operator: {code}" + )) + })?, other => { return Err(throw(format!( "a semver constraint operator is not an int, got {other:?}" -- cgit v1.3.1-4-g156e