feat!: print newlines properly

This commit is contained in:
Ryan 2025-03-18 17:24:59 -04:00
parent e3cdf4260e
commit 1c65a2b035
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
3 changed files with 49 additions and 27 deletions

View File

@ -12,27 +12,30 @@ pub async fn serve(
request: Request<Incoming>, request: Request<Incoming>,
state: State, state: State,
) -> Result<Response<BoxBody<Bytes, Error>>, Error> { ) -> Result<Response<BoxBody<Bytes, Error>>, 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()) { Ok(match (request.method(), request.uri().path()) {
(&Method::POST, "/reload") => { (&Method::POST, "/reload") => {
Response::new(full(format!("{:#?}", reload(&state.lua, None)))) Response::new(full(format!("{:#?}", reload(&state.lua, None))))
} }
(&Method::POST, "/eval") => handle_code!(eval), (&Method::POST, "/eval") => match eval(
(&Method::POST, "/exec") => handle_code!(exec), &state.lua,
&String::from_utf8_lossy(&request.into_body().collect().await?.to_bytes()),
None,
)
.await
{
Ok(value) => status_code_response(StatusCode::OK, full(value.to_string())),
Err(error) => status_code_response(StatusCode::BAD_REQUEST, full(error.to_string())),
},
(&Method::POST, "/exec") => match exec(
&state.lua,
&String::from_utf8_lossy(&request.into_body().collect().await?.to_bytes()),
None,
)
.await
{
Ok(()) => status_code_response(StatusCode::OK, empty()),
Err(error) => status_code_response(StatusCode::BAD_REQUEST, full(error.to_string())),
},
(&Method::GET, "/ping") => Response::new(full("pong!")), (&Method::GET, "/ping") => Response::new(full("pong!")),
_ => status_code_response(StatusCode::NOT_FOUND, empty()), _ => status_code_response(StatusCode::NOT_FOUND, empty()),
}) })

View File

@ -15,10 +15,12 @@ pub mod matrix;
use crate::{ListenerMap, build_info::built}; use crate::{ListenerMap, build_info::built};
use mlua::{Lua, Table}; use mlua::{Lua, Table};
use std::io; use std::{
fmt::{self, Display, Formatter},
io,
};
#[derive(Debug)] #[derive(Debug)]
#[allow(dead_code)]
pub enum Error { pub enum Error {
CreateEnv(mlua::Error), CreateEnv(mlua::Error),
EvalChunk(mlua::Error), EvalChunk(mlua::Error),
@ -28,6 +30,23 @@ pub enum Error {
ReadFile(io::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( pub fn register_globals(
lua: &Lua, lua: &Lua,
globals: &Table, globals: &Table,

View File

@ -49,16 +49,16 @@ pub async fn on_regular_room_message(
match split.0.unwrap_or(body).to_lowercase().as_str() { match split.0.unwrap_or(body).to_lowercase().as_str() {
"reload" => output = Some(format!("{:#?}", lua::reload(&ctx.state.lua, None))), "reload" => output = Some(format!("{:#?}", lua::reload(&ctx.state.lua, None))),
"eval" if let Some(code) = code => { "eval" if let Some(code) = code => {
output = Some(format!( output = Some(match lua::eval(&ctx.state.lua, code, None).await {
"{:#?}", Ok(value) => value.to_string(),
lua::eval(&ctx.state.lua, code, None).await Err(error) => error.to_string(),
)); });
} }
"exec" if let Some(code) = code => { "exec" if let Some(code) = code => {
output = Some(format!( output = Some(match lua::exec(&ctx.state.lua, code, None).await {
"{:#?}", Ok(()) => String::from("ok"),
lua::exec(&ctx.state.lua, code, None).await Err(error) => error.to_string(),
)); });
} }
"ping" => { "ping" => {
room.send(RoomMessageEventContent::text_plain("pong!")) room.send(RoomMessageEventContent::text_plain("pong!"))