From c1b57fb510961e02134df4883327155199d73087 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 16 May 2026 23:35:37 +0900 Subject: feat(port): port Bound.php --- crates/shirabe-php-shim/src/lib.rs | 4 ++ crates/shirabe-semver/src/constraint/bound.rs | 77 +++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) (limited to 'crates') diff --git a/crates/shirabe-php-shim/src/lib.rs b/crates/shirabe-php-shim/src/lib.rs index 645909f..6dac434 100644 --- a/crates/shirabe-php-shim/src/lib.rs +++ b/crates/shirabe-php-shim/src/lib.rs @@ -370,6 +370,10 @@ pub fn version_compare(v1: &str, v2: &str, op: &str) -> bool { todo!() } +pub fn version_compare_2(v1: &str, v2: &str) -> i64 { + todo!() +} + pub fn microtime(get_as_float: bool) -> f64 { todo!() } diff --git a/crates/shirabe-semver/src/constraint/bound.rs b/crates/shirabe-semver/src/constraint/bound.rs index 6037f0d..1a63fc3 100644 --- a/crates/shirabe-semver/src/constraint/bound.rs +++ b/crates/shirabe-semver/src/constraint/bound.rs @@ -1 +1,78 @@ //! ref: composer/vendor/composer/semver/src/Constraint/Bound.php + +use anyhow::bail; + +use shirabe_php_shim as php; + +#[derive(Debug, PartialEq)] +pub struct Bound { + version: String, + is_inclusive: bool, +} + +impl Bound { + pub fn new(version: String, is_inclusive: bool) -> Self { + Self { + version, + is_inclusive, + } + } + + pub fn get_version(&self) -> &str { + &self.version + } + + pub fn is_inclusive(&self) -> bool { + self.is_inclusive + } + + pub fn is_zero(&self) -> bool { + self.get_version() == "0.0.0.0-dev" && self.is_inclusive() + } + + pub fn is_positive_infinity(&self) -> bool { + self.get_version() == format!("{}.0.0.0", i64::MAX) && !self.is_inclusive() + } + + pub fn compare_to(&self, other: &Bound, operator: &str) -> anyhow::Result { + if operator != "<" && operator != ">" { + bail!("Does not support any other operator other than > or <."); + } + + if self == other { + return Ok(false); + } + + let compare_result = php::version_compare_2(self.get_version(), other.get_version()); + + if compare_result != 0 { + return Ok((if operator == ">" { 1 } else { -1 }) == compare_result); + } + + Ok(if operator == ">" { + other.is_inclusive() + } else { + !other.is_inclusive() + }) + } + + pub fn __to_string(&self) -> String { + format!( + "{} [{}]", + self.get_version(), + if self.is_inclusive() { + "inclusive" + } else { + "exclusive" + } + ) + } + + pub fn zero() -> Self { + Bound::new("0.0.0.0-dev".to_string(), true) + } + + pub fn positive_infinity() -> Self { + Bound::new(format!("{}.0.0.0", i64::MAX), false) + } +} -- cgit v1.3.1