aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-semver/src
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-05-19 00:10:22 +0900
committernsfisis <nsfisis@gmail.com>2026-05-19 00:11:03 +0900
commitc839244d8d09f3036ebfee8eef7eb6b147e593ab (patch)
treefe48c94f2c2e62468beef5ff1a8f3cff6adeef4f /crates/shirabe-semver/src
parent48839250146b217e2756ed3c0e624fd341b54d6c (diff)
downloadphp-shirabe-c839244d8d09f3036ebfee8eef7eb6b147e593ab.tar.gz
php-shirabe-c839244d8d09f3036ebfee8eef7eb6b147e593ab.tar.zst
php-shirabe-c839244d8d09f3036ebfee8eef7eb6b147e593ab.zip
fix(compile): fix various compile errors
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-semver/src')
-rw-r--r--crates/shirabe-semver/src/comparator.rs4
-rw-r--r--crates/shirabe-semver/src/compiling_matcher.rs2
-rw-r--r--crates/shirabe-semver/src/constraint/constraint.rs17
-rw-r--r--crates/shirabe-semver/src/constraint/constraint_interface.rs2
-rw-r--r--crates/shirabe-semver/src/constraint/multi_constraint.rs38
-rw-r--r--crates/shirabe-semver/src/interval.rs7
-rw-r--r--crates/shirabe-semver/src/intervals.rs39
-rw-r--r--crates/shirabe-semver/src/semver.rs3
-rw-r--r--crates/shirabe-semver/src/version_parser.rs30
9 files changed, 67 insertions, 75 deletions
diff --git a/crates/shirabe-semver/src/comparator.rs b/crates/shirabe-semver/src/comparator.rs
index 0ae6d94..575a656 100644
--- a/crates/shirabe-semver/src/comparator.rs
+++ b/crates/shirabe-semver/src/comparator.rs
@@ -30,7 +30,7 @@ impl Comparator {
}
pub fn compare(version1: String, operator: String, version2: String) -> bool {
- let constraint = Constraint::new(operator, version2).unwrap();
- constraint.match_specific(&Constraint::new("==".to_string(), version1).unwrap(), true)
+ let constraint = Constraint::new(operator, version2);
+ constraint.match_specific(&Constraint::new("==".to_string(), version1), true)
}
}
diff --git a/crates/shirabe-semver/src/compiling_matcher.rs b/crates/shirabe-semver/src/compiling_matcher.rs
index 942abe5..f6a3689 100644
--- a/crates/shirabe-semver/src/compiling_matcher.rs
+++ b/crates/shirabe-semver/src/compiling_matcher.rs
@@ -56,7 +56,7 @@ impl CompilingMatcher {
.find(|(op, _)| *op == operator)
.map(|(_, s)| *s)
.expect("unknown operator");
- let result = constraint.matches(&Constraint::new(trans_op.to_string(), version).unwrap());
+ let result = constraint.matches(&Constraint::new(trans_op.to_string(), version));
Self::result_cache()
.lock()
diff --git a/crates/shirabe-semver/src/constraint/constraint.rs b/crates/shirabe-semver/src/constraint/constraint.rs
index f8ff04e..1435139 100644
--- a/crates/shirabe-semver/src/constraint/constraint.rs
+++ b/crates/shirabe-semver/src/constraint/constraint.rs
@@ -60,22 +60,25 @@ impl Constraint {
}
}
- pub fn new(operator: String, version: String) -> anyhow::Result<Self> {
- let op_int = Self::trans_op_str(&operator).ok_or_else(|| {
- anyhow::anyhow!(
+ pub fn new(operator: impl Into<String>, version: impl Into<String>) -> Self {
+ let operator: String = operator.into();
+ let op_int = Self::trans_op_str(&operator).unwrap_or_else(|| {
+ // PHP raises InvalidArgumentException; in the Rust port keep that as a panic
+ // because invalid operators are programmer errors caught during porting.
+ panic!(
"Invalid operator \"{}\" given, expected one of: {}",
operator,
Self::get_supported_operators().join(", ")
)
- })?;
+ });
- Ok(Self {
+ Self {
operator: op_int,
- version,
+ version: version.into(),
pretty_string: None,
lower_bound: Mutex::new(None),
upper_bound: Mutex::new(None),
- })
+ }
}
pub fn get_version(&self) -> &str {
diff --git a/crates/shirabe-semver/src/constraint/constraint_interface.rs b/crates/shirabe-semver/src/constraint/constraint_interface.rs
index f8879b3..09536e6 100644
--- a/crates/shirabe-semver/src/constraint/constraint_interface.rs
+++ b/crates/shirabe-semver/src/constraint/constraint_interface.rs
@@ -2,7 +2,7 @@
use crate::constraint::bound::Bound;
-pub trait ConstraintInterface {
+pub trait ConstraintInterface: std::fmt::Debug {
fn matches(&self, provider: &dyn ConstraintInterface) -> bool;
fn compile(&self, other_operator: i64) -> String;
diff --git a/crates/shirabe-semver/src/constraint/multi_constraint.rs b/crates/shirabe-semver/src/constraint/multi_constraint.rs
index 04e262b..5596cc4 100644
--- a/crates/shirabe-semver/src/constraint/multi_constraint.rs
+++ b/crates/shirabe-semver/src/constraint/multi_constraint.rs
@@ -2,7 +2,6 @@
use std::cell::RefCell;
-use anyhow::bail;
use crate::constraint::bound::Bound;
use crate::constraint::constraint_interface::ConstraintInterface;
@@ -26,26 +25,22 @@ impl std::fmt::Debug for MultiConstraint {
}
impl MultiConstraint {
- pub fn new(
- constraints: Vec<Box<dyn ConstraintInterface>>,
- conjunctive: bool,
- ) -> anyhow::Result<Self> {
- if constraints.len() < 2 {
- bail!(
- "Must provide at least two constraints for a MultiConstraint. Use \
+ pub fn new(constraints: Vec<Box<dyn ConstraintInterface>>, conjunctive: bool) -> Self {
+ assert!(
+ constraints.len() >= 2,
+ "Must provide at least two constraints for a MultiConstraint. Use \
the regular Constraint class for one constraint only or MatchAllConstraint for none. You may use \
MultiConstraint::create() which optimizes and handles those cases automatically."
- );
- }
+ );
- Ok(Self {
+ Self {
constraints,
pretty_string: None,
string: RefCell::new(None),
conjunctive,
lower_bound: RefCell::new(None),
upper_bound: RefCell::new(None),
- })
+ }
}
pub fn get_constraints(&self) -> &[Box<dyn ConstraintInterface>] {
@@ -123,7 +118,7 @@ impl MultiConstraint {
return Ok(constraints.into_iter().next().unwrap());
}
- Ok(Box::new(MultiConstraint::new(constraints, conjunctive)?))
+ Ok(Box::new(MultiConstraint::new(constraints, conjunctive)))
}
// Returns the (possibly optimized) constraints and the effective conjunctive flag.
@@ -163,16 +158,13 @@ impl MultiConstraint {
&& right1.starts_with('<')
&& left1.get(2..) == right0.get(3..)
{
- Some(Box::new(
- MultiConstraint::new(
- vec![
- l_mc.constraints[0].clone_box(),
- r_mc.constraints[1].clone_box(),
- ],
- true,
- )
- .unwrap(),
- )
+ Some(Box::new(MultiConstraint::new(
+ vec![
+ l_mc.constraints[0].clone_box(),
+ r_mc.constraints[1].clone_box(),
+ ],
+ true,
+ ))
as Box<dyn ConstraintInterface>)
} else {
None
diff --git a/crates/shirabe-semver/src/interval.rs b/crates/shirabe-semver/src/interval.rs
index cf4f481..237ad0b 100644
--- a/crates/shirabe-semver/src/interval.rs
+++ b/crates/shirabe-semver/src/interval.rs
@@ -31,14 +31,13 @@ impl Interval {
pub fn from_zero() -> &'static Constraint {
static ZERO: OnceLock<Constraint> = OnceLock::new();
- ZERO.get_or_init(|| Constraint::new(">=".to_string(), "0.0.0.0-dev".to_string()).unwrap())
+ ZERO.get_or_init(|| Constraint::new(">=".to_string(), "0.0.0.0-dev".to_string()))
}
pub fn until_positive_infinity() -> &'static Constraint {
static POSITIVE_INFINITY: OnceLock<Constraint> = OnceLock::new();
- POSITIVE_INFINITY.get_or_init(|| {
- Constraint::new("<".to_string(), format!("{}.0.0.0", i64::MAX)).unwrap()
- })
+ POSITIVE_INFINITY
+ .get_or_init(|| Constraint::new("<".to_string(), format!("{}.0.0.0", i64::MAX)))
}
pub fn any() -> Self {
diff --git a/crates/shirabe-semver/src/intervals.rs b/crates/shirabe-semver/src/intervals.rs
index 898cab3..384cb4e 100644
--- a/crates/shirabe-semver/src/intervals.rs
+++ b/crates/shirabe-semver/src/intervals.rs
@@ -66,8 +66,7 @@ impl Intervals {
}
// Phase B: ConstraintInterface needs clone_box() to create owned copies from references.
- let multi =
- MultiConstraint::new(vec![candidate.clone_box(), constraint.clone_box()], true)?;
+ let multi = MultiConstraint::new(vec![candidate.clone_box(), constraint.clone_box()], true);
let intersection_intervals = Self::get(&multi)?;
let candidate_intervals = Self::get(candidate)?;
@@ -125,7 +124,7 @@ impl Intervals {
}
// Phase B: ConstraintInterface needs clone_box().
- let multi = MultiConstraint::new(vec![a.clone_box(), b.clone_box()], true)?;
+ let multi = MultiConstraint::new(vec![a.clone_box(), b.clone_box()], true);
let intersection_intervals = Self::generate_intervals(&multi, true)?;
Ok(!intersection_intervals.numeric.is_empty()
@@ -185,7 +184,7 @@ impl Intervals {
un_equal_constraints.push(Box::new(Constraint::new(
"!=".to_string(),
interval.get_end().get_version().to_string(),
- )?));
+ )));
i += 1;
continue;
}
@@ -202,7 +201,7 @@ impl Intervals {
// count is 1 if entire constraint is just one != expression
if un_equal_constraints.len() > 1 {
constraints
- .push(Box::new(MultiConstraint::new(un_equal_constraints, true)?));
+ .push(Box::new(MultiConstraint::new(un_equal_constraints, true)));
} else {
constraints.push(un_equal_constraints.into_iter().next().unwrap());
}
@@ -220,7 +219,7 @@ impl Intervals {
constraints.push(Box::new(Constraint::new(
"==".to_string(),
interval.get_start().get_version().to_string(),
- )?));
+ )));
i += 1;
continue;
}
@@ -238,7 +237,7 @@ impl Intervals {
Box::new(interval.get_end().clone()),
],
true,
- )?));
+ )));
}
i += 1;
@@ -260,12 +259,12 @@ impl Intervals {
dev_constraints.push(Box::new(Constraint::new(
"!=".to_string(),
branch_name.clone(),
- )?));
+ )));
} else {
dev_constraints.push(Box::new(Constraint::new(
"==".to_string(),
branch_name.clone(),
- )?));
+ )));
}
}
@@ -274,25 +273,25 @@ impl Intervals {
if intervals.branches.exclude {
if constraints.len() > 1 {
let merged: Vec<Box<dyn ConstraintInterface>> =
- std::iter::once(Box::new(MultiConstraint::new(constraints, false)?)
+ std::iter::once(Box::new(MultiConstraint::new(constraints, false))
as Box<dyn ConstraintInterface>)
.chain(dev_constraints)
.collect();
- return Ok(Box::new(MultiConstraint::new(merged, true)?));
+ return Ok(Box::new(MultiConstraint::new(merged, true)));
}
if constraints.len() == 1
&& constraints[0].__to_string() == Interval::from_zero().__to_string()
{
if dev_constraints.len() > 1 {
- return Ok(Box::new(MultiConstraint::new(dev_constraints, true)?));
+ return Ok(Box::new(MultiConstraint::new(dev_constraints, true)));
}
return Ok(dev_constraints.into_iter().next().unwrap());
}
let merged: Vec<Box<dyn ConstraintInterface>> =
constraints.into_iter().chain(dev_constraints).collect();
- return Ok(Box::new(MultiConstraint::new(merged, true)?));
+ return Ok(Box::new(MultiConstraint::new(merged, true)));
}
// otherwise devConstraints contains a list of == operators for branches which are
@@ -301,7 +300,7 @@ impl Intervals {
}
if constraints.len() > 1 {
- return Ok(Box::new(MultiConstraint::new(constraints, false)?));
+ return Ok(Box::new(MultiConstraint::new(constraints, false)));
}
if constraints.len() == 1 {
@@ -505,7 +504,7 @@ impl Intervals {
}
if start.is_none() && active_intervals >= activation_threshold {
- start = Some(Constraint::new(operator.clone(), version.clone())?);
+ start = Some(Constraint::new(operator.clone(), version.clone()));
} 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
@@ -517,7 +516,7 @@ impl Intervals {
} else {
intervals.push(Interval::new(
start_c,
- Constraint::new(operator.clone(), version.clone())?,
+ Constraint::new(operator.clone(), version.clone()),
));
if stop_on_first_valid_interval {
@@ -590,10 +589,10 @@ impl Intervals {
numeric: vec![
Interval::new(
Interval::from_zero().clone(),
- Constraint::new("<".to_string(), constraint.get_version().to_string())?,
+ Constraint::new("<".to_string(), constraint.get_version().to_string()),
),
Interval::new(
- Constraint::new(">".to_string(), constraint.get_version().to_string())?,
+ Constraint::new(">".to_string(), constraint.get_version().to_string()),
Interval::until_positive_infinity().clone(),
),
],
@@ -604,8 +603,8 @@ impl Intervals {
// convert ==x to an interval of >=x - <=x
Ok(IntervalCollection {
numeric: vec![Interval::new(
- Constraint::new(">=".to_string(), constraint.get_version().to_string())?,
- Constraint::new("<=".to_string(), constraint.get_version().to_string())?,
+ Constraint::new(">=".to_string(), constraint.get_version().to_string()),
+ Constraint::new("<=".to_string(), constraint.get_version().to_string()),
)],
branches: Interval::no_dev(),
})
diff --git a/crates/shirabe-semver/src/semver.rs b/crates/shirabe-semver/src/semver.rs
index 0aff3d7..28e74a6 100644
--- a/crates/shirabe-semver/src/semver.rs
+++ b/crates/shirabe-semver/src/semver.rs
@@ -19,8 +19,7 @@ impl Semver {
pub fn satisfies(version: String, constraints: String) -> anyhow::Result<bool> {
let version_parser = Self::version_parser();
- let provider =
- Constraint::new("==".to_string(), version_parser.normalize(&version, None)?)?;
+ let provider = Constraint::new("==".to_string(), version_parser.normalize(&version, None)?);
let parsed_constraints = version_parser.parse_constraints(&constraints)?;
Ok(parsed_constraints.matches(&provider))
}
diff --git a/crates/shirabe-semver/src/version_parser.rs b/crates/shirabe-semver/src/version_parser.rs
index 2c3bcc9..359beec 100644
--- a/crates/shirabe-semver/src/version_parser.rs
+++ b/crates/shirabe-semver/src/version_parser.rs
@@ -328,7 +328,7 @@ impl VersionParser {
let constraint: Box<dyn ConstraintInterface> = if constraint_objects.len() == 1 {
constraint_objects.into_iter().next().unwrap()
} else {
- Box::new(MultiConstraint::new(constraint_objects, true)?)
+ Box::new(MultiConstraint::new(constraint_objects, true))
};
or_groups.push(constraint);
@@ -402,7 +402,7 @@ impl VersionParser {
return Ok(vec![Box::new(Constraint::new(
">=".to_string(),
"0.0.0.0-dev".to_string(),
- )?)]);
+ ))]);
}
return Ok(vec![Box::new(MatchAllConstraint {
@@ -461,7 +461,7 @@ impl VersionParser {
let low_version =
self.normalize(&format!("{}{}", &constraint[1..], stability_suffix), None)?;
- let lower_bound = Constraint::new(">=".to_string(), low_version)?;
+ let lower_bound = Constraint::new(">=".to_string(), low_version);
// For upper bound, we increment the position of one more significance,
// but highPosition = 0 would be illegal
@@ -471,7 +471,7 @@ impl VersionParser {
self.manipulate_version_string(&matches, high_position, 1, "0")
.unwrap_or_default()
);
- let upper_bound = Constraint::new("<".to_string(), high_version)?;
+ let upper_bound = Constraint::new("<".to_string(), high_version);
return Ok(vec![Box::new(lower_bound), Box::new(upper_bound)]);
}
@@ -508,7 +508,7 @@ impl VersionParser {
let low_version =
self.normalize(&format!("{}{}", &constraint[1..], stability_suffix), None)?;
- let lower_bound = Constraint::new(">=".to_string(), low_version)?;
+ let lower_bound = Constraint::new(">=".to_string(), low_version);
// For upper bound, we increment the position of one more significance,
// but highPosition = 0 would be illegal
@@ -517,7 +517,7 @@ impl VersionParser {
self.manipulate_version_string(&matches, position, 1, "0")
.unwrap_or_default()
);
- let upper_bound = Constraint::new("<".to_string(), high_version)?;
+ let upper_bound = Constraint::new("<".to_string(), high_version);
return Ok(vec![Box::new(lower_bound), Box::new(upper_bound)]);
}
@@ -557,12 +557,12 @@ impl VersionParser {
return Ok(vec![Box::new(Constraint::new(
"<".to_string(),
high_version,
- )?)]);
+ ))]);
}
return Ok(vec![
- Box::new(Constraint::new(">=".to_string(), low_version)?),
- Box::new(Constraint::new("<".to_string(), high_version)?),
+ Box::new(Constraint::new(">=".to_string(), low_version)),
+ Box::new(Constraint::new("<".to_string(), high_version)),
]);
}
@@ -596,7 +596,7 @@ impl VersionParser {
let lower_bound = Constraint::new(
">=".to_string(),
format!("{}{}", low_version, low_stability_suffix),
- )?;
+ );
// PHP's empty() on "0" returns true, but here we only check for truly empty/missing
let empty = |x: &Option<String>| -> bool { x.as_deref().is_none_or(|s| s.is_empty()) };
@@ -610,7 +610,7 @@ impl VersionParser {
{
let to_str = matches[10].clone().unwrap_or_default(); // matches['to']
let hv = self.normalize(&to_str, None)?;
- Constraint::new("<=".to_string(), hv)?
+ Constraint::new("<=".to_string(), hv)
} else {
// matches[11]=to major, matches[12]=to minor, matches[13]=to patch,
// matches[14]=to fourth
@@ -632,7 +632,7 @@ impl VersionParser {
self.manipulate_version_string(&high_match, position, 1, "0")
.unwrap_or_default()
);
- Constraint::new("<".to_string(), hv)?
+ Constraint::new("<".to_string(), hv)
};
return Ok(vec![Box::new(lower_bound), Box::new(upper_bound)]);
@@ -644,7 +644,7 @@ impl VersionParser {
let version_str = match_[2].clone().unwrap_or_default();
let op_str = match_[1].clone().unwrap_or_default();
- let version_result: anyhow::Result<String> = (match self.normalize(&version_str, None) {
+ let version_result: anyhow::Result<String> = match self.normalize(&version_str, None) {
Ok(v) => Ok(v),
Err(e) => {
// recover from an invalid constraint like foobar-dev which should be
@@ -661,7 +661,7 @@ impl VersionParser {
Err(e)
}
}
- });
+ };
if let Ok(mut version) = version_result {
let op = if op_str.is_empty() { "=" } else { &op_str };
@@ -691,7 +691,7 @@ impl VersionParser {
} else {
op_str
};
- return Ok(vec![Box::new(Constraint::new(final_op, version)?)]);
+ return Ok(vec![Box::new(Constraint::new(final_op, version))]);
}
}