impl custom aliases

This commit is contained in:
highflowey 2024-08-23 03:48:28 +03:30
parent 2eb167025a
commit a633afc828
2 changed files with 15 additions and 7 deletions

View file

@ -1,4 +1,8 @@
use crate::{luaurc::path_to_alias, path::get_parent_path, LuneStandardLibrary};
use crate::{
luaurc::{path_to_alias, Luaurc},
path::get_parent_path,
LuneStandardLibrary,
};
use mlua::prelude::*;
use path::resolve_path;
use std::path::PathBuf;
@ -14,10 +18,14 @@ pub async fn lua_require(lua: &Lua, path: String) -> LuaResult<LuaMultiValue> {
if context::RequireContext::std_exists(lua, &require_alias.alias)? {
context::RequireContext::require_std(lua, require_alias)
} else {
Err(LuaError::runtime(format!(
"Tried requiring a custom alias '{}'\nbut aliases are not implemented yet.",
require_alias.alias,
)))
let require_path_abs = resolve_path(
&Luaurc::resolve_path(lua, &require_alias)
.await
.into_lua_err()?,
)
.await?;
context::RequireContext::require(lua, require_path_rel, require_path_abs).await
}
} else {
let parent_path = get_parent_path(lua)?;

View file

@ -67,7 +67,7 @@ async fn parse_luaurc(_: &mlua::Lua, path: &PathBuf) -> Result<Option<Luaurc>, L
let content = fs::read(path).await?;
serde_json::from_slice(&content)
.map(Some)
.map_err(|x| x.into())
.map_err(std::convert::Into::into)
} else {
Ok(None)
}
@ -86,7 +86,7 @@ impl Luaurc {
for path in ancestors {
if path.starts_with(&cwd) {
if let Some(luaurc) = parse_luaurc(lua, &parent.join(".luaurc")).await? {
if let Some(luaurc) = parse_luaurc(lua, &path.join(".luaurc")).await? {
if let Some(aliases) = luaurc.aliases {
if let Some(alias_path) = aliases.get(&alias.alias) {
let resolved = path.join(alias_path.join(&alias.path));