From 2eb167025a59f688ffff1c12a21374400f2b3fb1 Mon Sep 17 00:00:00 2001 From: highflowey Date: Fri, 23 Aug 2024 03:36:38 +0330 Subject: [PATCH] luaurc error impl --- Cargo.lock | 1 + crates/lune-std/Cargo.toml | 2 ++ crates/lune-std/src/globals/require/mod.rs | 2 +- crates/lune-std/src/luaurc.rs | 42 +++++++++++++++------- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 59d32a9..d316fca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1571,6 +1571,7 @@ dependencies = [ "mlua-luau-scheduler", "serde", "serde_json", + "thiserror", "tokio", ] diff --git a/crates/lune-std/Cargo.toml b/crates/lune-std/Cargo.toml index 07762b6..f6901e8 100644 --- a/crates/lune-std/Cargo.toml +++ b/crates/lune-std/Cargo.toml @@ -45,6 +45,8 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" tokio = { version = "1", default-features = false, features = ["fs", "sync"] } +thiserror = "1.0.63" + lune-utils = { version = "0.1.3", path = "../lune-utils" } lune-std-datetime = { optional = true, version = "0.1.3", path = "../lune-std-datetime" } diff --git a/crates/lune-std/src/globals/require/mod.rs b/crates/lune-std/src/globals/require/mod.rs index 1bc98fd..7472310 100644 --- a/crates/lune-std/src/globals/require/mod.rs +++ b/crates/lune-std/src/globals/require/mod.rs @@ -8,7 +8,7 @@ mod path; pub async fn lua_require(lua: &Lua, path: String) -> LuaResult { let require_path_rel = PathBuf::from(path); - let require_alias = path_to_alias(&require_path_rel)?; + let require_alias = path_to_alias(&require_path_rel).into_lua_err()?; if let Some(require_alias) = require_alias { if context::RequireContext::std_exists(lua, &require_alias.alias)? { diff --git a/crates/lune-std/src/luaurc.rs b/crates/lune-std/src/luaurc.rs index b9d5d4b..95c7320 100644 --- a/crates/lune-std/src/luaurc.rs +++ b/crates/lune-std/src/luaurc.rs @@ -1,11 +1,11 @@ use crate::path::get_parent_path; -use mlua::ExternalResult; use serde::Deserialize; use std::{ collections::HashMap, env::current_dir, path::{Path, PathBuf}, }; +use thiserror::Error; use tokio::fs; #[derive(Debug, Clone, Eq, Hash, PartialEq)] @@ -19,6 +19,23 @@ pub struct Luaurc { aliases: Option>, } +#[derive(Debug, Error)] +pub enum LuaurcError { + #[error("Require with alias doesn't contain '/'")] + UsedAliasWithoutSlash, + #[error("Failed to convert string to path")] + FailedStringToPathConversion, + #[error("Failed to find a path for alias '{0}' in .luaurc files")] + FailedToFindAlias(String), + + #[error("IOError: {0}")] + IOError(#[from] std::io::Error), + #[error("JsonError: {0}")] + JsonError(#[from] serde_json::Error), + #[error("LuaError: {0}")] + LuaError(#[from] mlua::Error), +} + /// Parses path into `RequireAlias` struct /// /// ### Examples @@ -26,15 +43,15 @@ pub struct Luaurc { /// `@lune/task` becomes `Some({ alias: "lune", path: "task" })` /// /// `../path/script` becomes `None` -pub fn path_to_alias(path: &Path) -> Result, mlua::Error> { +pub fn path_to_alias(path: &Path) -> Result, LuaurcError> { if let Some(aliased_path) = path .to_str() - .ok_or(mlua::Error::runtime("Couldn't turn path into string"))? + .ok_or(LuaurcError::FailedStringToPathConversion)? .strip_prefix('@') { - let (alias, path) = aliased_path.split_once('/').ok_or(mlua::Error::runtime( - "Require with alias doesn't contain '/'", - ))?; + let (alias, path) = aliased_path + .split_once('/') + .ok_or(LuaurcError::UsedAliasWithoutSlash)?; Ok(Some(RequireAlias { alias: alias.to_string(), @@ -45,10 +62,12 @@ pub fn path_to_alias(path: &Path) -> Result, mlua::Error> { } } -async fn parse_luaurc(_: &mlua::Lua, path: &PathBuf) -> Result, mlua::Error> { +async fn parse_luaurc(_: &mlua::Lua, path: &PathBuf) -> Result, LuaurcError> { if fs::try_exists(path).await? { let content = fs::read(path).await?; - serde_json::from_slice(&content).map(Some).into_lua_err() + serde_json::from_slice(&content) + .map(Some) + .map_err(|x| x.into()) } else { Ok(None) } @@ -60,7 +79,7 @@ impl Luaurc { pub async fn resolve_path<'lua>( lua: &'lua mlua::Lua, alias: &'lua RequireAlias, - ) -> Result { + ) -> Result { let cwd = current_dir()?; let parent = cwd.join(get_parent_path(lua)?); let ancestors = parent.ancestors(); @@ -81,9 +100,6 @@ impl Luaurc { } } - Err(mlua::Error::runtime(format!( - "Coudln't find the alias '{}' in any .luaurc file", - alias.alias - ))) + Err(LuaurcError::FailedToFindAlias(alias.alias.to_string())) } }