mirror of
https://github.com/lune-org/lune.git
synced 2025-04-16 01:43:49 +01:00
resolve require path
This commit is contained in:
parent
ba2d7203f8
commit
3f709f1e96
2 changed files with 36 additions and 3 deletions
|
@ -1,5 +1,38 @@
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
|
|
||||||
pub fn create(lua: &Lua) -> LuaResult<LuaValue> {
|
use crate::{luaurc::path_to_alias, path::get_parent_path};
|
||||||
todo!()
|
|
||||||
|
pub async fn lua_require(lua: &Lua, path: String) -> LuaResult<LuaMultiValue> {
|
||||||
|
let require_path_rel = PathBuf::from(path);
|
||||||
|
let require_alias = path_to_alias(&require_path_rel)?;
|
||||||
|
|
||||||
|
if let Some(require_alias) = require_alias {
|
||||||
|
if require_alias.alias == "lune" {
|
||||||
|
Err(LuaError::runtime(format!(
|
||||||
|
"Tried requiring a lune library '{}'\nbut aliases are not implemented yet.",
|
||||||
|
require_alias.path,
|
||||||
|
)))
|
||||||
|
} else {
|
||||||
|
Err(LuaError::runtime(format!(
|
||||||
|
"Tried requiring a custom alias '{}'\nbut aliases are not implemented yet.",
|
||||||
|
require_alias.alias,
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let parent_path = get_parent_path(lua)?;
|
||||||
|
let require_path_abs = parent_path.join(require_path_rel);
|
||||||
|
|
||||||
|
Err(LuaError::runtime(format!(
|
||||||
|
"Tried requiring '{}'\nbut requires are not implemented yet.",
|
||||||
|
require_path_abs.to_string_lossy(),
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create(lua: &Lua) -> LuaResult<LuaValue> {
|
||||||
|
let f = lua.create_async_function(lua_require)?;
|
||||||
|
|
||||||
|
f.into_lua(lua)
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ pub struct Luaurc {
|
||||||
/// `@lune/task` becomes `Some({ alias: "lune", path: "task" })`
|
/// `@lune/task` becomes `Some({ alias: "lune", path: "task" })`
|
||||||
///
|
///
|
||||||
/// `../path/script` becomes `None`
|
/// `../path/script` becomes `None`
|
||||||
pub fn path_to_alias(path: &Path) -> Result<Option<RequireAlias>, mlua::Error> {
|
pub fn path_to_alias(path: &Path) -> Result<Option<RequireAlias<'_>>, mlua::Error> {
|
||||||
if let Some(aliased_path) = path
|
if let Some(aliased_path) = path
|
||||||
.to_str()
|
.to_str()
|
||||||
.ok_or(mlua::Error::runtime("Couldn't turn path into string"))?
|
.ok_or(mlua::Error::runtime("Couldn't turn path into string"))?
|
||||||
|
|
Loading…
Add table
Reference in a new issue