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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use crate::sync;
use core::cmp;
use core::fmt;

#[derive(Copy, Clone, PartialEq, Eq)]
pub struct Version {
    pub major: u8,
    pub minor: u8,
    pub micro: u8
}

impl Version {
    pub const fn empty() -> Self {
        Self { major: 0, minor: 0, micro: 0 }
    }

    pub const fn new(major: u8, minor: u8, micro: u8) -> Self {
        Self { major, minor, micro }
    }
}

impl Ord for Version {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        if self.major < other.major {
            cmp::Ordering::Less
        }
        else if self.major == other.major {
            if self.minor < other.minor {
                cmp::Ordering::Less
            }
            else if self.minor == other.minor {
                if self.micro < other.micro {
                    cmp::Ordering::Less
                }
                else {
                    cmp::Ordering::Equal
                }
            }
            else {
                cmp::Ordering::Greater
            }
        }
        else {
            cmp::Ordering::Greater
        }
    }
}

impl PartialOrd for Version {
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl fmt::Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}.{}", self.major, self.minor, self.micro)
    }
}

pub struct VersionInterval {
    min: Option<Version>,
    max: Option<Version>
}

impl VersionInterval {
    pub const fn all() -> Self {
        Self {
            min: None,
            max: None
        }
    }

    pub const fn from(min: Version) -> Self {
        Self {
            min: Some(min),
            max: None
        }
    }

    pub const fn to(max: Version) -> Self {
        Self {
            min: None,
            max: Some(max)
        }
    }

    pub const fn from_to(min: Version, max: Version) -> Self {
        Self {
            min: Some(min),
            max: Some(max)
        }
    }

    pub fn matches(&self, ver: Version) -> bool {
        if let Some(min_v) = self.min {
            if ver < min_v {
                return false;
            }
        }
        if let Some(max_v) = self.max {
            if ver > max_v {
                return false;
            }
        }

        true
    }
}

static mut G_VERSION: sync::Locked<Version> = sync::Locked::new(false, Version::empty());

pub fn set_version(version: Version) {
    unsafe {
        G_VERSION.set(version);
    }
}

pub fn get_version() -> Version {
    unsafe {
        *G_VERSION.get()
    }
}