mirror of
https://github.com/lune-org/lune.git
synced 2025-04-04 10:30:54 +01:00
move path_to_alias to RequireAlias::from_path
This commit is contained in:
parent
c6a1808e71
commit
a7f9c5f4f9
2 changed files with 26 additions and 24 deletions
|
@ -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()? {
|
||||
|
|
|
@ -36,29 +36,31 @@ pub enum LuaurcError {
|
|||
LuaError(#[from] mlua::Error),
|
||||
}
|
||||
|
||||
/// Parses path into `RequireAlias` struct
|
||||
///
|
||||
/// ### Examples
|
||||
///
|
||||
/// `@lune/task` becomes `Some({ alias: "lune", path: "task" })`
|
||||
///
|
||||
/// `../path/script` becomes `None`
|
||||
pub fn path_to_alias(path: &Path) -> Result<Option<RequireAlias>, LuaurcError> {
|
||||
if let Some(aliased_path) = path
|
||||
.to_str()
|
||||
.ok_or(LuaurcError::FailedStringToPathConversion)?
|
||||
.strip_prefix('@')
|
||||
{
|
||||
let (alias, path) = aliased_path
|
||||
.split_once('/')
|
||||
.ok_or(LuaurcError::UsedAliasWithoutSlash)?;
|
||||
impl RequireAlias {
|
||||
/// Parses path into `RequireAlias` struct
|
||||
///
|
||||
/// ### Examples
|
||||
///
|
||||
/// `@lune/task` becomes `Some({ alias: "lune", path: "task" })`
|
||||
///
|
||||
/// `../path/script` becomes `None`
|
||||
pub fn from_path(path: &Path) -> Result<Option<Self>, LuaurcError> {
|
||||
if let Some(aliased_path) = path
|
||||
.to_str()
|
||||
.ok_or(LuaurcError::FailedStringToPathConversion)?
|
||||
.strip_prefix('@')
|
||||
{
|
||||
let (alias, path) = aliased_path
|
||||
.split_once('/')
|
||||
.ok_or(LuaurcError::UsedAliasWithoutSlash)?;
|
||||
|
||||
Ok(Some(RequireAlias {
|
||||
alias: alias.to_string(),
|
||||
path: path.to_string(),
|
||||
}))
|
||||
} else {
|
||||
Ok(None)
|
||||
Ok(Some(RequireAlias {
|
||||
alias: alias.to_string(),
|
||||
path: path.to_string(),
|
||||
}))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue