fix(commands/print): Add basic support for printing non-string types

This commit is contained in:
Benedikt Peetz 2023-09-09 22:51:28 +02:00
parent 357c42332f
commit 9a9cda535a
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
5 changed files with 66 additions and 48 deletions

46
Cargo.lock generated
View File

@ -338,9 +338,9 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]]
name = "bytes"
version = "1.4.0"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be"
checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223"
[[package]]
name = "cassowary"
@ -399,15 +399,14 @@ dependencies = [
[[package]]
name = "chrono"
version = "0.4.28"
version = "0.4.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95ed24df0632f708f5f6d8082675bef2596f7084dee3dd55f632290bf35bfe0f"
checksum = "defd4e7873dbddba6c7c91e199c7fcb946abc4a6a4ac3195400bcfb01b5de877"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"time",
"wasm-bindgen",
"windows-targets",
]
@ -971,9 +970,9 @@ dependencies = [
[[package]]
name = "futures-signals"
version = "0.3.32"
version = "0.3.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36a12cb78961d5c0bc0e358599bba98ec09201090a22339cd8ea27e815c11b25"
checksum = "3b175f2f6600dd81d92d20cf10872b03ea9df6b2513ca7f672341260dacb1ab2"
dependencies = [
"discard",
"futures-channel",
@ -1755,9 +1754,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]]
name = "openssl-sys"
version = "0.9.92"
version = "0.9.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db7e971c2c2bba161b2d2fdf37080177eff520b3bc044787c7f1f5f9e78d869b"
checksum = "db4d56a4c0478783083cfafcc42493dd4a981d41669da64b4572a2a089b51b1d"
dependencies = [
"cc",
"libc",
@ -2261,9 +2260,9 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "rustix"
version = "0.38.11"
version = "0.38.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0c3dde1fc030af041adc40e79c0e7fbcf431dd24870053d187d7c66e4b87453"
checksum = "bdf14a7a466ce88b5eac3da815b53aefc208ce7e74d1c263aabb04d88c4abeb1"
dependencies = [
"bitflags 2.4.0",
"errno",
@ -2357,9 +2356,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.105"
version = "1.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "693151e1ac27563d6dbcec9dee9fbd5da8539b20fa14ad3752b2e6d363ace360"
checksum = "2cc66a619ed80bf7a0f6b17dd063a84b88f6dea1813737cf469aef1d081142c2"
dependencies = [
"itoa",
"ryu",
@ -2565,17 +2564,6 @@ dependencies = [
"syn 2.0.31",
]
[[package]]
name = "time"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a"
dependencies = [
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
"winapi",
]
[[package]]
name = "tinyvec"
version = "1.6.0"
@ -2660,9 +2648,9 @@ checksum = "7cda73e2f1397b1262d6dfdcef8aafae14d1de7748d66822d3bfeeb6d03e5e4b"
[[package]]
name = "toml_edit"
version = "0.19.14"
version = "0.19.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8123f27e969974a3dfba720fdb560be359f57b44302d280ba72e76a74480e8a"
checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421"
dependencies = [
"indexmap 2.0.0",
"toml_datetime",
@ -2894,12 +2882,6 @@ version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"

View File

@ -1,7 +1,9 @@
use cli_log::{debug, info};
use mlua::{IntoLua, LuaSerdeExt, Table, Value};
use std::collections::HashMap;
use super::CommandTransferValue;
use cli_log::info;
use mlua::{ErrorContext, FromLua, IntoLua, LuaSerdeExt, Value};
use super::{CommandTransferValue, Table};
impl<'lua> IntoLua<'lua> for CommandTransferValue {
fn into_lua(self, lua: &'lua mlua::Lua) -> mlua::Result<mlua::Value<'lua>> {
@ -9,11 +11,44 @@ impl<'lua> IntoLua<'lua> for CommandTransferValue {
return unwrap(converted_output, lua);
}
}
impl<'lua> FromLua<'lua> for CommandTransferValue {
fn from_lua(value: Value<'lua>, lua: &'lua mlua::Lua) -> mlua::Result<Self> {
match value {
Value::Nil => Ok(Self::Nil),
Value::Boolean(bool) => Ok(Self::Boolean(bool)),
Value::Integer(int) => Ok(Self::Integer(int)),
Value::Number(num) => Ok(Self::Number(num)),
Value::String(string) => Ok(Self::String(
string
.to_str()
.context("Lua string seems to contain non UTF-8 chars")?
.to_owned(),
)),
Value::Table(table) => {
// FIXME(@soispha): This will fail, when the key is not a string, this should be
// accounted for <2023-09-09>
let mut rust_table: Table = HashMap::new();
for pair in table.pairs() {
let (key, value) = pair?;
rust_table.insert(key, value);
}
return Ok(Self::Table(rust_table));
}
Value::Function(_) => todo!(),
Value::Thread(_) => todo!(),
Value::UserData(_) => todo!(),
Value::Error(_) => todo!(),
Value::LightUserData(_) => todo!(),
}
}
}
fn unwrap<'lua>(
value_to_unwrap: Value<'lua>,
lua: &'lua mlua::Lua,
) -> mlua::Result<mlua::Value<'lua>> {
fn unwrap_first_level<'lua>(table: Table<'lua>) -> mlua::Result<Value<'lua>> {
fn unwrap_first_level<'lua>(table: mlua::Table<'lua>) -> mlua::Result<Value<'lua>> {
let (_, value): (Value, Value) = table
.pairs()
.next()
@ -64,7 +99,7 @@ fn unwrap<'lua>(
// },
// }
// ```
let output_table: Table = lua
let output_table: mlua::Table = lua
.load("{}")
.eval()
.expect("This is static, it should always work");

View File

@ -1,5 +1,6 @@
use std::{collections::HashMap, fmt::Display};
use mlua::FromLua;
use serde::{Deserialize, Serialize};
pub mod type_conversions;

View File

@ -9,15 +9,16 @@ use language_macros::ci_command_enum;
// As they are not static, it could be easier for other people,
// if they stay here.
use crate::app::command_interface::command_transfer_value::CommandTransferValue;
use mlua::IntoLua;
use crate::app::Event;
use mlua::IntoLua;
#[ci_command_enum]
struct Commands {
/// Returns the string given to it
// FIXME(@Soispha): This is a workaround because the default print prints to stdout,
// which is obviously not ideal
print: fn(String) -> String,
/// Prints to the output, with a newline.
// HACK(@soispha): The stdlib Lua `print()` function has stdout as output hardcoded,
// redirecting stdout seems too much like a hack thus we are just redefining the print function
// to output to a controlled output. <2023-09-09>
print: fn(CommandTransferValue),
// Begin debug functions
/// Greets the user

View File

@ -78,6 +78,11 @@ pub async fn handle(
send_status_output!(output);
EventStatus::Ok
}
Command::Print(output) => {
let output_str: String = output.to_string();
send_status_output!(output_str);
EventStatus::Ok
}
Command::CommandLineShow => {
app.ui.cli_enable();
@ -129,12 +134,6 @@ pub async fn handle(
send_main_output!("Hi, {}!", name);
EventStatus::Ok
}
Command::Print(output) => {
// FIXME(@Soispha): This only works with strings, which is a clear downside to the
// original print function. Find a way to just use the original one.
send_main_output!("{}", output);
EventStatus::Ok
}
Command::GreetMultiple => {
let mut table: Table = HashMap::new();
table.insert("UserId".to_owned(), CommandTransferValue::Integer(2));