From 3b3fa65bfe2d8f8ad37a00a73075a62bf976758c Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sun, 21 Apr 2024 19:34:37 +0200 Subject: [PATCH] Create full enum for lune standard libraries --- Cargo.lock | 33 ++++++++ crates/lune-std-datetime/Cargo.toml | 5 ++ crates/lune-std-datetime/src/lib.rs | 15 ++++ crates/lune-std-fs/Cargo.toml | 5 ++ crates/lune-std-fs/src/lib.rs | 15 ++++ crates/lune-std-luau/Cargo.toml | 5 ++ crates/lune-std-luau/src/lib.rs | 15 ++++ crates/lune-std-net/Cargo.toml | 5 ++ crates/lune-std-net/src/lib.rs | 15 ++++ crates/lune-std-process/Cargo.toml | 5 ++ crates/lune-std-process/src/lib.rs | 15 ++++ crates/lune-std-regex/Cargo.toml | 5 ++ crates/lune-std-regex/src/lib.rs | 15 ++++ crates/lune-std-roblox/Cargo.toml | 5 ++ crates/lune-std-roblox/src/lib.rs | 15 ++++ crates/lune-std-serde/Cargo.toml | 5 ++ crates/lune-std-serde/src/lib.rs | 15 ++++ crates/lune-std/Cargo.toml | 2 + crates/lune-std/src/lib.rs | 4 + crates/lune-std/src/library.rs | 121 ++++++++++++++++++++++++++++ 20 files changed, 320 insertions(+) create mode 100644 crates/lune-std/src/library.rs diff --git a/Cargo.lock b/Cargo.lock index 8e585e0..bec3b23 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1551,39 +1551,72 @@ dependencies = [ "lune-std-serde", "lune-std-stdio", "lune-std-task", + "mlua", ] [[package]] name = "lune-std-datetime" version = "0.8.3" +dependencies = [ + "lune-utils", + "mlua", +] [[package]] name = "lune-std-fs" version = "0.8.3" +dependencies = [ + "lune-utils", + "mlua", +] [[package]] name = "lune-std-luau" version = "0.8.3" +dependencies = [ + "lune-utils", + "mlua", +] [[package]] name = "lune-std-net" version = "0.8.3" +dependencies = [ + "lune-utils", + "mlua", +] [[package]] name = "lune-std-process" version = "0.8.3" +dependencies = [ + "lune-utils", + "mlua", +] [[package]] name = "lune-std-regex" version = "0.8.3" +dependencies = [ + "lune-utils", + "mlua", +] [[package]] name = "lune-std-roblox" version = "0.8.3" +dependencies = [ + "lune-utils", + "mlua", +] [[package]] name = "lune-std-serde" version = "0.8.3" +dependencies = [ + "lune-utils", + "mlua", +] [[package]] name = "lune-std-stdio" diff --git a/crates/lune-std-datetime/Cargo.toml b/crates/lune-std-datetime/Cargo.toml index cc1f85e..043853b 100644 --- a/crates/lune-std-datetime/Cargo.toml +++ b/crates/lune-std-datetime/Cargo.toml @@ -9,3 +9,8 @@ path = "src/lib.rs" [lints] workspace = true + +[dependencies] +mlua = "0.9.7" + +lune-utils = { version = "0.8.3", path = "../lune-utils" } diff --git a/crates/lune-std-datetime/src/lib.rs b/crates/lune-std-datetime/src/lib.rs index 2e802e7..5a1dfae 100644 --- a/crates/lune-std-datetime/src/lib.rs +++ b/crates/lune-std-datetime/src/lib.rs @@ -1 +1,16 @@ #![allow(clippy::cargo_common_metadata)] + +use mlua::prelude::*; + +use lune_utils::TableBuilder; + +/** + Creates the `datetime` standard library module. + + # Errors + + Errors when out of memory. +*/ +pub fn module(lua: &Lua) -> LuaResult { + TableBuilder::new(lua)?.build_readonly() +} diff --git a/crates/lune-std-fs/Cargo.toml b/crates/lune-std-fs/Cargo.toml index 17a0996..cbb2fcc 100644 --- a/crates/lune-std-fs/Cargo.toml +++ b/crates/lune-std-fs/Cargo.toml @@ -9,3 +9,8 @@ path = "src/lib.rs" [lints] workspace = true + +[dependencies] +mlua = "0.9.7" + +lune-utils = { version = "0.8.3", path = "../lune-utils" } diff --git a/crates/lune-std-fs/src/lib.rs b/crates/lune-std-fs/src/lib.rs index 2e802e7..81a1507 100644 --- a/crates/lune-std-fs/src/lib.rs +++ b/crates/lune-std-fs/src/lib.rs @@ -1 +1,16 @@ #![allow(clippy::cargo_common_metadata)] + +use mlua::prelude::*; + +use lune_utils::TableBuilder; + +/** + Creates the `fs` standard library module. + + # Errors + + Errors when out of memory. +*/ +pub fn module(lua: &Lua) -> LuaResult { + TableBuilder::new(lua)?.build_readonly() +} diff --git a/crates/lune-std-luau/Cargo.toml b/crates/lune-std-luau/Cargo.toml index fe51e61..dcd57c0 100644 --- a/crates/lune-std-luau/Cargo.toml +++ b/crates/lune-std-luau/Cargo.toml @@ -9,3 +9,8 @@ path = "src/lib.rs" [lints] workspace = true + +[dependencies] +mlua = "0.9.7" + +lune-utils = { version = "0.8.3", path = "../lune-utils" } diff --git a/crates/lune-std-luau/src/lib.rs b/crates/lune-std-luau/src/lib.rs index 2e802e7..59b0934 100644 --- a/crates/lune-std-luau/src/lib.rs +++ b/crates/lune-std-luau/src/lib.rs @@ -1 +1,16 @@ #![allow(clippy::cargo_common_metadata)] + +use mlua::prelude::*; + +use lune_utils::TableBuilder; + +/** + Creates the `luau` standard library module. + + # Errors + + Errors when out of memory. +*/ +pub fn module(lua: &Lua) -> LuaResult { + TableBuilder::new(lua)?.build_readonly() +} diff --git a/crates/lune-std-net/Cargo.toml b/crates/lune-std-net/Cargo.toml index 6409e9d..ff10648 100644 --- a/crates/lune-std-net/Cargo.toml +++ b/crates/lune-std-net/Cargo.toml @@ -9,3 +9,8 @@ path = "src/lib.rs" [lints] workspace = true + +[dependencies] +mlua = "0.9.7" + +lune-utils = { version = "0.8.3", path = "../lune-utils" } diff --git a/crates/lune-std-net/src/lib.rs b/crates/lune-std-net/src/lib.rs index 2e802e7..24b8917 100644 --- a/crates/lune-std-net/src/lib.rs +++ b/crates/lune-std-net/src/lib.rs @@ -1 +1,16 @@ #![allow(clippy::cargo_common_metadata)] + +use mlua::prelude::*; + +use lune_utils::TableBuilder; + +/** + Creates the `net` standard library module. + + # Errors + + Errors when out of memory. +*/ +pub fn module(lua: &Lua) -> LuaResult { + TableBuilder::new(lua)?.build_readonly() +} diff --git a/crates/lune-std-process/Cargo.toml b/crates/lune-std-process/Cargo.toml index 0c743bb..7676d24 100644 --- a/crates/lune-std-process/Cargo.toml +++ b/crates/lune-std-process/Cargo.toml @@ -9,3 +9,8 @@ path = "src/lib.rs" [lints] workspace = true + +[dependencies] +mlua = "0.9.7" + +lune-utils = { version = "0.8.3", path = "../lune-utils" } diff --git a/crates/lune-std-process/src/lib.rs b/crates/lune-std-process/src/lib.rs index 2e802e7..4490966 100644 --- a/crates/lune-std-process/src/lib.rs +++ b/crates/lune-std-process/src/lib.rs @@ -1 +1,16 @@ #![allow(clippy::cargo_common_metadata)] + +use mlua::prelude::*; + +use lune_utils::TableBuilder; + +/** + Creates the `process` standard library module. + + # Errors + + Errors when out of memory. +*/ +pub fn module(lua: &Lua) -> LuaResult { + TableBuilder::new(lua)?.build_readonly() +} diff --git a/crates/lune-std-regex/Cargo.toml b/crates/lune-std-regex/Cargo.toml index 294e70d..a8e2ecd 100644 --- a/crates/lune-std-regex/Cargo.toml +++ b/crates/lune-std-regex/Cargo.toml @@ -9,3 +9,8 @@ path = "src/lib.rs" [lints] workspace = true + +[dependencies] +mlua = "0.9.7" + +lune-utils = { version = "0.8.3", path = "../lune-utils" } diff --git a/crates/lune-std-regex/src/lib.rs b/crates/lune-std-regex/src/lib.rs index 2e802e7..ad73214 100644 --- a/crates/lune-std-regex/src/lib.rs +++ b/crates/lune-std-regex/src/lib.rs @@ -1 +1,16 @@ #![allow(clippy::cargo_common_metadata)] + +use mlua::prelude::*; + +use lune_utils::TableBuilder; + +/** + Creates the `regex` standard library module. + + # Errors + + Errors when out of memory. +*/ +pub fn module(lua: &Lua) -> LuaResult { + TableBuilder::new(lua)?.build_readonly() +} diff --git a/crates/lune-std-roblox/Cargo.toml b/crates/lune-std-roblox/Cargo.toml index d66ce67..d84a73c 100644 --- a/crates/lune-std-roblox/Cargo.toml +++ b/crates/lune-std-roblox/Cargo.toml @@ -9,3 +9,8 @@ path = "src/lib.rs" [lints] workspace = true + +[dependencies] +mlua = "0.9.7" + +lune-utils = { version = "0.8.3", path = "../lune-utils" } diff --git a/crates/lune-std-roblox/src/lib.rs b/crates/lune-std-roblox/src/lib.rs index 2e802e7..5f26a9e 100644 --- a/crates/lune-std-roblox/src/lib.rs +++ b/crates/lune-std-roblox/src/lib.rs @@ -1 +1,16 @@ #![allow(clippy::cargo_common_metadata)] + +use mlua::prelude::*; + +use lune_utils::TableBuilder; + +/** + Creates the `roblox` standard library module. + + # Errors + + Errors when out of memory. +*/ +pub fn module(lua: &Lua) -> LuaResult { + TableBuilder::new(lua)?.build_readonly() +} diff --git a/crates/lune-std-serde/Cargo.toml b/crates/lune-std-serde/Cargo.toml index d83ea10..f34b1ea 100644 --- a/crates/lune-std-serde/Cargo.toml +++ b/crates/lune-std-serde/Cargo.toml @@ -9,3 +9,8 @@ path = "src/lib.rs" [lints] workspace = true + +[dependencies] +mlua = "0.9.7" + +lune-utils = { version = "0.8.3", path = "../lune-utils" } diff --git a/crates/lune-std-serde/src/lib.rs b/crates/lune-std-serde/src/lib.rs index 2e802e7..38f6980 100644 --- a/crates/lune-std-serde/src/lib.rs +++ b/crates/lune-std-serde/src/lib.rs @@ -1 +1,16 @@ #![allow(clippy::cargo_common_metadata)] + +use mlua::prelude::*; + +use lune_utils::TableBuilder; + +/** + Creates the `serde` standard library module. + + # Errors + + Errors when out of memory. +*/ +pub fn module(lua: &Lua) -> LuaResult { + TableBuilder::new(lua)?.build_readonly() +} diff --git a/crates/lune-std/Cargo.toml b/crates/lune-std/Cargo.toml index b4a3dbd..0a33e42 100644 --- a/crates/lune-std/Cargo.toml +++ b/crates/lune-std/Cargo.toml @@ -36,6 +36,8 @@ stdio = ["dep:lune-std-stdio"] task = ["dep:lune-std-task"] [dependencies] +mlua = "0.9.7" + lune-std-datetime = { optional = true, version = "0.8.3", path = "../lune-std-datetime" } lune-std-fs = { optional = true, version = "0.8.3", path = "../lune-std-fs" } lune-std-luau = { optional = true, version = "0.8.3", path = "../lune-std-luau" } diff --git a/crates/lune-std/src/lib.rs b/crates/lune-std/src/lib.rs index 2e802e7..8e9b1df 100644 --- a/crates/lune-std/src/lib.rs +++ b/crates/lune-std/src/lib.rs @@ -1 +1,5 @@ #![allow(clippy::cargo_common_metadata)] + +mod library; + +pub use library::LuneStandardLibrary; diff --git a/crates/lune-std/src/library.rs b/crates/lune-std/src/library.rs new file mode 100644 index 0000000..b6e7204 --- /dev/null +++ b/crates/lune-std/src/library.rs @@ -0,0 +1,121 @@ +use std::str::FromStr; + +use mlua::prelude::*; + +/** + A standard library provided by Lune. +*/ +#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] +#[rustfmt::skip] +pub enum LuneStandardLibrary { + #[cfg(feature = "datetime")] DateTime, + #[cfg(feature = "fs")] Fs, + #[cfg(feature = "luau")] Luau, + #[cfg(feature = "net")] Net, + #[cfg(feature = "task")] Task, + #[cfg(feature = "process")] Process, + #[cfg(feature = "regex")] Regex, + #[cfg(feature = "serde")] Serde, + #[cfg(feature = "stdio")] Stdio, + #[cfg(feature = "roblox")] Roblox, +} + +impl LuneStandardLibrary { + /** + All available standard libraries. + */ + #[rustfmt::skip] + pub const ALL: &'static [Self] = &[ + #[cfg(feature = "datetime")] Self::DateTime, + #[cfg(feature = "fs")] Self::Fs, + #[cfg(feature = "luau")] Self::Luau, + #[cfg(feature = "net")] Self::Net, + #[cfg(feature = "task")] Self::Task, + #[cfg(feature = "process")] Self::Process, + #[cfg(feature = "regex")] Self::Regex, + #[cfg(feature = "serde")] Self::Serde, + #[cfg(feature = "stdio")] Self::Stdio, + #[cfg(feature = "roblox")] Self::Roblox, + ]; + + /** + Gets the name of the library, such as `datetime` or `fs`. + */ + #[must_use] + #[rustfmt::skip] + #[allow(unreachable_patterns)] + pub fn name(&self) -> &'static str { + match self { + #[cfg(feature = "datetime")] Self::DateTime => "datetime", + #[cfg(feature = "fs")] Self::Fs => "fs", + #[cfg(feature = "luau")] Self::Luau => "luau", + #[cfg(feature = "net")] Self::Net => "net", + #[cfg(feature = "task")] Self::Task => "task", + #[cfg(feature = "process")] Self::Process => "process", + #[cfg(feature = "regex")] Self::Regex => "regex", + #[cfg(feature = "serde")] Self::Serde => "serde", + #[cfg(feature = "stdio")] Self::Stdio => "stdio", + #[cfg(feature = "roblox")] Self::Roblox => "roblox", + + _ => unreachable!("no standard library enabled"), + } + } + + /** + Creates the Lua module for the library. + + # Errors + + If the library could not be created. + */ + #[rustfmt::skip] + #[allow(unreachable_patterns)] + pub fn module<'lua>(&self, lua: &'lua Lua) -> LuaResult> { + let res = match self { + #[cfg(feature = "datetime")] Self::DateTime => lune_std_datetime::module(lua), + #[cfg(feature = "fs")] Self::Fs => lune_std_fs::module(lua), + #[cfg(feature = "luau")] Self::Luau => lune_std_luau::module(lua), + #[cfg(feature = "net")] Self::Net => lune_std_net::module(lua), + #[cfg(feature = "task")] Self::Task => lune_std_task::module(lua), + #[cfg(feature = "process")] Self::Process => lune_std_process::module(lua), + #[cfg(feature = "regex")] Self::Regex => lune_std_regex::module(lua), + #[cfg(feature = "serde")] Self::Serde => lune_std_serde::module(lua), + #[cfg(feature = "stdio")] Self::Stdio => lune_std_stdio::module(lua), + #[cfg(feature = "roblox")] Self::Roblox => lune_std_roblox::module(lua), + + _ => unreachable!("no standard library enabled"), + }; + match res { + Ok(v) => v.into_lua_multi(lua), + Err(e) => Err(e.context(format!( + "Failed to create standard library '{}'", + self.name() + ))), + } + } +} + +impl FromStr for LuneStandardLibrary { + type Err = String; + #[rustfmt::skip] + fn from_str(s: &str) -> Result { + let low = s.trim().to_ascii_lowercase(); + Ok(match low.as_str() { + #[cfg(feature = "datetime")] "datetime" => Self::DateTime, + #[cfg(feature = "fs")] "fs" => Self::Fs, + #[cfg(feature = "luau")] "luau" => Self::Luau, + #[cfg(feature = "net")] "net" => Self::Net, + #[cfg(feature = "task")] "task" => Self::Task, + #[cfg(feature = "process")] "process" => Self::Process, + #[cfg(feature = "regex")] "regex" => Self::Regex, + #[cfg(feature = "serde")] "serde" => Self::Serde, + #[cfg(feature = "stdio")] "stdio" => Self::Stdio, + #[cfg(feature = "roblox")] "roblox" => Self::Roblox, + + _ => return Err(format!( + "Unknown standard library '{low}'\nValid libraries are: {}", + Self::ALL.iter().map(Self::name).collect::>().join(", ") + )), + }) + } +}