aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-semver/src/constraint/bound.rs
blob: 1a63fc3af74c70d64c30b2121f12d1eb53dadc98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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)
    }
}