lune_std/
library.rs

1use std::str::FromStr;
2
3use mlua::prelude::*;
4
5/**
6    A standard library provided by Lune.
7*/
8#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
9#[rustfmt::skip]
10pub enum LuneStandardLibrary {
11    #[cfg(feature = "datetime")] DateTime,
12    #[cfg(feature = "fs")]       Fs,
13    #[cfg(feature = "luau")]     Luau,
14    #[cfg(feature = "net")]      Net,
15    #[cfg(feature = "task")]     Task,
16    #[cfg(feature = "process")]  Process,
17    #[cfg(feature = "regex")]    Regex,
18    #[cfg(feature = "serde")]    Serde,
19    #[cfg(feature = "stdio")]    Stdio,
20    #[cfg(feature = "roblox")]   Roblox,
21}
22
23impl LuneStandardLibrary {
24    /**
25        All available standard libraries.
26    */
27    #[rustfmt::skip]
28    pub const ALL: &'static [Self] = &[
29        #[cfg(feature = "datetime")] Self::DateTime,
30        #[cfg(feature = "fs")]       Self::Fs,
31        #[cfg(feature = "luau")]     Self::Luau,
32        #[cfg(feature = "net")]      Self::Net,
33        #[cfg(feature = "task")]     Self::Task,
34        #[cfg(feature = "process")]  Self::Process,
35        #[cfg(feature = "regex")]    Self::Regex,
36        #[cfg(feature = "serde")]    Self::Serde,
37        #[cfg(feature = "stdio")]    Self::Stdio,
38        #[cfg(feature = "roblox")]   Self::Roblox,
39    ];
40
41    /**
42        Gets the name of the library, such as `datetime` or `fs`.
43    */
44    #[must_use]
45    #[rustfmt::skip]
46    #[allow(unreachable_patterns)]
47    pub fn name(&self) -> &'static str {
48        match self {
49            #[cfg(feature = "datetime")] Self::DateTime => "datetime",
50            #[cfg(feature = "fs")]       Self::Fs       => "fs",
51            #[cfg(feature = "luau")]     Self::Luau     => "luau",
52            #[cfg(feature = "net")]      Self::Net      => "net",
53            #[cfg(feature = "task")]     Self::Task     => "task",
54            #[cfg(feature = "process")]  Self::Process  => "process",
55            #[cfg(feature = "regex")]    Self::Regex    => "regex",
56            #[cfg(feature = "serde")]    Self::Serde    => "serde",
57            #[cfg(feature = "stdio")]    Self::Stdio    => "stdio",
58            #[cfg(feature = "roblox")]   Self::Roblox   => "roblox",
59
60            _ => unreachable!("no standard library enabled"),
61        }
62    }
63
64    /**
65        Returns type definitions for the library.
66    */
67    #[must_use]
68    #[rustfmt::skip]
69    #[allow(unreachable_patterns)]
70    pub fn typedefs(&self) -> String {
71    	match self {
72            #[cfg(feature = "datetime")] Self::DateTime => lune_std_datetime::typedefs(),
73            #[cfg(feature = "fs")]       Self::Fs       => lune_std_fs::typedefs(),
74            #[cfg(feature = "luau")]     Self::Luau     => lune_std_luau::typedefs(),
75            #[cfg(feature = "net")]      Self::Net      => lune_std_net::typedefs(),
76            #[cfg(feature = "task")]     Self::Task     => lune_std_task::typedefs(),
77            #[cfg(feature = "process")]  Self::Process  => lune_std_process::typedefs(),
78            #[cfg(feature = "regex")]    Self::Regex    => lune_std_regex::typedefs(),
79            #[cfg(feature = "serde")]    Self::Serde    => lune_std_serde::typedefs(),
80            #[cfg(feature = "stdio")]    Self::Stdio    => lune_std_stdio::typedefs(),
81            #[cfg(feature = "roblox")]   Self::Roblox   => lune_std_roblox::typedefs(),
82
83            _ => unreachable!("no standard library enabled"),
84        }
85    }
86
87    /**
88        Creates the Lua module for the library.
89
90        # Errors
91
92        If the library could not be created.
93    */
94    #[rustfmt::skip]
95    #[allow(unreachable_patterns)]
96    pub fn module(&self, lua: Lua) -> LuaResult<LuaTable> {
97        let mod_lua = lua.clone();
98        let res: LuaResult<LuaTable> = match self {
99            #[cfg(feature = "datetime")] Self::DateTime => lune_std_datetime::module(mod_lua),
100            #[cfg(feature = "fs")]       Self::Fs       => lune_std_fs::module(mod_lua),
101            #[cfg(feature = "luau")]     Self::Luau     => lune_std_luau::module(mod_lua),
102            #[cfg(feature = "net")]      Self::Net      => lune_std_net::module(mod_lua),
103            #[cfg(feature = "task")]     Self::Task     => lune_std_task::module(mod_lua),
104            #[cfg(feature = "process")]  Self::Process  => lune_std_process::module(mod_lua),
105            #[cfg(feature = "regex")]    Self::Regex    => lune_std_regex::module(mod_lua),
106            #[cfg(feature = "serde")]    Self::Serde    => lune_std_serde::module(mod_lua),
107            #[cfg(feature = "stdio")]    Self::Stdio    => lune_std_stdio::module(mod_lua),
108            #[cfg(feature = "roblox")]   Self::Roblox   => lune_std_roblox::module(mod_lua),
109
110            _ => unreachable!("no standard library enabled"),
111        };
112        match res {
113            Ok(v) => Ok(v),
114            Err(e) => Err(e.context(format!(
115                "Failed to create standard library '{}'",
116                self.name()
117            ))),
118        }
119    }
120}
121
122impl FromStr for LuneStandardLibrary {
123    type Err = String;
124    #[rustfmt::skip]
125    fn from_str(s: &str) -> Result<Self, Self::Err> {
126        let low = s.trim().to_ascii_lowercase();
127        Ok(match low.as_str() {
128            #[cfg(feature = "datetime")] "datetime" => Self::DateTime,
129            #[cfg(feature = "fs")]       "fs"       => Self::Fs,
130            #[cfg(feature = "luau")]     "luau"     => Self::Luau,
131            #[cfg(feature = "net")]      "net"      => Self::Net,
132            #[cfg(feature = "task")]     "task"     => Self::Task,
133            #[cfg(feature = "process")]  "process"  => Self::Process,
134            #[cfg(feature = "regex")]    "regex"    => Self::Regex,
135            #[cfg(feature = "serde")]    "serde"    => Self::Serde,
136            #[cfg(feature = "stdio")]    "stdio"    => Self::Stdio,
137            #[cfg(feature = "roblox")]   "roblox"   => Self::Roblox,
138
139            _ => {
140                return Err(format!(
141                    "Unknown standard library '{low}'\nValid libraries are: {}",
142                    Self::ALL
143                        .iter()
144                        .map(Self::name)
145                        .collect::<Vec<_>>()
146                        .join(", ")
147                ))
148            }
149        })
150    }
151}