refactored engine decoding
This commit is contained in:
58
frontend/src/lua/engine_decoder.rs
Normal file
58
frontend/src/lua/engine_decoder.rs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
use mlua::Function;
|
||||||
|
|
||||||
|
pub struct Engine {
|
||||||
|
pub grabber: Function,
|
||||||
|
pub builder: Function,
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_engine_vec(
|
||||||
|
lua_instance: mlua::Lua,
|
||||||
|
engine_table: mlua::Table,
|
||||||
|
) -> mlua::Result<Vec<Engine>> {
|
||||||
|
let mut engine_vec: Vec<Engine> = Vec::new();
|
||||||
|
|
||||||
|
for pair in engine_table.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 current_engine = Engine {
|
||||||
|
name: name,
|
||||||
|
builder: builder_function.ok_or(mlua::Error::RuntimeError(
|
||||||
|
"Missing Builder Function".to_string(),
|
||||||
|
))?,
|
||||||
|
grabber: grabber_function.ok_or(mlua::Error::RuntimeError(
|
||||||
|
"Missing Grabber Function".to_string(),
|
||||||
|
))?,
|
||||||
|
};
|
||||||
|
|
||||||
|
engine_vec.push(current_engine)
|
||||||
|
}
|
||||||
|
Ok(engine_vec)
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ use std::fs;
|
|||||||
mod luahtml;
|
mod luahtml;
|
||||||
use luahtml::LuaHtml;
|
use luahtml::LuaHtml;
|
||||||
|
|
||||||
mod engine_decoder;
|
pub mod engine_decoder;
|
||||||
|
|
||||||
pub fn create_instance() -> mlua::Lua {
|
pub fn create_instance() -> mlua::Lua {
|
||||||
mlua::Lua::new()
|
mlua::Lua::new()
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
mod lua;
|
mod lua;
|
||||||
|
use lua::engine_decoder;
|
||||||
|
use lua::engine_decoder::Engine;
|
||||||
|
|
||||||
// use backend::return_results_vec;
|
// use backend::return_results_vec;
|
||||||
use std::{env, process};
|
use std::{env, process};
|
||||||
@@ -27,58 +29,60 @@ fn main() {
|
|||||||
// Long lived lua instance
|
// Long lived lua instance
|
||||||
let mut lua_instance = lua::create_instance();
|
let mut lua_instance = lua::create_instance();
|
||||||
|
|
||||||
let search_engines: mlua::Table =
|
let engine_table: mlua::Table =
|
||||||
lua::intialise_lua(&mut lua_instance, config_location).expect("Failed to initialise lua");
|
lua::intialise_lua(&mut lua_instance, config_location).expect("Failed to initialise lua");
|
||||||
|
|
||||||
for pair in search_engines.pairs::<String, mlua::Value>() {
|
let engine_vec = engine_decoder::get_engine_vec(lua_instance, engine_table);
|
||||||
let (name, functions) = pair.expect("Invalid Search Engine Pair");
|
|
||||||
|
|
||||||
println!("{}", name);
|
// for pair in search_engines.pairs::<String, mlua::Value>() {
|
||||||
|
// let (name, functions) = pair.expect("Invalid Search Engine Pair");
|
||||||
let mut grabber_function: Option<mlua::Function> = None;
|
//
|
||||||
let mut builder_function: Option<mlua::Function> = lua_instance
|
// println!("{}", name);
|
||||||
.globals()
|
//
|
||||||
.get("DefaultBuilder")
|
// let mut grabber_function: Option<mlua::Function> = None;
|
||||||
.expect("Could Not Unwrap DefaultBuilder");
|
// let mut builder_function: Option<mlua::Function> = lua_instance
|
||||||
|
// .globals()
|
||||||
match functions {
|
// .get("DefaultBuilder")
|
||||||
// If only grabber is supplied
|
// .expect("Could Not Unwrap DefaultBuilder");
|
||||||
mlua::Value::Function(function) => grabber_function = Some(function),
|
//
|
||||||
mlua::Value::Table(table) => {
|
// match functions {
|
||||||
for inner_pair in table.pairs::<String, mlua::Function>() {
|
// // If only grabber is supplied
|
||||||
let (name, function) = inner_pair.expect("Error decoding pairs");
|
// mlua::Value::Function(function) => grabber_function = Some(function),
|
||||||
|
// mlua::Value::Table(table) => {
|
||||||
match name.as_str() {
|
// for inner_pair in table.pairs::<String, mlua::Function>() {
|
||||||
"grabber" => grabber_function = Some(function),
|
// let (name, function) = inner_pair.expect("Error decoding pairs");
|
||||||
"builder" => builder_function = Some(function),
|
//
|
||||||
_ => {}
|
// match name.as_str() {
|
||||||
}
|
// "grabber" => grabber_function = Some(function),
|
||||||
}
|
// "builder" => builder_function = Some(function),
|
||||||
}
|
// _ => {}
|
||||||
|
// }
|
||||||
_ => {
|
// }
|
||||||
eprintln!("Unexpected value in returned lua table")
|
// }
|
||||||
}
|
//
|
||||||
}
|
// _ => {
|
||||||
|
// eprintln!("Unexpected value in returned lua table")
|
||||||
let results = grabber_function
|
// }
|
||||||
.expect("No function found")
|
// }
|
||||||
.call::<mlua::Table>("test");
|
//
|
||||||
|
// let results = grabber_function
|
||||||
let mut built_results: Vec<String> = Vec::new();
|
// .expect("No function found")
|
||||||
|
// .call::<mlua::Table>("test");
|
||||||
for result in results
|
//
|
||||||
.expect("cannot get results")
|
// let mut built_results: Vec<String> = Vec::new();
|
||||||
.sequence_values::<mlua::Table>()
|
//
|
||||||
{
|
// for result in results
|
||||||
let built_node = builder_function
|
// .expect("cannot get results")
|
||||||
.clone()
|
// .sequence_values::<mlua::Table>()
|
||||||
.expect("Invalid Builder Function")
|
// {
|
||||||
.call::<String>(result);
|
// let built_node = builder_function
|
||||||
|
// .clone()
|
||||||
built_results.push(built_node.expect("invalid built node"));
|
// .expect("Invalid Builder Function")
|
||||||
}
|
// .call::<String>(result);
|
||||||
|
//
|
||||||
println!("{:#?}", built_results)
|
// built_results.push(built_node.expect("invalid built node"));
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
|
// println!("{:#?}", built_results)
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user