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

View File

@ -1,7 +1,9 @@
use cli_log::{debug, info}; use std::collections::HashMap;
use mlua::{IntoLua, LuaSerdeExt, Table, Value};
use super::CommandTransferValue; use cli_log::info;
use mlua::{ErrorContext, FromLua, IntoLua, LuaSerdeExt, Value};
use super::{CommandTransferValue, Table};
impl<'lua> IntoLua<'lua> for CommandTransferValue { impl<'lua> IntoLua<'lua> for CommandTransferValue {
fn into_lua(self, lua: &'lua mlua::Lua) -> mlua::Result<mlua::Value<'lua>> { 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); 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>( fn unwrap<'lua>(
value_to_unwrap: Value<'lua>, value_to_unwrap: Value<'lua>,
lua: &'lua mlua::Lua, lua: &'lua mlua::Lua,
) -> mlua::Result<mlua::Value<'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 let (_, value): (Value, Value) = table
.pairs() .pairs()
.next() .next()
@ -64,7 +99,7 @@ fn unwrap<'lua>(
// }, // },
// } // }
// ``` // ```
let output_table: Table = lua let output_table: mlua::Table = lua
.load("{}") .load("{}")
.eval() .eval()
.expect("This is static, it should always work"); .expect("This is static, it should always work");

View File

@ -1,5 +1,6 @@
use std::{collections::HashMap, fmt::Display}; use std::{collections::HashMap, fmt::Display};
use mlua::FromLua;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub mod type_conversions; 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, // As they are not static, it could be easier for other people,
// if they stay here. // if they stay here.
use crate::app::command_interface::command_transfer_value::CommandTransferValue; use crate::app::command_interface::command_transfer_value::CommandTransferValue;
use mlua::IntoLua;
use crate::app::Event; use crate::app::Event;
use mlua::IntoLua;
#[ci_command_enum] #[ci_command_enum]
struct Commands { struct Commands {
/// Returns the string given to it /// Prints to the output, with a newline.
// FIXME(@Soispha): This is a workaround because the default print prints to stdout, // HACK(@soispha): The stdlib Lua `print()` function has stdout as output hardcoded,
// which is obviously not ideal // redirecting stdout seems too much like a hack thus we are just redefining the print function
print: fn(String) -> String, // to output to a controlled output. <2023-09-09>
print: fn(CommandTransferValue),
// Begin debug functions // Begin debug functions
/// Greets the user /// Greets the user

View File

@ -78,6 +78,11 @@ pub async fn handle(
send_status_output!(output); send_status_output!(output);
EventStatus::Ok EventStatus::Ok
} }
Command::Print(output) => {
let output_str: String = output.to_string();
send_status_output!(output_str);
EventStatus::Ok
}
Command::CommandLineShow => { Command::CommandLineShow => {
app.ui.cli_enable(); app.ui.cli_enable();
@ -129,12 +134,6 @@ pub async fn handle(
send_main_output!("Hi, {}!", name); send_main_output!("Hi, {}!", name);
EventStatus::Ok 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 => { Command::GreetMultiple => {
let mut table: Table = HashMap::new(); let mut table: Table = HashMap::new();
table.insert("UserId".to_owned(), CommandTransferValue::Integer(2)); table.insert("UserId".to_owned(), CommandTransferValue::Integer(2));