ASYNC SEARCH RESULTS YESSSSS
This commit is contained in:
@@ -5,5 +5,6 @@ edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
backend = {path = "../backend/"}
|
||||
futures = "0.3.34"
|
||||
mlua = { version = "0.12.2", features = ["lua54", "async", "vendored"] }
|
||||
tokio = { version = "1.54.1", features = ["full"] }
|
||||
|
||||
@@ -7,7 +7,7 @@ pub struct Engine {
|
||||
}
|
||||
|
||||
pub fn get_engine_vec(
|
||||
lua_instance: mlua::Lua,
|
||||
lua_instance: &mut mlua::Lua,
|
||||
engine_table: mlua::Table,
|
||||
) -> mlua::Result<Vec<Engine>> {
|
||||
let mut engine_vec: Vec<Engine> = Vec::new();
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
mod lua;
|
||||
use lua::engine_decoder;
|
||||
use lua::engine_decoder::Engine;
|
||||
|
||||
// use backend::return_results_vec;
|
||||
use std::{env, process};
|
||||
|
||||
fn main() {
|
||||
mod runner;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let mut args = env::args();
|
||||
let _bin = args.next();
|
||||
// let something = return_results_vec();
|
||||
@@ -32,57 +33,10 @@ fn main() {
|
||||
let engine_table: mlua::Table =
|
||||
lua::intialise_lua(&mut lua_instance, config_location).expect("Failed to initialise lua");
|
||||
|
||||
let engine_vec = engine_decoder::get_engine_vec(lua_instance, engine_table);
|
||||
let engine_vec = engine_decoder::get_engine_vec(&mut lua_instance, engine_table)
|
||||
.expect("could not get engines");
|
||||
|
||||
// for pair in search_engines.pairs::<String, mlua::Value>() {
|
||||
// let (name, functions) = pair.expect("Invalid Search Engine Pair");
|
||||
//
|
||||
// println!("{}", name);
|
||||
//
|
||||
// let mut grabber_function: Option<mlua::Function> = None;
|
||||
// let mut builder_function: Option<mlua::Function> = lua_instance
|
||||
// .globals()
|
||||
// .get("DefaultBuilder")
|
||||
// .expect("Could Not Unwrap DefaultBuilder");
|
||||
//
|
||||
// match functions {
|
||||
// // If only grabber is supplied
|
||||
// mlua::Value::Function(function) => grabber_function = Some(function),
|
||||
// mlua::Value::Table(table) => {
|
||||
// for inner_pair in table.pairs::<String, mlua::Function>() {
|
||||
// let (name, function) = inner_pair.expect("Error decoding pairs");
|
||||
//
|
||||
// match name.as_str() {
|
||||
// "grabber" => grabber_function = Some(function),
|
||||
// "builder" => builder_function = Some(function),
|
||||
// _ => {}
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// _ => {
|
||||
// eprintln!("Unexpected value in returned lua table")
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// let results = grabber_function
|
||||
// .expect("No function found")
|
||||
// .call::<mlua::Table>("test");
|
||||
//
|
||||
// let mut built_results: Vec<String> = Vec::new();
|
||||
//
|
||||
// for result in results
|
||||
// .expect("cannot get results")
|
||||
// .sequence_values::<mlua::Table>()
|
||||
// {
|
||||
// let built_node = builder_function
|
||||
// .clone()
|
||||
// .expect("Invalid Builder Function")
|
||||
// .call::<String>(result);
|
||||
//
|
||||
// built_results.push(built_node.expect("invalid built node"));
|
||||
// }
|
||||
//
|
||||
// println!("{:#?}", built_results)
|
||||
// }
|
||||
let results = runner::run_engines(engine_vec, "test".to_string()).await;
|
||||
|
||||
println!("{:#?}", results)
|
||||
}
|
||||
|
||||
41
frontend/src/runner.rs
Normal file
41
frontend/src/runner.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use crate::engine_decoder::Engine;
|
||||
use futures::future::try_join_all;
|
||||
use mlua::{Table, Value};
|
||||
|
||||
pub async fn run_engines(
|
||||
engines: Vec<Engine>,
|
||||
query: String,
|
||||
) -> mlua::Result<Vec<(String, Value)>> {
|
||||
let engine_futures = engines.into_iter().map(|engine| {
|
||||
let value = query.clone();
|
||||
async move {
|
||||
let results: Table = engine.grabber.call_async(value).await?;
|
||||
|
||||
let items: Vec<Value> = results
|
||||
.sequence_values::<Value>()
|
||||
.collect::<Result<Vec<Value>, mlua::Error>>()?;
|
||||
|
||||
let builder = engine.builder;
|
||||
let engine_name = engine.name;
|
||||
|
||||
let builder_futures = items.into_iter().map(|item| {
|
||||
let builder = builder.clone();
|
||||
let engine_name = engine_name.clone();
|
||||
|
||||
async move {
|
||||
let result: Value = builder.call_async((item, "test")).await?;
|
||||
|
||||
Ok::<_, mlua::Error>((engine_name, result))
|
||||
}
|
||||
});
|
||||
|
||||
try_join_all(builder_futures).await
|
||||
}
|
||||
});
|
||||
|
||||
let results: Vec<Vec<(String, Value)>> = try_join_all(engine_futures).await?;
|
||||
|
||||
let flattened: Vec<(String, Value)> = results.into_iter().flatten().collect();
|
||||
|
||||
Ok(flattened)
|
||||
}
|
||||
Reference in New Issue
Block a user