chore: catch err, move out helpers
This commit is contained in:
parent
fc76c653d1
commit
97c66aeb3b
3 changed files with 33 additions and 3 deletions
|
@ -2,7 +2,7 @@ use mlua::{Lua, MultiValue};
|
||||||
use tokio::{sync::{mpsc, broadcast}, net::{TcpListener, TcpStream}, io::{AsyncWriteExt, AsyncReadExt}};
|
use tokio::{sync::{mpsc, broadcast}, net::{TcpListener, TcpStream}, io::{AsyncWriteExt, AsyncReadExt}};
|
||||||
use tracing::{debug, error};
|
use tracing::{debug, error};
|
||||||
|
|
||||||
use crate::runtime::{prepare_lua_runtime, pretty_lua};
|
use crate::{runtime::register_builtin_fn, helpers::pretty_lua};
|
||||||
|
|
||||||
|
|
||||||
pub struct ControlChannel {
|
pub struct ControlChannel {
|
||||||
|
@ -50,7 +50,11 @@ impl ControlChannel {
|
||||||
|
|
||||||
async fn process(&mut self, mut stream: TcpStream) {
|
async fn process(&mut self, mut stream: TcpStream) {
|
||||||
let mut lua = Lua::new();
|
let mut lua = Lua::new();
|
||||||
prepare_lua_runtime(&mut lua, self.source.clone());
|
if let Err(e) = register_builtin_fn(&mut lua, self.source.clone()) {
|
||||||
|
error!("could not prepare Lua runtime: {}", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
self.source.send(
|
self.source.send(
|
||||||
format!("LuaJit 5.2 via rlua inside process #{}\n@> ", std::process::id())
|
format!("LuaJit 5.2 via rlua inside process #{}\n@> ", std::process::id())
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
@ -60,6 +64,7 @@ impl ControlChannel {
|
||||||
|
|
||||||
rx = stream.read_u8() => match rx { // FIXME is read_exact cancelable?
|
rx = stream.read_u8() => match rx { // FIXME is read_exact cancelable?
|
||||||
Ok(c) => {
|
Ok(c) => {
|
||||||
|
// TODO move this "lua repl" code outside of here!
|
||||||
if !c.is_ascii() {
|
if !c.is_ascii() {
|
||||||
debug!("character '{}' is not ascii", c);
|
debug!("character '{}' is not ascii", c);
|
||||||
break;
|
break;
|
||||||
|
|
25
src/helpers.rs
Normal file
25
src/helpers.rs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
use mlua::{Value, Table};
|
||||||
|
|
||||||
|
pub fn pretty_lua(val: Value) -> String {
|
||||||
|
// TODO there must be some builtin to do this, right???
|
||||||
|
match val {
|
||||||
|
Value::Nil => "nil".into(),
|
||||||
|
Value::Boolean(b) => if b { "true".into() } else { "false".into() },
|
||||||
|
Value::LightUserData(x) => format!("LightUserData({:?})", x),
|
||||||
|
Value::Integer(n) => format!("{}", n),
|
||||||
|
Value::Number(n) => format!("{:.3}", n),
|
||||||
|
Value::String(s) => s.to_str().expect("string is not str").into(),
|
||||||
|
Value::Table(t) => try_serialize_table(&t),
|
||||||
|
Value::Function(f) => format!("Function({:?}", f),
|
||||||
|
Value::Thread(t) => format!("Thread({:?})", t),
|
||||||
|
Value::UserData(x) => format!("UserData({:?})", x),
|
||||||
|
Value::Error(e) => format!("Error({:?}) : {}", e, e.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_serialize_table(t: &Table) -> String {
|
||||||
|
match serde_json::to_string(t) {
|
||||||
|
Ok(txt) => txt,
|
||||||
|
Err(_e) => format!("{:?}", t),
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
mod runtime;
|
mod runtime;
|
||||||
mod channel;
|
mod channel;
|
||||||
|
mod helpers;
|
||||||
|
|
||||||
use channel::ControlChannel;
|
use channel::ControlChannel;
|
||||||
use tracing::{error, debug};
|
use tracing::{error, debug};
|
||||||
|
@ -41,4 +42,3 @@ async fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue