feat!: display newlines properly
This commit is contained in:
		| @@ -48,10 +48,10 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { | |||||||
|         let source = ctx.source.clone(); |         let source = ctx.source.clone(); | ||||||
|         tokio::spawn(async move { |         tokio::spawn(async move { | ||||||
|             let source = source.lock().await; |             let source = source.lock().await; | ||||||
|             source.reply(&format!( |             source.reply( | ||||||
|                 "{:?}", |                 &reload(&source.state.lua, source.message.username()) | ||||||
|                 reload(&source.state.lua, source.message.username()) |                     .map_or_else(|error| error.to_string(), |()| String::from("ok")), | ||||||
|             )); |             ); | ||||||
|         }); |         }); | ||||||
|         1 |         1 | ||||||
|     })); |     })); | ||||||
| @@ -62,10 +62,11 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { | |||||||
|             let code = get_string(ctx, "code").expect("argument should exist"); |             let code = get_string(ctx, "code").expect("argument should exist"); | ||||||
|             tokio::spawn(async move { |             tokio::spawn(async move { | ||||||
|                 let source = source.lock().await; |                 let source = source.lock().await; | ||||||
|                 source.reply(&format!( |                 source.reply( | ||||||
|                     "{:?}", |                     &eval(&source.state.lua, &code, source.message.username()) | ||||||
|                     eval(&source.state.lua, &code, source.message.username()).await |                         .await | ||||||
|                 )); |                         .unwrap_or_else(|error| error.to_string()), | ||||||
|  |                 ); | ||||||
|             }); |             }); | ||||||
|             1 |             1 | ||||||
|         })), |         })), | ||||||
| @@ -77,10 +78,11 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) { | |||||||
|             let code = get_string(ctx, "code").expect("argument should exist"); |             let code = get_string(ctx, "code").expect("argument should exist"); | ||||||
|             tokio::spawn(async move { |             tokio::spawn(async move { | ||||||
|                 let source = source.lock().await; |                 let source = source.lock().await; | ||||||
|                 source.reply(&format!( |                 source.reply( | ||||||
|                     "{:?}", |                     &exec(&source.state.lua, &code, source.message.username()) | ||||||
|                     exec(&source.state.lua, &code, source.message.username()).await |                         .await | ||||||
|                 )); |                         .map_or_else(|error| error.to_string(), |()| String::from("ok")), | ||||||
|  |                 ); | ||||||
|             }); |             }); | ||||||
|             1 |             1 | ||||||
|         })), |         })), | ||||||
|   | |||||||
							
								
								
									
										41
									
								
								src/http.rs
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								src/http.rs
									
									
									
									
									
								
							| @@ -12,27 +12,28 @@ 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( | ||||||
|             Response::new(full(format!("{:#?}", reload(&state.lua, None)))) |             reload(&state.lua, None).map_or_else(|error| full(error.to_string()), |()| empty()), | ||||||
|         } |         ), | ||||||
|         (&Method::POST, "/eval") => handle_code!(eval), |         (&Method::POST, "/eval") => Response::new(full( | ||||||
|         (&Method::POST, "/exec") => handle_code!(exec), |             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!")), |         (&Method::GET, "/ping") => Response::new(full("pong!")), | ||||||
|         _ => status_code_response(StatusCode::NOT_FOUND, empty()), |         _ => status_code_response(StatusCode::NOT_FOUND, empty()), | ||||||
|     }) |     }) | ||||||
|   | |||||||
| @@ -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, | ||||||
|   | |||||||
| @@ -1,7 +1,7 @@ | |||||||
| use super::Context; | use super::Context; | ||||||
| use crate::{ | use crate::{ | ||||||
|     events::call_listeners, |     events::call_listeners, | ||||||
|     lua::{self, matrix::room::Room as LuaRoom}, |     lua::{eval, exec, matrix::room::Room as LuaRoom, reload}, | ||||||
| }; | }; | ||||||
| use anyhow::Result; | use anyhow::Result; | ||||||
| use log::{debug, error}; | use log::{debug, error}; | ||||||
| @@ -47,18 +47,25 @@ pub async fn on_regular_room_message( | |||||||
|  |  | ||||||
|         let mut output = None; |         let mut output = None; | ||||||
|         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( | ||||||
|  |                     reload(&ctx.state.lua, None) | ||||||
|  |                         .map_or_else(|error| error.to_string(), |()| String::from("ok")), | ||||||
|  |                 ); | ||||||
|  |             } | ||||||
|             "eval" if let Some(code) = code => { |             "eval" if let Some(code) = code => { | ||||||
|                 output = Some(format!( |                 output = Some( | ||||||
|                     "{:#?}", |                     eval(&ctx.state.lua, code, None) | ||||||
|                     lua::eval(&ctx.state.lua, code, None).await |                         .await | ||||||
|                 )); |                         .unwrap_or_else(|error| error.to_string()), | ||||||
|  |                 ); | ||||||
|             } |             } | ||||||
|             "exec" if let Some(code) = code => { |             "exec" if let Some(code) = code => { | ||||||
|                 output = Some(format!( |                 output = Some( | ||||||
|                     "{:#?}", |                     exec(&ctx.state.lua, code, None) | ||||||
|                     lua::exec(&ctx.state.lua, code, None).await |                         .await | ||||||
|                 )); |                         .map_or_else(|error| error.to_string(), |()| String::from("ok")), | ||||||
|  |                 ); | ||||||
|             } |             } | ||||||
|             "ping" => { |             "ping" => { | ||||||
|                 room.send(RoomMessageEventContent::text_plain("pong!")) |                 room.send(RoomMessageEventContent::text_plain("pong!")) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user