use crate::version::Version;
use std::cmp::Ordering;
const PHP_INT_MAX: &str = "9223372036854775807";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Bound {
version: String,
is_inclusive: bool,
}
impl Bound {
#[must_use]
pub fn new(version: String, is_inclusive: bool) -> Self {
Self {
version,
is_inclusive,
}
}
#[must_use]
pub fn zero() -> Self {
Self {
version: "0.0.0.0-dev".to_owned(),
is_inclusive: true,
}
}
#[must_use]
pub fn positive_infinity() -> Self {
Self {
version: format!("{PHP_INT_MAX}.0.0.0"),
is_inclusive: false,
}
}
#[must_use]
pub fn version(&self) -> &str {
&self.version
}
#[must_use]
pub fn is_inclusive(&self) -> bool {
self.is_inclusive
}
#[must_use]
pub fn is_zero(&self) -> bool {
self.version == "0.0.0.0-dev" && self.is_inclusive
}
#[must_use]
pub fn is_positive_infinity(&self) -> bool {
self.version == format!("{PHP_INT_MAX}.0.0.0") && !self.is_inclusive
}
#[must_use]
pub fn compare_to(&self, other: &Bound, gt: bool) -> bool {
if self == other {
return false;
}
match self.version_cmp(other) {
Ordering::Greater => gt,
Ordering::Less => !gt,
Ordering::Equal => {
if gt {
other.is_inclusive
} else {
!other.is_inclusive
}
}
}
}
#[must_use]
pub fn version_cmp(&self, other: &Bound) -> Ordering {
match (
Version::parse(&self.version),
Version::parse(&other.version),
) {
(Ok(a), Ok(b)) => a.cmp(&b),
_ => self.version.cmp(&other.version),
}
}
}