style: cleanup
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
ato 2023-02-18 23:32:50 +01:00
parent a7e7972033
commit 1c917722a6
5 changed files with 18 additions and 45 deletions

View File

@ -1,4 +1,5 @@
use mlua::UserData;
use tokio::fs;
#[derive(Clone)]
pub struct FS {}
@ -8,8 +9,7 @@ impl UserData for FS {
methods.add_async_method(
"write",
|_l, _this, (filename, contents): (String, String)| async move {
tokio::fs::write(filename, contents).await.unwrap();
Ok(())
Ok(fs::write(filename, contents).await.unwrap())
},
)
}

View File

@ -2,14 +2,14 @@ pub mod fs;
use mlua::Lua;
use std::time::Duration;
use tokio::time::sleep;
use tokio::time;
use self::fs::FS;
pub fn load_builtins(lua: &Lua) {
let sleep = lua
.create_async_function(|_l, n: u64| async move {
sleep(Duration::from_secs(n)).await;
time::sleep(Duration::from_secs(n)).await;
Ok(())
})
.unwrap();

View File

@ -31,6 +31,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
runtime.load(&load_builtins);
runtime.load(&load_apis);
runtime.start().await;
Ok(())

View File

@ -1,6 +1,7 @@
use std::collections::HashMap;
use mlua::{LuaSerdeExt, UserData, UserDataFields};
use serde_json::{from_str, Value};
#[derive(Clone)]
pub struct Part {
@ -9,16 +10,10 @@ pub struct Part {
}
impl UserData for Part {
fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_set("filename", |_, this, value| {
this.filename = value;
Ok(())
});
fields.add_field_method_set("filename", |_, this, value| Ok(this.filename = value));
fields.add_field_method_get("filename", |_, this| Ok(this.filename.to_owned()));
fields.add_field_method_set("content", |_, this, value| {
this.content = value;
Ok(())
});
fields.add_field_method_set("content", |_, this, value| Ok(this.content = value));
fields.add_field_method_get("content", |_, this| Ok(this.content.to_owned()));
}
}
@ -36,45 +31,25 @@ pub struct Req {
impl UserData for Req {
fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_set("body", |_, this, value| {
this.body = value;
Ok(())
});
fields.add_field_method_get("body", |l, this| {
match serde_json::from_str::<serde_json::Value>(&this.body) {
Ok(value) => Ok(l.to_value(&value)?),
Err(_) => Ok(l.to_value(&this.body)?),
}
fields.add_field_method_set("body", |_, this, value| Ok(this.body = value));
fields.add_field_method_get("body", |l, this| match from_str::<Value>(&this.body) {
Ok(value) => Ok(l.to_value(&value)?),
Err(_) => Ok(l.to_value(&this.body)?),
});
fields.add_field_method_set("form", |_, this, value| {
this.form = value;
Ok(())
});
fields.add_field_method_set("form", |_, this, value| Ok(this.form = value));
fields.add_field_method_get("form", |_, this| Ok(this.form.to_owned()));
fields.add_field_method_set("headers", |_, this, value| {
this.headers = value;
Ok(())
});
fields.add_field_method_set("headers", |_, this, value| Ok(this.headers = value));
fields.add_field_method_get("headers", |_, this| Ok(this.headers.to_owned()));
fields.add_field_method_set("method", |_, this, value| {
this.method = value;
Ok(())
});
fields.add_field_method_set("method", |_, this, value| Ok(this.method = value));
fields.add_field_method_get("method", |_, this| Ok(this.method.to_owned()));
fields.add_field_method_set("params", |_, this, value| {
this.params = value;
Ok(())
});
fields.add_field_method_set("params", |_, this, value| Ok(this.params = value));
fields.add_field_method_get("params", |_, this| Ok(this.params.to_owned()));
fields.add_field_method_set("parts", |_, this, value| {
this.parts = value;
Ok(())
});
fields.add_field_method_set("parts", |_, this, value| Ok(this.parts = value));
fields.add_field_method_get("parts", |_, this| Ok(this.parts.to_owned()));
}
}

View File

@ -25,10 +25,7 @@ pub struct Res {
impl UserData for Res {
fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_set("body", |_, this, value| {
this.body = value;
Ok(())
});
fields.add_field_method_set("body", |_, this, value| Ok(this.body = value));
fields.add_field_method_get("body", |_, this| Ok(this.body.to_owned()));
fields.add_field_method_set("json", |_, this, value| {