extern crate backend; use mlua::{Lua, Table}; use std::fs; mod luahtml; use luahtml::LuaHtml; pub mod engine_decoder; pub fn create_instance() -> mlua::Lua { mlua::Lua::new() } pub fn intialise_lua(instance: &mut mlua::Lua, path: Option) -> mlua::Result { let lua = instance; let function_table = create_lua_function_table(&lua)?; lua.globals().set("idx", function_table)?; let path = path.unwrap_or("./config.lua".to_string()); let file_info = fs::read_to_string(path)?; lua.load(&file_info).exec()?; let search_engines: Table = lua.globals().get("SearchEngines")?; Ok(search_engines) } fn create_lua_function_table(lua: &Lua) -> mlua::Result
{ let idx_table = lua.create_table()?; lua.globals().set( "DefaultBuilder", "function (values) return '

'..values.title..'

'", )?; idx_table.set( "fetchUrl", lua.create_async_function(|_, url: String| async move { let scraped_data = tokio::task::spawn_blocking(move || backend::scrape_url_blocking(&url)) .await .map_err(mlua::Error::external)?; Ok(scraped_data) })?, )?; idx_table.set( "decodeHtml", lua.create_function(|_, result: String| { let document = backend::get_sanitised_html_graph(&result); Ok(LuaHtml { document }) })?, )?; Ok(idx_table) }