From 13cff6ff0080329d22f53650d45c2a7c519bb4e6 Mon Sep 17 00:00:00 2001 From: highflowey Date: Fri, 23 Aug 2024 03:27:32 +0330 Subject: [PATCH] move path functions to path.rs --- crates/lune-std/src/globals/require/mod.rs | 43 ++------------------- crates/lune-std/src/globals/require/path.rs | 39 +++++++++++++++++++ 2 files changed, 43 insertions(+), 39 deletions(-) create mode 100644 crates/lune-std/src/globals/require/path.rs diff --git a/crates/lune-std/src/globals/require/mod.rs b/crates/lune-std/src/globals/require/mod.rs index 27ed019..1bc98fd 100644 --- a/crates/lune-std/src/globals/require/mod.rs +++ b/crates/lune-std/src/globals/require/mod.rs @@ -1,35 +1,10 @@ use crate::{luaurc::path_to_alias, path::get_parent_path, LuneStandardLibrary}; use mlua::prelude::*; -use std::path::{Path, PathBuf}; -use tokio::fs; +use path::resolve_path; +use std::path::PathBuf; -pub mod context; - -/// tries these alternatives on given path: -/// -/// * .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"); - - for ext in ["lua", "luau"] { - // try extension on given path - let path = append_extension(path, ext); - - if fs::try_exists(&path).await? { - return Ok(path); - }; - - // try extension on given path's init - let init_path = append_extension(init_path, ext); - - if fs::try_exists(&init_path).await? { - return Ok(init_path); - }; - } - - Err(LuaError::runtime("Could not resolve path")) -} +mod context; +mod path; pub async fn lua_require(lua: &Lua, path: String) -> LuaResult { let require_path_rel = PathBuf::from(path); @@ -70,13 +45,3 @@ pub fn create(lua: &Lua) -> LuaResult { f.into_lua(lua) } - -fn append_extension(path: impl Into, ext: &'static str) -> PathBuf { - let mut new = 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())), - None => new.set_extension(ext), - }; - new -} diff --git a/crates/lune-std/src/globals/require/path.rs b/crates/lune-std/src/globals/require/path.rs new file mode 100644 index 0000000..e11b3c7 --- /dev/null +++ b/crates/lune-std/src/globals/require/path.rs @@ -0,0 +1,39 @@ +use mlua::prelude::*; +use std::path::{Path, PathBuf}; +use tokio::fs; + +/// tries these alternatives on given path: +/// +/// * .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"); + + for ext in ["lua", "luau"] { + // try extension on given path + let path = append_extension(path, ext); + + if fs::try_exists(&path).await? { + return Ok(path); + }; + + // try extension on given path's init + let init_path = append_extension(init_path, ext); + + if fs::try_exists(&init_path).await? { + return Ok(init_path); + }; + } + + Err(LuaError::runtime("Could not resolve path")) +} + +fn append_extension(path: impl Into, ext: &'static str) -> PathBuf { + let mut new = 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())), + None => new.set_extension(ext), + }; + new +}