Skip to content

Commit fc9aee8

Browse files
committed
Make some module methods public
1 parent 4923740 commit fc9aee8

File tree

5 files changed

+46
-21
lines changed

5 files changed

+46
-21
lines changed

src/bytes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ impl<T: AsRef<[u8]> + MaybeSend + 'static> From<T> for BytesBox {
2121

2222
impl UserData for BytesBox {}
2323

24-
pub(crate) enum StringOrBytes {
24+
/// A type that can represent either a Lua string or a `BytesBox` userdata.
25+
pub enum StringOrBytes {
2526
String(LuaString),
2627
Bytes(UserDataRef<BytesBox>),
2728
}

src/env.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,42 @@ use std::result::Result as StdResult;
44
use mlua::{Lua, Result, Table};
55

66
/// Returns the current working directory
7-
fn current_dir(_lua: &Lua, _: ()) -> Result<StdResult<PathBuf, String>> {
7+
pub fn current_dir(_lua: &Lua, _: ()) -> Result<StdResult<PathBuf, String>> {
88
let dir = lua_try!(std::env::current_dir());
99
Ok(Ok(dir))
1010
}
1111

1212
/// Changes the current working directory to the specified path
13-
fn set_current_dir(_lua: &Lua, path: String) -> Result<StdResult<bool, String>> {
13+
pub fn set_current_dir(_lua: &Lua, path: String) -> Result<StdResult<bool, String>> {
1414
lua_try!(std::env::set_current_dir(path));
1515
Ok(Ok(true))
1616
}
1717

1818
/// Returns the full filesystem path of the current running executable
19-
fn current_exe(_lua: &Lua, _: ()) -> Result<StdResult<PathBuf, String>> {
19+
pub fn current_exe(_lua: &Lua, _: ()) -> Result<StdResult<PathBuf, String>> {
2020
let exe = lua_try!(std::env::current_exe());
2121
Ok(Ok(exe))
2222
}
2323

2424
/// Returns the path of the current user’s home directory if known
25-
fn home_dir(_lua: &Lua, _: ()) -> Result<Option<PathBuf>> {
25+
pub fn home_dir(_lua: &Lua, _: ()) -> Result<Option<PathBuf>> {
2626
Ok(std::env::home_dir())
2727
}
2828

2929
/// Fetches the environment variable key from the current process
30-
fn var(_lua: &Lua, key: String) -> Result<Option<String>> {
30+
pub fn var(_lua: &Lua, key: String) -> Result<Option<String>> {
3131
Ok(std::env::var(key).ok())
3232
}
3333

3434
/// Returns a table containing all environment variables of the current process
35-
fn vars(lua: &Lua, _: ()) -> Result<Table> {
35+
pub fn vars(lua: &Lua, _: ()) -> Result<Table> {
3636
lua.create_table_from(std::env::vars())
3737
}
3838

3939
/// Sets the environment variable key to the value in the current process
4040
///
4141
/// If value is Nil, the environment variable will be removed
42-
fn set_var(_lua: &Lua, (key, value): (String, Option<String>)) -> Result<()> {
42+
pub fn set_var(_lua: &Lua, (key, value): (String, Option<String>)) -> Result<()> {
4343
match value {
4444
Some(v) => unsafe { std::env::set_var(key, v) },
4545
None => unsafe { std::env::remove_var(key) },

src/json.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,11 @@ struct LuaJsonMapIter {
188188
iter: serde_json::map::Iter<'this>,
189189
}
190190

191-
fn decode(lua: &Lua, (data, opts): (StringOrBytes, Option<Table>)) -> Result<StdResult<Value, String>> {
191+
/// Decodes a JSON string or bytes into a Lua value.
192+
///
193+
/// The optional `opts` table can contain:
194+
/// - `set_array_metatable` (boolean): If true, sets a metatable for arrays. Default is false.
195+
pub fn decode(lua: &Lua, (data, opts): (StringOrBytes, Option<Table>)) -> Result<StdResult<Value, String>> {
192196
let opts = opts.as_ref();
193197
let mut options = SerializeOptions::new();
194198
if let Some(enabled) = opts.and_then(|t| t.get::<bool>("set_array_metatable").ok()) {
@@ -199,20 +203,28 @@ fn decode(lua: &Lua, (data, opts): (StringOrBytes, Option<Table>)) -> Result<Std
199203
Ok(Ok(lua.to_value_with(&json, options)?))
200204
}
201205

202-
fn decode_native(lua: &Lua, data: StringOrBytes) -> Result<StdResult<Value, String>> {
206+
/// Decodes a JSON string or bytes as a native Rust object.
207+
///
208+
/// The returned value can be a primitive type or userdata.
209+
pub fn decode_native(lua: &Lua, data: StringOrBytes) -> Result<StdResult<Value, String>> {
203210
let json: serde_json::Value = lua_try!(serde_json::from_slice(&data.as_bytes_deref()));
204211
Ok(Ok(lua_try!(JsonObject::from(json).into_lua(lua))))
205212
}
206213

207-
fn encode(value: Value, options: Option<Table>) -> StdResult<String, String> {
214+
/// Encodes a Lua value into a JSON string.
215+
///
216+
/// The optional `opts` table can contain:
217+
/// - `pretty` (boolean): If true, pretty formats the JSON string. Default is false.
218+
/// - `relaxed` (boolean): If true, skip recursive tables and unsupported types. Default is false.
219+
pub fn encode(value: Value, opts: Option<Table>) -> StdResult<String, String> {
208220
let mut value = value.to_serializable();
209-
let options = options.as_ref();
221+
let opts = opts.as_ref();
210222

211-
if options.and_then(|t| t.get::<bool>("relaxed").ok()) == Some(true) {
223+
if opts.and_then(|t| t.get::<bool>("relaxed").ok()) == Some(true) {
212224
value = value.deny_recursive_tables(false).deny_unsupported_types(false);
213225
}
214226

215-
if options.and_then(|t| t.get::<bool>("pretty").ok()) == Some(true) {
227+
if opts.and_then(|t| t.get::<bool>("pretty").ok()) == Some(true) {
216228
value = value.sort_keys(true);
217229
return serde_json::to_string_pretty(&value).map_err(|e| e.to_string());
218230
}

src/regex.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use quick_cache::sync::Cache;
99
// A reasonable cache size for regexes. This can be adjusted as needed.
1010
const REGEX_CACHE_SIZE: usize = 256;
1111

12+
/// A compiled regular expression.
1213
#[derive(Clone, Debug)]
1314
pub struct Regex(regex::bytes::Regex);
1415

@@ -152,25 +153,25 @@ impl UserData for RegexSet {
152153
/// Compiles a regular expression.
153154
///
154155
/// Once compiled, it can be used repeatedly to search, split or replace substrings in a text.
155-
fn regex_new(lua: &Lua, re: LuaString) -> Result<StdResult<Regex, String>> {
156+
pub fn regex_new(lua: &Lua, re: LuaString) -> Result<StdResult<Regex, String>> {
156157
let re = re.to_str()?;
157158
Ok(Ok(lua_try!(Regex::new(lua, &re))))
158159
}
159160

160161
/// Escapes a string so that it can be used as a literal in a regular expression.
161-
fn regex_escape(_: &Lua, text: LuaString) -> Result<String> {
162+
pub fn regex_escape(_: &Lua, text: LuaString) -> Result<String> {
162163
Ok(regex::escape(&text.to_str()?))
163164
}
164165

165166
/// Returns true if there is a match for the regex anywhere in the given text.
166-
fn regex_is_match(lua: &Lua, (re, text): (LuaString, LuaString)) -> Result<StdResult<bool, String>> {
167+
pub fn regex_is_match(lua: &Lua, (re, text): (LuaString, LuaString)) -> Result<StdResult<bool, String>> {
167168
let re = re.to_str()?;
168169
let re = lua_try!(Regex::new(lua, &re));
169170
Ok(Ok(re.is_match(&text.as_bytes())))
170171
}
171172

172173
/// Returns all matches of the regex in the given text or nil if there is no match.
173-
fn regex_match(lua: &Lua, (re, text): (LuaString, LuaString)) -> Result<StdResult<Value, String>> {
174+
pub fn regex_match(lua: &Lua, (re, text): (LuaString, LuaString)) -> Result<StdResult<Value, String>> {
174175
let re = re.to_str()?;
175176
let re = lua_try!(Regex::new(lua, &re));
176177
match re.captures(&text.as_bytes()) {

src/yaml.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,11 @@ struct LuaYamlMapIter {
194194
iter: serde_yaml::mapping::Iter<'this>,
195195
}
196196

197-
fn decode(lua: &Lua, (data, opts): (StringOrBytes, Option<Table>)) -> Result<StdResult<Value, String>> {
197+
/// Decodes a YAML string or byte array into a Lua value.
198+
///
199+
/// The `opts` table can contain the following options:
200+
/// - `set_array_metatable` (boolean): If true, sets a metatable for arrays. Default is false.
201+
pub fn decode(lua: &Lua, (data, opts): (StringOrBytes, Option<Table>)) -> Result<StdResult<Value, String>> {
198202
let opts = opts.as_ref();
199203
let mut options = SerializeOptions::new();
200204
if let Some(enabled) = opts.and_then(|t| t.get::<bool>("set_array_metatable").ok()) {
@@ -206,13 +210,20 @@ fn decode(lua: &Lua, (data, opts): (StringOrBytes, Option<Table>)) -> Result<Std
206210
Ok(Ok(lua.to_value_with(&yaml, options)?))
207211
}
208212

209-
fn decode_native(lua: &Lua, data: StringOrBytes) -> Result<StdResult<Value, String>> {
213+
/// Decodes a YAML string or bytes as a native Rust object.
214+
///
215+
/// The returned value can be a primitive type or userdata.
216+
pub fn decode_native(lua: &Lua, data: StringOrBytes) -> Result<StdResult<Value, String>> {
210217
let mut yaml: serde_yaml::Value = lua_try!(serde_yaml::from_slice(&data.as_bytes_deref()));
211218
lua_try!(yaml.apply_merge());
212219
Ok(Ok(lua_try!(YamlObject::from(yaml).into_lua(lua))))
213220
}
214221

215-
fn encode(value: Value, opts: Option<Table>) -> StdResult<String, String> {
222+
/// Encodes a Lua value into a YAML string.
223+
///
224+
/// The optional `opts` table can contain:
225+
/// - `relaxed` (boolean): If true, skip recursive tables and unsupported types. Default is false.
226+
pub fn encode(value: Value, opts: Option<Table>) -> StdResult<String, String> {
216227
let opts = opts.as_ref();
217228
let mut value = value.to_serializable();
218229

0 commit comments

Comments
 (0)