diff --git a/crates/lune-std/src/globals/require/path.rs b/crates/lune-std/src/globals/require/path.rs index e889b82..c00ad55 100644 --- a/crates/lune-std/src/globals/require/path.rs +++ b/crates/lune-std/src/globals/require/path.rs @@ -2,10 +2,14 @@ use mlua::prelude::*; use std::path::{Component, Path, PathBuf}; use tokio::fs; -/// tries these alternatives on given path: -/// -/// * .lua and .luau extension -/// * path.join("init.luau") and path.join("init.lua") +/** + +tries these alternatives on given path if path doesn't exist + +* .lua and .luau extension +* path.join("init.luau") and path.join("init.lua") + + */ pub async fn resolve_path(path: &Path) -> LuaResult { let init_path = &path.join("init"); @@ -28,6 +32,15 @@ pub async fn resolve_path(path: &Path) -> LuaResult { Err(LuaError::runtime("Could not resolve path")) } +/** + +Removes useless components from the given path + +### Example + +`./path/./path` turns into `./path/path` + + */ pub fn normalize_path(path: &Path) -> PathBuf { let mut components = path.components().peekable(); let mut ret = if let Some(c @ Component::Prefix(..)) = components.clone().peek() { @@ -55,8 +68,17 @@ pub fn normalize_path(path: &Path) -> PathBuf { ret } +/** + +adds extension to path without replacing it's current extensions + +### Example + +appending `.luau` to `path/path.config` will return `path/path.config.luau` + + */ fn append_extension(path: impl Into, ext: &'static str) -> PathBuf { - let mut new = path.into(); + let mut new: PathBuf = path.into(); match new.extension() { // FUTURE: There's probably a better way to do this than converting to a lossy string Some(e) => new.set_extension(format!("{}.{ext}", e.to_string_lossy())), diff --git a/crates/lune-std/src/path.rs b/crates/lune-std/src/path.rs index e35f9f5..bf33309 100644 --- a/crates/lune-std/src/path.rs +++ b/crates/lune-std/src/path.rs @@ -1,5 +1,10 @@ use std::path::PathBuf; +/** + +return's the path of the script that called this function + + */ pub fn get_script_path(lua: &mlua::Lua) -> Result { let Some(debug) = lua.inspect_stack(2) else { return Err(mlua::Error::runtime("Failed to inspect stack")); @@ -17,6 +22,11 @@ pub fn get_script_path(lua: &mlua::Lua) -> Result { } } +/** + +return's the parent directory of the script that called this function + + */ pub fn get_parent_path(lua: &mlua::Lua) -> Result { let script = get_script_path(lua)?;