From 8fb6de392bbac104c07a5cdbac12a4fd25ab1127 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 17 May 2026 04:56:37 +0900 Subject: fix(semver): resolve shirabe-semver compile errors - Replace RefCell with Mutex in Constraint for thread safety - Add clone_box() to ConstraintInterface for cloning trait objects - Propagate Result from Constraint::new() and unwrap at call sites - Fix VersionParser instantiation (unit struct, not fn) - Add indexmap dependency to shirabe-semver --- crates/shirabe-semver/src/constraint/constraint.rs | 44 +++++++++++++++------- .../src/constraint/constraint_interface.rs | 2 + .../src/constraint/match_all_constraint.rs | 6 +++ .../src/constraint/match_none_constraint.rs | 6 +++ .../src/constraint/multi_constraint.rs | 15 +++++++- 5 files changed, 58 insertions(+), 15 deletions(-) (limited to 'crates/shirabe-semver/src/constraint') diff --git a/crates/shirabe-semver/src/constraint/constraint.rs b/crates/shirabe-semver/src/constraint/constraint.rs index b0c937c..8db322d 100644 --- a/crates/shirabe-semver/src/constraint/constraint.rs +++ b/crates/shirabe-semver/src/constraint/constraint.rs @@ -1,6 +1,6 @@ //! ref: composer/vendor/composer/semver/src/Constraint/Constraint.php -use std::cell::RefCell; +use std::sync::Mutex; use anyhow::bail; use shirabe_php_shim as php; @@ -8,13 +8,13 @@ use shirabe_php_shim as php; use crate::constraint::bound::Bound; use crate::constraint::constraint_interface::ConstraintInterface; -#[derive(Debug, Clone)] +#[derive(Debug)] pub struct Constraint { pub(crate) operator: i64, pub(crate) version: String, pub(crate) pretty_string: Option, - pub(crate) lower_bound: RefCell>, - pub(crate) upper_bound: RefCell>, + pub(crate) lower_bound: Mutex>, + pub(crate) upper_bound: Mutex>, } impl Constraint { @@ -73,8 +73,8 @@ impl Constraint { operator: op_int, version, pretty_string: None, - lower_bound: RefCell::new(None), - upper_bound: RefCell::new(None), + lower_bound: Mutex::new(None), + upper_bound: Mutex::new(None), }) } @@ -284,13 +284,13 @@ impl Constraint { } fn extract_bounds(&self) { - if self.lower_bound.borrow().is_some() { + if self.lower_bound.lock().unwrap().is_some() { return; } if self.version.starts_with("dev-") { - *self.lower_bound.borrow_mut() = Some(Bound::zero()); - *self.upper_bound.borrow_mut() = Some(Bound::positive_infinity()); + *self.lower_bound.lock().unwrap() = Some(Bound::zero()); + *self.upper_bound.lock().unwrap() = Some(Bound::positive_infinity()); return; } @@ -313,8 +313,20 @@ impl Constraint { _ => panic!("unknown operator: {}", self.operator), }; - *self.lower_bound.borrow_mut() = Some(lower); - *self.upper_bound.borrow_mut() = Some(upper); + *self.lower_bound.lock().unwrap() = Some(lower); + *self.upper_bound.lock().unwrap() = Some(upper); + } +} + +impl Clone for Constraint { + fn clone(&self) -> Self { + Self { + operator: self.operator, + version: self.version.clone(), + pretty_string: self.pretty_string.clone(), + lower_bound: Mutex::new(self.lower_bound.lock().unwrap().clone()), + upper_bound: Mutex::new(self.upper_bound.lock().unwrap().clone()), + } } } @@ -350,7 +362,8 @@ impl ConstraintInterface for Constraint { fn get_lower_bound(&self) -> Bound { self.extract_bounds(); self.lower_bound - .borrow() + .lock() + .unwrap() .clone() .expect("extract_bounds should have set lower_bound") } @@ -358,11 +371,16 @@ impl ConstraintInterface for Constraint { fn get_upper_bound(&self) -> Bound { self.extract_bounds(); self.upper_bound - .borrow() + .lock() + .unwrap() .clone() .expect("extract_bounds should have set upper_bound") } + fn clone_box(&self) -> Box { + Box::new(self.clone()) + } + fn as_any(&self) -> &dyn std::any::Any { self } diff --git a/crates/shirabe-semver/src/constraint/constraint_interface.rs b/crates/shirabe-semver/src/constraint/constraint_interface.rs index 980a23a..f8879b3 100644 --- a/crates/shirabe-semver/src/constraint/constraint_interface.rs +++ b/crates/shirabe-semver/src/constraint/constraint_interface.rs @@ -22,5 +22,7 @@ pub trait ConstraintInterface { false } + fn clone_box(&self) -> Box; + fn as_any(&self) -> &dyn std::any::Any; } diff --git a/crates/shirabe-semver/src/constraint/match_all_constraint.rs b/crates/shirabe-semver/src/constraint/match_all_constraint.rs index 4d7b988..b6b2445 100644 --- a/crates/shirabe-semver/src/constraint/match_all_constraint.rs +++ b/crates/shirabe-semver/src/constraint/match_all_constraint.rs @@ -34,6 +34,12 @@ impl ConstraintInterface for MatchAllConstraint { "*".to_string() } + fn clone_box(&self) -> Box { + Box::new(MatchAllConstraint { + pretty_string: self.pretty_string.clone(), + }) + } + fn as_any(&self) -> &dyn std::any::Any { self } diff --git a/crates/shirabe-semver/src/constraint/match_none_constraint.rs b/crates/shirabe-semver/src/constraint/match_none_constraint.rs index 4add794..17a45bb 100644 --- a/crates/shirabe-semver/src/constraint/match_none_constraint.rs +++ b/crates/shirabe-semver/src/constraint/match_none_constraint.rs @@ -34,6 +34,12 @@ impl ConstraintInterface for MatchNoneConstraint { "[]".to_string() } + fn clone_box(&self) -> Box { + Box::new(MatchNoneConstraint { + pretty_string: self.pretty_string.clone(), + }) + } + fn as_any(&self) -> &dyn std::any::Any { self } diff --git a/crates/shirabe-semver/src/constraint/multi_constraint.rs b/crates/shirabe-semver/src/constraint/multi_constraint.rs index 86afa8d..29d6f5e 100644 --- a/crates/shirabe-semver/src/constraint/multi_constraint.rs +++ b/crates/shirabe-semver/src/constraint/multi_constraint.rs @@ -166,8 +166,8 @@ impl MultiConstraint { Some(Box::new( MultiConstraint::new( vec![ - l_mc.constraints[0].clone(), - r_mc.constraints[1].clone(), + l_mc.constraints[0].clone_box(), + r_mc.constraints[1].clone_box(), ], true, ) @@ -311,6 +311,17 @@ impl ConstraintInterface for MultiConstraint { !self.conjunctive } + fn clone_box(&self) -> Box { + Box::new(MultiConstraint { + constraints: self.constraints.iter().map(|c| c.clone_box()).collect(), + pretty_string: self.pretty_string.clone(), + string: RefCell::new(self.string.borrow().clone()), + conjunctive: self.conjunctive, + lower_bound: RefCell::new(self.lower_bound.borrow().clone()), + upper_bound: RefCell::new(self.upper_bound.borrow().clone()), + }) + } + fn as_any(&self) -> &dyn std::any::Any { self } -- cgit v1.3.1