diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-05-16 23:35:37 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-05-16 23:35:37 +0900 |
| commit | c1b57fb510961e02134df4883327155199d73087 (patch) | |
| tree | a9c00c3b6e55688b0f513aaf0d85dea9cebb6b6d /crates | |
| parent | ef1bdb737d73125ab71dd9ff5f0f135ce7aaba07 (diff) | |
| download | php-shirabe-c1b57fb510961e02134df4883327155199d73087.tar.gz php-shirabe-c1b57fb510961e02134df4883327155199d73087.tar.zst php-shirabe-c1b57fb510961e02134df4883327155199d73087.zip | |
feat(port): port Bound.php
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/shirabe-php-shim/src/lib.rs | 4 | ||||
| -rw-r--r-- | crates/shirabe-semver/src/constraint/bound.rs | 77 |
2 files changed, 81 insertions, 0 deletions
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<bool> { + 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) + } +} |
