From 417a234cd263bbf420a8ab8f17d53e58208b80df Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Tue, 18 Mar 2025 17:24:59 -0400 Subject: [PATCH] feat!: display newlines properly --- src/commands.rs | 26 ++++++++++++++------------ src/http.rs | 41 +++++++++++++++++++++-------------------- src/lua/mod.rs | 23 +++++++++++++++++++++-- src/matrix/bot.rs | 27 +++++++++++++++++---------- 4 files changed, 73 insertions(+), 44 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index ecb40a3..9120b15 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -48,10 +48,10 @@ pub fn register(commands: &mut CommandDispatcher>) { let source = ctx.source.clone(); tokio::spawn(async move { let source = source.lock().await; - source.reply(&format!( - "{:?}", - reload(&source.state.lua, source.message.username()) - )); + source.reply( + &reload(&source.state.lua, source.message.username()) + .map_or_else(|error| error.to_string(), |()| String::from("ok")), + ); }); 1 })); @@ -62,10 +62,11 @@ pub fn register(commands: &mut CommandDispatcher>) { let code = get_string(ctx, "code").expect("argument should exist"); tokio::spawn(async move { let source = source.lock().await; - source.reply(&format!( - "{:?}", - eval(&source.state.lua, &code, source.message.username()).await - )); + source.reply( + &eval(&source.state.lua, &code, source.message.username()) + .await + .unwrap_or_else(|error| error.to_string()), + ); }); 1 })), @@ -77,10 +78,11 @@ pub fn register(commands: &mut CommandDispatcher>) { let code = get_string(ctx, "code").expect("argument should exist"); tokio::spawn(async move { let source = source.lock().await; - source.reply(&format!( - "{:?}", - exec(&source.state.lua, &code, source.message.username()).await - )); + source.reply( + &exec(&source.state.lua, &code, source.message.username()) + .await + .map_or_else(|error| error.to_string(), |()| String::from("ok")), + ); }); 1 })), diff --git a/src/http.rs b/src/http.rs index 759510e..39c60a8 100644 --- a/src/http.rs +++ b/src/http.rs @@ -12,27 +12,28 @@ pub async fn serve( request: Request, state: State, ) -> Result>, Error> { - macro_rules! handle_code { - ($handler:ident) => { - match std::str::from_utf8(&request.into_body().collect().await?.to_bytes()) { - Ok(code) => Response::new(full(format!( - "{:#?}", - $handler(&state.lua, code, None).await - ))), - Err(error) => status_code_response( - StatusCode::BAD_REQUEST, - full(format!("invalid utf-8 data received: {error:?}")), - ), - } - }; - } - Ok(match (request.method(), request.uri().path()) { - (&Method::POST, "/reload") => { - Response::new(full(format!("{:#?}", reload(&state.lua, None)))) - } - (&Method::POST, "/eval") => handle_code!(eval), - (&Method::POST, "/exec") => handle_code!(exec), + (&Method::POST, "/reload") => Response::new( + reload(&state.lua, None).map_or_else(|error| full(error.to_string()), |()| empty()), + ), + (&Method::POST, "/eval") => Response::new(full( + eval( + &state.lua, + &String::from_utf8_lossy(&request.into_body().collect().await?.to_bytes()), + None, + ) + .await + .unwrap_or_else(|error| error.to_string()), + )), + (&Method::POST, "/exec") => Response::new( + exec( + &state.lua, + &String::from_utf8_lossy(&request.into_body().collect().await?.to_bytes()), + None, + ) + .await + .map_or_else(|error| full(error.to_string()), |()| empty()), + ), (&Method::GET, "/ping") => Response::new(full("pong!")), _ => status_code_response(StatusCode::NOT_FOUND, empty()), }) diff --git a/src/lua/mod.rs b/src/lua/mod.rs index bf95bfc..a1508e0 100644 --- a/src/lua/mod.rs +++ b/src/lua/mod.rs @@ -15,10 +15,12 @@ pub mod matrix; use crate::{ListenerMap, build_info::built}; use mlua::{Lua, Table}; -use std::io; +use std::{ + fmt::{self, Display, Formatter}, + io, +}; #[derive(Debug)] -#[allow(dead_code)] pub enum Error { CreateEnv(mlua::Error), EvalChunk(mlua::Error), @@ -28,6 +30,23 @@ pub enum Error { ReadFile(io::Error), } +impl Display for Error { + fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result { + write!( + formatter, + "failed to {}", + match self { + Error::CreateEnv(error) => format!("create environment: {error}"), + Error::EvalChunk(error) => format!("evaluate chunk: {error}"), + Error::ExecChunk(error) => format!("execute chunk: {error}"), + Error::LoadChunk(error) => format!("load chunk: {error}"), + Error::MissingPath(error) => format!("get SCRIPT_PATH global: {error}"), + Error::ReadFile(error) => format!("read script file: {error}"), + } + ) + } +} + pub fn register_globals( lua: &Lua, globals: &Table, diff --git a/src/matrix/bot.rs b/src/matrix/bot.rs index cb47f8a..30c3e1a 100644 --- a/src/matrix/bot.rs +++ b/src/matrix/bot.rs @@ -1,7 +1,7 @@ use super::Context; use crate::{ events::call_listeners, - lua::{self, matrix::room::Room as LuaRoom}, + lua::{eval, exec, matrix::room::Room as LuaRoom, reload}, }; use anyhow::Result; use log::{debug, error}; @@ -47,18 +47,25 @@ pub async fn on_regular_room_message( let mut output = None; match split.0.unwrap_or(body).to_lowercase().as_str() { - "reload" => output = Some(format!("{:#?}", lua::reload(&ctx.state.lua, None))), + "reload" => { + output = Some( + reload(&ctx.state.lua, None) + .map_or_else(|error| error.to_string(), |()| String::from("ok")), + ); + } "eval" if let Some(code) = code => { - output = Some(format!( - "{:#?}", - lua::eval(&ctx.state.lua, code, None).await - )); + output = Some( + eval(&ctx.state.lua, code, None) + .await + .unwrap_or_else(|error| error.to_string()), + ); } "exec" if let Some(code) = code => { - output = Some(format!( - "{:#?}", - lua::exec(&ctx.state.lua, code, None).await - )); + output = Some( + exec(&ctx.state.lua, code, None) + .await + .map_or_else(|error| error.to_string(), |()| String::from("ok")), + ); } "ping" => { room.send(RoomMessageEventContent::text_plain("pong!"))