mirror of
https://github.com/lune-org/lune.git
synced 2025-04-13 06:50:54 +01:00
move path functions to path.rs
This commit is contained in:
parent
505ff977f0
commit
13cff6ff00
2 changed files with 43 additions and 39 deletions
|
@ -1,35 +1,10 @@
|
||||||
use crate::{luaurc::path_to_alias, path::get_parent_path, LuneStandardLibrary};
|
use crate::{luaurc::path_to_alias, path::get_parent_path, LuneStandardLibrary};
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
use std::path::{Path, PathBuf};
|
use path::resolve_path;
|
||||||
use tokio::fs;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub mod context;
|
mod context;
|
||||||
|
mod path;
|
||||||
/// 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<PathBuf> {
|
|
||||||
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"))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn lua_require(lua: &Lua, path: String) -> LuaResult<LuaMultiValue> {
|
pub async fn lua_require(lua: &Lua, path: String) -> LuaResult<LuaMultiValue> {
|
||||||
let require_path_rel = PathBuf::from(path);
|
let require_path_rel = PathBuf::from(path);
|
||||||
|
@ -70,13 +45,3 @@ pub fn create(lua: &Lua) -> LuaResult<LuaValue> {
|
||||||
|
|
||||||
f.into_lua(lua)
|
f.into_lua(lua)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn append_extension(path: impl Into<PathBuf>, 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
|
|
||||||
}
|
|
||||||
|
|
39
crates/lune-std/src/globals/require/path.rs
Normal file
39
crates/lune-std/src/globals/require/path.rs
Normal file
|
@ -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<PathBuf> {
|
||||||
|
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<PathBuf>, 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
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue