feat!: print newlines properly

This commit is contained in:
Ryan 2025-03-18 17:24:59 -04:00
parent 8494c35d1c
commit d89afd18dc
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>,
state: State,
) -> 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()) {
(&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, "/eval") => match eval(
&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!")),
_ => status_code_response(StatusCode::NOT_FOUND, empty()),
})

View File

@ -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,
"{}",
match self {
Error::CreateEnv(error) => format!("failed to create environment: {error}"),
Error::EvalChunk(error) => format!("failed to evaluate chunk: {error}"),
Error::ExecChunk(error) => format!("failed to execute chunk: {error}"),
Error::LoadChunk(error) => format!("failed to load chunk: {error}"),
Error::MissingPath(error) => format!("failed to get SCRIPT_PATH global: {error}"),
Error::ReadFile(error) => format!("failed to read script file: {error}"),
}
)
}
}
pub fn register_globals(
lua: &Lua,
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() {
"reload" => output = Some(format!("{:#?}", lua::reload(&ctx.state.lua, None))),
"eval" if let Some(code) = code => {
output = Some(format!(
"{:#?}",
lua::eval(&ctx.state.lua, code, None).await
));
output = Some(match lua::eval(&ctx.state.lua, code, None).await {
Ok(value) => value.to_string(),
Err(error) => error.to_string(),
});
}
"exec" if let Some(code) = code => {
output = Some(format!(
"{:#?}",
lua::exec(&ctx.state.lua, code, None).await
));
output = Some(match lua::exec(&ctx.state.lua, code, None).await {
Ok(()) => String::from("ok"),
Err(error) => error.to_string(),
});
}
"ping" => {
room.send(RoomMessageEventContent::text_plain("pong!"))