const VERSION_LUAU: u8 = 1 << 0;
const VERSION_LUA52: u8 = 1 << 1;
const VERSION_LUA53: u8 = 1 << 2;
const VERSION_LUA54: u8 = 1 << 3;
const VERSION_LUAJIT: u8 = 1 << 4;
const VERSION_CFXLUA: u8 = 1 << 5;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct LuaVersion {
bitfield: u8,
}
impl LuaVersion {
pub fn new() -> Self {
Self::default()
}
pub fn lua51() -> Self {
Self { bitfield: 0 }
}
#[cfg(feature = "luau")]
pub fn luau() -> Self {
Self {
bitfield: VERSION_LUAU,
}
}
#[cfg(feature = "luau")]
pub fn with_luau(self) -> Self {
Self {
bitfield: self.bitfield | VERSION_LUAU,
}
}
pub fn has_luau(self) -> bool {
cfg!(feature = "luau") && (self.bitfield & VERSION_LUAU != 0)
}
#[cfg(feature = "lua52")]
pub fn lua52() -> Self {
Self {
bitfield: VERSION_LUA52,
}
}
#[cfg(feature = "lua52")]
pub fn with_lua52(self) -> Self {
self | Self::lua52()
}
pub fn has_lua52(self) -> bool {
cfg!(feature = "lua52") && (self.bitfield & VERSION_LUA52 != 0)
}
#[cfg(feature = "lua53")]
pub fn lua53() -> Self {
Self {
bitfield: VERSION_LUA52 | VERSION_LUA53,
}
}
#[cfg(feature = "lua53")]
pub fn with_lua53(self) -> Self {
self | Self::lua53()
}
pub fn has_lua53(self) -> bool {
cfg!(feature = "lua53") && (self.bitfield & VERSION_LUA53 != 0)
}
#[cfg(feature = "lua54")]
pub fn lua54() -> Self {
Self {
bitfield: VERSION_LUA52 | VERSION_LUA53 | VERSION_LUA54,
}
}
#[cfg(feature = "lua54")]
pub fn with_lua54(self) -> Self {
self | Self::lua54()
}
pub fn has_lua54(self) -> bool {
cfg!(feature = "lua54") && (self.bitfield & VERSION_LUA54 != 0)
}
#[cfg(feature = "luajit")]
pub fn luajit() -> Self {
Self {
bitfield: VERSION_LUAJIT,
}
}
#[cfg(feature = "luajit")]
pub fn with_luajit(self) -> Self {
self | Self::luajit()
}
pub fn has_luajit(self) -> bool {
cfg!(feature = "luajit") && (self.bitfield & VERSION_LUAJIT != 0)
}
#[cfg(feature = "cfxlua")]
pub fn cfxlua() -> Self {
Self {
bitfield: VERSION_LUA52 | VERSION_LUA53 | VERSION_LUA54 | VERSION_CFXLUA,
}
}
#[cfg(feature = "cfxlua")]
pub fn with_cfxlua(self) -> Self {
self | Self::cfxlua()
}
pub fn has_cfxlua(self) -> bool {
cfg!(feature = "cfxlua") && (self.bitfield & VERSION_CFXLUA != 0)
}
}
impl Default for LuaVersion {
fn default() -> Self {
Self {
bitfield: VERSION_LUAU | VERSION_LUA52 | VERSION_LUA53 | VERSION_LUA54 | VERSION_LUAJIT,
}
}
}
impl std::ops::BitOr for LuaVersion {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
Self {
bitfield: self.bitfield | rhs.bitfield,
}
}
}
impl std::ops::BitOrAssign for LuaVersion {
fn bitor_assign(&mut self, rhs: Self) {
self.bitfield |= rhs.bitfield;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lua51_sanity() {
assert!(!LuaVersion::lua51().has_lua52());
assert!(!LuaVersion::lua51().has_lua53());
}
}