feat!: display newlines properly
This commit is contained in:
parent
e3cdf4260e
commit
417a234cd2
@ -48,10 +48,10 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
|
||||
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<Mutex<CommandSource>>) {
|
||||
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<Mutex<CommandSource>>) {
|
||||
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
|
||||
})),
|
||||
|
41
src/http.rs
41
src/http.rs
@ -12,27 +12,28 @@ 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, "/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()),
|
||||
})
|
||||
|
@ -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,
|
||||
|
@ -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!"))
|
||||
|
Loading…
x
Reference in New Issue
Block a user