move path_to_alias to RequireAlias::from_path

This commit is contained in:
highflowey 2024-08-30 20:38:51 +03:30
parent c6a1808e71
commit a7f9c5f4f9
2 changed files with 26 additions and 24 deletions

View file

@ -1,5 +1,5 @@
use crate::{
luaurc::{path_to_alias, Luaurc},
luaurc::{Luaurc, RequireAlias},
path::get_parent_path,
LuneStandardLibrary,
};
@ -40,7 +40,7 @@ pub enum RequireError {
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).into_lua_err()?;
let require_alias = RequireAlias::from_path(&require_path_rel).into_lua_err()?;
if let Some(require_alias) = require_alias {
if context::RequireContext::std_exists(lua, &require_alias.alias).into_lua_err()? {

View file

@ -36,6 +36,7 @@ pub enum LuaurcError {
LuaError(#[from] mlua::Error),
}
impl RequireAlias {
/// Parses path into `RequireAlias` struct
///
/// ### Examples
@ -43,7 +44,7 @@ pub enum LuaurcError {
/// `@lune/task` becomes `Some({ alias: "lune", path: "task" })`
///
/// `../path/script` becomes `None`
pub fn path_to_alias(path: &Path) -> Result<Option<RequireAlias>, LuaurcError> {
pub fn from_path(path: &Path) -> Result<Option<Self>, LuaurcError> {
if let Some(aliased_path) = path
.to_str()
.ok_or(LuaurcError::FailedStringToPathConversion)?
@ -61,6 +62,7 @@ pub fn path_to_alias(path: &Path) -> Result<Option<RequireAlias>, LuaurcError> {
Ok(None)
}
}
}
async fn parse_luaurc(_: &mlua::Lua, path: &PathBuf) -> Result<Option<Luaurc>, LuaurcError> {
if fs::try_exists(path).await? {