aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-semver/src/interval.rs
blob: 237ad0bace4034dcf36ce00de042f4c8e38df5cf (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
//! ref: composer/vendor/composer/semver/src/Interval.php

use std::sync::OnceLock;

use crate::constraint::constraint::Constraint;

#[derive(Debug, Clone)]
pub struct DevConstraintSet {
    pub names: Vec<String>,
    pub exclude: bool,
}

#[derive(Debug, Clone)]
pub struct Interval {
    start: Constraint,
    end: Constraint,
}

impl Interval {
    pub fn new(start: Constraint, end: Constraint) -> Self {
        Self { start, end }
    }

    pub fn get_start(&self) -> &Constraint {
        &self.start
    }

    pub fn get_end(&self) -> &Constraint {
        &self.end
    }

    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()))
    }

    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)))
    }

    pub fn any() -> Self {
        Self::new(
            Self::from_zero().clone(),
            Self::until_positive_infinity().clone(),
        )
    }

    pub fn any_dev() -> DevConstraintSet {
        DevConstraintSet {
            names: vec![],
            exclude: true,
        }
    }

    pub fn no_dev() -> DevConstraintSet {
        DevConstraintSet {
            names: vec![],
            exclude: false,
        }
    }
}