StandardLibrary trait

This commit is contained in:
highflowey 2024-08-23 00:50:05 +03:30
parent 3f709f1e96
commit 2b7bb89037

View file

@ -2,6 +2,17 @@ use std::str::FromStr;
use mlua::prelude::*; use mlua::prelude::*;
pub trait StandardLibrary
where
Self: Sized + 'static + FromStr,
{
const ALL: &'static [Self];
fn name(&self) -> &'static str;
fn module<'lua>(&self, lua: &'lua Lua) -> LuaResult<LuaMultiValue<'lua>>;
}
/** /**
A standard library provided by Lune. A standard library provided by Lune.
*/ */
@ -20,12 +31,12 @@ pub enum LuneStandardLibrary {
#[cfg(feature = "roblox")] Roblox, #[cfg(feature = "roblox")] Roblox,
} }
impl LuneStandardLibrary { impl StandardLibrary for LuneStandardLibrary {
/** /**
All available standard libraries. All available standard libraries.
*/ */
#[rustfmt::skip] #[rustfmt::skip]
pub const ALL: &'static [Self] = &[ const ALL: &'static [Self] = &[
#[cfg(feature = "datetime")] Self::DateTime, #[cfg(feature = "datetime")] Self::DateTime,
#[cfg(feature = "fs")] Self::Fs, #[cfg(feature = "fs")] Self::Fs,
#[cfg(feature = "luau")] Self::Luau, #[cfg(feature = "luau")] Self::Luau,
@ -44,7 +55,7 @@ impl LuneStandardLibrary {
#[must_use] #[must_use]
#[rustfmt::skip] #[rustfmt::skip]
#[allow(unreachable_patterns)] #[allow(unreachable_patterns)]
pub fn name(&self) -> &'static str { fn name(&self) -> &'static str {
match self { match self {
#[cfg(feature = "datetime")] Self::DateTime => "datetime", #[cfg(feature = "datetime")] Self::DateTime => "datetime",
#[cfg(feature = "fs")] Self::Fs => "fs", #[cfg(feature = "fs")] Self::Fs => "fs",
@ -70,7 +81,7 @@ impl LuneStandardLibrary {
*/ */
#[rustfmt::skip] #[rustfmt::skip]
#[allow(unreachable_patterns)] #[allow(unreachable_patterns)]
pub fn module<'lua>(&self, lua: &'lua Lua) -> LuaResult<LuaMultiValue<'lua>> { fn module<'lua>(&self, lua: &'lua Lua) -> LuaResult<LuaMultiValue<'lua>> {
let res: LuaResult<LuaTable> = match self { let res: LuaResult<LuaTable> = match self {
#[cfg(feature = "datetime")] Self::DateTime => lune_std_datetime::module(lua), #[cfg(feature = "datetime")] Self::DateTime => lune_std_datetime::module(lua),
#[cfg(feature = "fs")] Self::Fs => lune_std_fs::module(lua), #[cfg(feature = "fs")] Self::Fs => lune_std_fs::module(lua),