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::{ use crate::{
luaurc::{path_to_alias, Luaurc}, luaurc::{Luaurc, RequireAlias},
path::get_parent_path, path::get_parent_path,
LuneStandardLibrary, LuneStandardLibrary,
}; };
@ -40,7 +40,7 @@ pub enum RequireError {
async fn lua_require(lua: &Lua, path: String) -> LuaResult<LuaMultiValue> { async fn lua_require(lua: &Lua, path: String) -> LuaResult<LuaMultiValue> {
let require_path_rel = PathBuf::from(path); 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 let Some(require_alias) = require_alias {
if context::RequireContext::std_exists(lua, &require_alias.alias).into_lua_err()? { if context::RequireContext::std_exists(lua, &require_alias.alias).into_lua_err()? {

View file

@ -36,29 +36,31 @@ pub enum LuaurcError {
LuaError(#[from] mlua::Error), LuaError(#[from] mlua::Error),
} }
/// Parses path into `RequireAlias` struct impl RequireAlias {
/// /// Parses path into `RequireAlias` struct
/// ### Examples ///
/// /// ### Examples
/// `@lune/task` becomes `Some({ alias: "lune", path: "task" })` ///
/// /// `@lune/task` becomes `Some({ alias: "lune", path: "task" })`
/// `../path/script` becomes `None` ///
pub fn path_to_alias(path: &Path) -> Result<Option<RequireAlias>, LuaurcError> { /// `../path/script` becomes `None`
if let Some(aliased_path) = path pub fn from_path(path: &Path) -> Result<Option<Self>, LuaurcError> {
.to_str() if let Some(aliased_path) = path
.ok_or(LuaurcError::FailedStringToPathConversion)? .to_str()
.strip_prefix('@') .ok_or(LuaurcError::FailedStringToPathConversion)?
{ .strip_prefix('@')
let (alias, path) = aliased_path {
.split_once('/') let (alias, path) = aliased_path
.ok_or(LuaurcError::UsedAliasWithoutSlash)?; .split_once('/')
.ok_or(LuaurcError::UsedAliasWithoutSlash)?;
Ok(Some(RequireAlias { Ok(Some(RequireAlias {
alias: alias.to_string(), alias: alias.to_string(),
path: path.to_string(), path: path.to_string(),
})) }))
} else { } else {
Ok(None) Ok(None)
}
} }
} }