diff --git a/src/lune/builtins/fs.rs b/src/lune/builtins/fs.rs index 8c4a081..71fc131 100644 --- a/src/lune/builtins/fs.rs +++ b/src/lune/builtins/fs.rs @@ -26,14 +26,14 @@ pub fn create(lua: &'static Lua) -> LuaResult { } async fn fs_read_file(lua: &'static Lua, path: String) -> LuaResult { - let bytes = fs::read(&path).await.map_err(LuaError::external)?; + let bytes = fs::read(&path).await.into_lua_err()?; lua.create_string(bytes) } async fn fs_read_dir(_: &'static Lua, path: String) -> LuaResult> { let mut dir_strings = Vec::new(); - let mut dir = fs::read_dir(&path).await.map_err(LuaError::external)?; - while let Some(dir_entry) = dir.next_entry().await.map_err(LuaError::external)? { + let mut dir = fs::read_dir(&path).await.into_lua_err()?; + while let Some(dir_entry) = dir.next_entry().await.into_lua_err()? { if let Some(dir_path_str) = dir_entry.path().to_str() { dir_strings.push(dir_path_str.to_owned()); } else { @@ -63,21 +63,19 @@ async fn fs_write_file( _: &'static Lua, (path, contents): (String, LuaString<'_>), ) -> LuaResult<()> { - fs::write(&path, &contents.as_bytes()) - .await - .map_err(LuaError::external) + fs::write(&path, &contents.as_bytes()).await.into_lua_err() } async fn fs_write_dir(_: &'static Lua, path: String) -> LuaResult<()> { - fs::create_dir_all(&path).await.map_err(LuaError::external) + fs::create_dir_all(&path).await.into_lua_err() } async fn fs_remove_file(_: &'static Lua, path: String) -> LuaResult<()> { - fs::remove_file(&path).await.map_err(LuaError::external) + fs::remove_file(&path).await.into_lua_err() } async fn fs_remove_dir(_: &'static Lua, path: String) -> LuaResult<()> { - fs::remove_dir_all(&path).await.map_err(LuaError::external) + fs::remove_dir_all(&path).await.into_lua_err() } async fn fs_metadata(_: &'static Lua, path: String) -> LuaResult { @@ -122,9 +120,7 @@ async fn fs_move( path_to.display() ))); } - fs::rename(path_from, path_to) - .await - .map_err(LuaError::external)?; + fs::rename(path_from, path_to).await.into_lua_err()?; Ok(()) } diff --git a/src/lune/builtins/net.rs b/src/lune/builtins/net.rs index 358d5ce..8bb7707 100644 --- a/src/lune/builtins/net.rs +++ b/src/lune/builtins/net.rs @@ -73,7 +73,7 @@ async fn net_request<'a>(lua: &'static Lua, config: RequestConfig<'a>) -> LuaRes .body(config.body.unwrap_or_default()) .send() .await - .map_err(LuaError::external)?; + .into_lua_err()?; // Extract status, headers let res_status = res.status().as_u16(); let res_status_text = res.status().canonical_reason(); @@ -88,7 +88,7 @@ async fn net_request<'a>(lua: &'static Lua, config: RequestConfig<'a>) -> LuaRes }) .collect::>(); // Read response bytes - let mut res_bytes = res.bytes().await.map_err(LuaError::external)?.to_vec(); + let mut res_bytes = res.bytes().await.into_lua_err()?.to_vec(); // Check for extra options, decompression if config.options.decompress { // NOTE: Header names are guaranteed to be lowercase because of the above @@ -120,9 +120,7 @@ async fn net_request<'a>(lua: &'static Lua, config: RequestConfig<'a>) -> LuaRes } async fn net_socket<'a>(lua: &'static Lua, url: String) -> LuaResult { - let (ws, _) = tokio_tungstenite::connect_async(url) - .await - .map_err(LuaError::external)?; + let (ws, _) = tokio_tungstenite::connect_async(url).await.into_lua_err()?; NetWebSocket::new(ws).into_lua_table(lua) } diff --git a/src/lune/builtins/roblox.rs b/src/lune/builtins/roblox.rs index 7c19386..2141d5c 100644 --- a/src/lune/builtins/roblox.rs +++ b/src/lune/builtins/roblox.rs @@ -41,7 +41,7 @@ async fn deserialize_place<'lua>( let data_model = doc.into_data_model_instance()?; Ok::<_, DocumentError>(data_model) }); - fut.await.map_err(LuaError::external)??.into_lua(lua) + fut.await.into_lua_err()??.into_lua(lua) } async fn deserialize_model<'lua>( @@ -54,7 +54,7 @@ async fn deserialize_model<'lua>( let instance_array = doc.into_instance_array()?; Ok::<_, DocumentError>(instance_array) }); - fut.await.map_err(LuaError::external)??.into_lua(lua) + fut.await.into_lua_err()??.into_lua(lua) } async fn serialize_place<'lua>( @@ -70,7 +70,7 @@ async fn serialize_place<'lua>( })?; Ok::<_, DocumentError>(bytes) }); - let bytes = fut.await.map_err(LuaError::external)??; + let bytes = fut.await.into_lua_err()??; lua.create_string(bytes) } @@ -87,7 +87,7 @@ async fn serialize_model<'lua>( })?; Ok::<_, DocumentError>(bytes) }); - let bytes = fut.await.map_err(LuaError::external)??; + let bytes = fut.await.into_lua_err()??; lua.create_string(bytes) } diff --git a/src/lune/builtins/stdio.rs b/src/lune/builtins/stdio.rs index 48b12c7..acd3727 100644 --- a/src/lune/builtins/stdio.rs +++ b/src/lune/builtins/stdio.rs @@ -43,7 +43,7 @@ pub fn create(lua: &'static Lua) -> LuaResult { .with_async_function("prompt", |_, options: PromptOptions| async move { task::spawn_blocking(move || prompt(options)) .await - .map_err(LuaError::external)? + .into_lua_err()? })? .build_readonly() } diff --git a/src/lune/importer/require.rs b/src/lune/importer/require.rs index 20048c6..51588e4 100644 --- a/src/lune/importer/require.rs +++ b/src/lune/importer/require.rs @@ -202,7 +202,7 @@ async fn load_file<'lua>( } // Try to read the wanted file, note that we use bytes instead of reading // to a string since lua scripts are not necessarily valid utf-8 strings - let contents = fs::read(&absolute_path).await.map_err(LuaError::external)?; + let contents = fs::read(&absolute_path).await.into_lua_err()?; // Use a name without extensions for loading the chunk, some // other code assumes the require path is without extensions let path_relative_no_extension = relative_path diff --git a/src/lune/lua/net/client.rs b/src/lune/lua/net/client.rs index 6381f63..2120c58 100644 --- a/src/lune/lua/net/client.rs +++ b/src/lune/lua/net/client.rs @@ -23,8 +23,8 @@ impl NetClientBuilder { { let mut map = HeaderMap::new(); for (key, val) in headers { - let hkey = HeaderName::from_str(key.as_ref()).map_err(LuaError::external)?; - let hval = HeaderValue::from_bytes(val.as_ref()).map_err(LuaError::external)?; + let hkey = HeaderName::from_str(key.as_ref()).into_lua_err()?; + let hval = HeaderValue::from_bytes(val.as_ref()).into_lua_err()?; map.insert(hkey, hval); } self.builder = self.builder.default_headers(map); @@ -32,7 +32,7 @@ impl NetClientBuilder { } pub fn build(self) -> LuaResult { - let client = self.builder.build().map_err(LuaError::external)?; + let client = self.builder.build().into_lua_err()?; Ok(NetClient(client)) } } diff --git a/src/lune/lua/net/response.rs b/src/lune/lua/net/response.rs index 2e94e24..fa2e748 100644 --- a/src/lune/lua/net/response.rs +++ b/src/lune/lua/net/response.rs @@ -24,7 +24,7 @@ impl NetServeResponse { .status(200) .header("Content-Type", "text/plain") .body(Body::from(self.body.unwrap())) - .map_err(LuaError::external)?, + .into_lua_err()?, NetServeResponseKind::Table => { let mut response = Response::builder(); for (key, value) in self.headers { @@ -33,7 +33,7 @@ impl NetServeResponse { response .status(self.status) .body(Body::from(self.body.unwrap_or_default())) - .map_err(LuaError::external)? + .into_lua_err()? } }) } diff --git a/src/lune/lua/net/server.rs b/src/lune/lua/net/server.rs index 236c2ae..d3428df 100644 --- a/src/lune/lua/net/server.rs +++ b/src/lune/lua/net/server.rs @@ -57,7 +57,7 @@ impl Service> for NetServiceInner { task::spawn_local(async move { // Create our new full websocket object, then // schedule our handler to get called asap - let ws = ws.await.map_err(LuaError::external)?; + let ws = ws.await.into_lua_err()?; let sock = NetWebSocket::new(ws).into_lua_table(lua)?; let sched = lua .app_data_ref::<&TaskScheduler>() @@ -77,7 +77,7 @@ impl Service> for NetServiceInner { let (parts, body) = req.into_parts(); Box::pin(async move { // Convert request body into bytes, extract handler - let bytes = to_bytes(body).await.map_err(LuaError::external)?; + let bytes = to_bytes(body).await.into_lua_err()?; let handler: LuaFunction = lua.registry_value(&key)?; // Create a readonly table for the request query params let query_params = TableBuilder::new(lua)? diff --git a/src/lune/lua/net/websocket.rs b/src/lune/lua/net/websocket.rs index 2f613a9..1057b1b 100644 --- a/src/lune/lua/net/websocket.rs +++ b/src/lune/lua/net/websocket.rs @@ -167,10 +167,10 @@ where reason: "".into(), }))) .await - .map_err(LuaError::external)?; + .into_lua_err()?; let res = ws.close(); - res.await.map_err(LuaError::external) + res.await.into_lua_err() } async fn send<'lua, T>( @@ -187,11 +187,11 @@ where let msg = if matches!(as_binary, Some(true)) { WsMessage::Binary(string.as_bytes().to_vec()) } else { - let s = string.to_str().map_err(LuaError::external)?; + let s = string.to_str().into_lua_err()?; WsMessage::Text(s.to_string()) }; let mut ws = socket.write_stream.lock().await; - ws.send(msg).await.map_err(LuaError::external) + ws.send(msg).await.into_lua_err() } async fn next<'lua, T>( @@ -202,7 +202,7 @@ where T: AsyncRead + AsyncWrite + Unpin, { let mut ws = socket.read_stream.lock().await; - let item = ws.next().await.transpose().map_err(LuaError::external); + let item = ws.next().await.transpose().into_lua_err(); let msg = match item { Ok(Some(WsMessage::Close(msg))) => { if let Some(msg) = &msg { diff --git a/src/lune/lua/process/mod.rs b/src/lune/lua/process/mod.rs index c4274bd..b74cd00 100644 --- a/src/lune/lua/process/mod.rs +++ b/src/lune/lua/process/mod.rs @@ -24,9 +24,7 @@ pub async fn pipe_and_inherit_child_process_stdio( let mut stdout = io::stdout(); let mut tee = AsyncTeeWriter::new(&mut stdout); - io::copy(&mut child_stdout, &mut tee) - .await - .map_err(LuaError::external)?; + io::copy(&mut child_stdout, &mut tee).await.into_lua_err()?; Ok::<_, LuaError>(tee.into_vec()) }); @@ -35,9 +33,7 @@ pub async fn pipe_and_inherit_child_process_stdio( let mut stderr = io::stderr(); let mut tee = AsyncTeeWriter::new(&mut stderr); - io::copy(&mut child_stderr, &mut tee) - .await - .map_err(LuaError::external)?; + io::copy(&mut child_stderr, &mut tee).await.into_lua_err()?; Ok::<_, LuaError>(tee.into_vec()) }); diff --git a/src/lune/lua/serde/compress_decompress.rs b/src/lune/lua/serde/compress_decompress.rs index 528847b..85e7ac5 100644 --- a/src/lune/lua/serde/compress_decompress.rs +++ b/src/lune/lua/serde/compress_decompress.rs @@ -102,7 +102,7 @@ pub async fn compress<'lua>( let source = source.as_ref().to_vec(); return task::spawn_blocking(move || compress_prepend_size(&source)) .await - .map_err(LuaError::external); + .into_lua_err(); } let mut bytes = Vec::new(); @@ -135,8 +135,8 @@ pub async fn decompress<'lua>( let source = source.as_ref().to_vec(); return task::spawn_blocking(move || decompress_size_prepended(&source)) .await - .map_err(LuaError::external)? - .map_err(LuaError::external); + .into_lua_err()? + .into_lua_err(); } let mut bytes = Vec::new(); diff --git a/src/lune/lua/serde/encode_decode.rs b/src/lune/lua/serde/encode_decode.rs index 77711e3..edbcc1b 100644 --- a/src/lune/lua/serde/encode_decode.rs +++ b/src/lune/lua/serde/encode_decode.rs @@ -61,23 +61,23 @@ impl EncodeDecodeConfig { EncodeDecodeFormat::Json => { let serialized: JsonValue = lua.from_value_with(value, LUA_DESERIALIZE_OPTIONS)?; if self.pretty { - serde_json::to_vec_pretty(&serialized).map_err(LuaError::external)? + serde_json::to_vec_pretty(&serialized).into_lua_err()? } else { - serde_json::to_vec(&serialized).map_err(LuaError::external)? + serde_json::to_vec(&serialized).into_lua_err()? } } EncodeDecodeFormat::Yaml => { let serialized: YamlValue = lua.from_value_with(value, LUA_DESERIALIZE_OPTIONS)?; let mut writer = Vec::with_capacity(128); - serde_yaml::to_writer(&mut writer, &serialized).map_err(LuaError::external)?; + serde_yaml::to_writer(&mut writer, &serialized).into_lua_err()?; writer } EncodeDecodeFormat::Toml => { let serialized: TomlValue = lua.from_value_with(value, LUA_DESERIALIZE_OPTIONS)?; let s = if self.pretty { - toml::to_string_pretty(&serialized).map_err(LuaError::external)? + toml::to_string_pretty(&serialized).into_lua_err()? } else { - toml::to_string(&serialized).map_err(LuaError::external)? + toml::to_string(&serialized).into_lua_err()? }; s.as_bytes().to_vec() } @@ -93,16 +93,16 @@ impl EncodeDecodeConfig { let bytes = string.as_bytes(); match self.format { EncodeDecodeFormat::Json => { - let value: JsonValue = serde_json::from_slice(bytes).map_err(LuaError::external)?; + let value: JsonValue = serde_json::from_slice(bytes).into_lua_err()?; lua.to_value_with(&value, LUA_SERIALIZE_OPTIONS) } EncodeDecodeFormat::Yaml => { - let value: YamlValue = serde_yaml::from_slice(bytes).map_err(LuaError::external)?; + let value: YamlValue = serde_yaml::from_slice(bytes).into_lua_err()?; lua.to_value_with(&value, LUA_SERIALIZE_OPTIONS) } EncodeDecodeFormat::Toml => { if let Ok(s) = string.to_str() { - let value: TomlValue = toml::from_str(s).map_err(LuaError::external)?; + let value: TomlValue = toml::from_str(s).into_lua_err()?; lua.to_value_with(&value, LUA_SERIALIZE_OPTIONS) } else { Err(LuaError::RuntimeError( diff --git a/src/lune/lua/stdio/formatting.rs b/src/lune/lua/stdio/formatting.rs index d51a83e..8437299 100644 --- a/src/lune/lua/stdio/formatting.rs +++ b/src/lune/lua/stdio/formatting.rs @@ -197,12 +197,12 @@ pub fn pretty_format_multi_value(multi: &LuaMultiValue) -> LuaResult { for value in multi { counter += 1; if let LuaValue::String(s) = value { - write!(buffer, "{}", s.to_string_lossy()).map_err(LuaError::external)?; + write!(buffer, "{}", s.to_string_lossy()).into_lua_err()?; } else { - pretty_format_value(&mut buffer, value, 0).map_err(LuaError::external)?; + pretty_format_value(&mut buffer, value, 0).into_lua_err()?; } if counter < multi.len() { - write!(&mut buffer, " ").map_err(LuaError::external)?; + write!(&mut buffer, " ").into_lua_err()?; } } Ok(buffer)