emmylua_parser/kind/
lua_version.rs

1use std::fmt;
2
3#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4pub struct LuaVersionNumber {
5    pub major: u32,
6    pub minor: u32,
7    pub patch: u32,
8}
9
10impl LuaVersionNumber {
11    #[allow(unused)]
12    pub fn new(major: u32, minor: u32, patch: u32) -> Self {
13        Self {
14            major,
15            minor,
16            patch,
17        }
18    }
19
20    #[allow(unused)]
21    pub const LUA_JIT: Self = Self {
22        major: 2,
23        minor: 0,
24        patch: 0,
25    };
26
27    pub fn from_str(s: &str) -> Option<Self> {
28        if s == "JIT" {
29            return Some(Self::LUA_JIT);
30        }
31
32        let mut iter = s.split('.').map(|it| it.parse::<u32>().unwrap_or(0));
33        let major = iter.next().unwrap_or(0);
34        let minor = iter.next().unwrap_or(0);
35        let patch = iter.next().unwrap_or(0);
36        Some(Self {
37            major,
38            minor,
39            patch,
40        })
41    }
42}
43
44impl PartialOrd for LuaVersionNumber {
45    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
46        Some(self.cmp(other))
47    }
48}
49
50impl Ord for LuaVersionNumber {
51    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
52        self.major
53            .cmp(&other.major)
54            .then_with(|| self.minor.cmp(&other.minor))
55            .then_with(|| self.patch.cmp(&other.patch))
56    }
57}
58
59impl fmt::Display for LuaVersionNumber {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        match *self {
62            LuaVersionNumber::LUA_JIT => write!(f, "Lua JIT"),
63            LuaVersionNumber { major, minor, .. } => write!(f, "Lua {}.{}", major, minor),
64        }
65    }
66}
67
68#[allow(unused)]
69#[derive(Debug, Clone, PartialEq, Eq, Hash)]
70pub enum LuaVersionCondition {
71    Eq(LuaVersionNumber),
72    Gte(LuaVersionNumber),
73    Lte(LuaVersionNumber),
74}
75
76#[allow(unused)]
77impl LuaVersionCondition {
78    pub fn check(&self, version: &LuaVersionNumber) -> bool {
79        match self {
80            LuaVersionCondition::Eq(v) => version == v,
81            LuaVersionCondition::Gte(v) => version >= v,
82            LuaVersionCondition::Lte(v) => version <= v,
83        }
84    }
85}
86
87impl fmt::Display for LuaVersionCondition {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        match self {
90            LuaVersionCondition::Eq(v) => write!(f, "{}", v),
91            LuaVersionCondition::Gte(v) => write!(f, ">= {}", v),
92            LuaVersionCondition::Lte(v) => write!(f, "<= {}", v),
93        }
94    }
95}