0
0
mirror of https://github.com/neon-mmd/websurfx.git synced 2024-11-22 14:08:23 -05:00

feat: fix bugs and add code to handle engine selections

Co-authored-by: Sabrina Jewson <58880148+SabrinaJewson@users.noreply.github.com>
This commit is contained in:
neon_arch 2023-07-14 12:56:06 +03:00
parent 0781385393
commit 2f01651be0

View File

@ -59,26 +59,35 @@ pub async fn aggregate(
} }
// fetch results from upstream search engines simultaneously/concurrently. // fetch results from upstream search engines simultaneously/concurrently.
let search_engines: Vec<Box<dyn SearchEngine>> = upstream_search_engines let search_engines: Vec<Box<dyn SearchEngine + Send + Sync>> = upstream_search_engines
.iter() .iter()
.map(|engine| match engine.to_lowercase().as_str() { .map(|engine| match engine.to_lowercase().as_str() {
"duckduckgo" => Box::new(duckduckgo::DuckDuckGo) as Box<dyn SearchEngine>, "duckduckgo" => Box::new(duckduckgo::DuckDuckGo) as Box<dyn SearchEngine + Send + Sync>,
"searx " => Box::new(searx::Searx) as Box<dyn SearchEngine>, "searx" => Box::new(searx::Searx) as Box<dyn SearchEngine + Send + Sync>,
&_ => panic!("Config Error: Incorrect config file option provided"),
}) })
.collect(); .collect();
let task_capacity: usize = search_engines.len();
let tasks: Vec<JoinHandle<Result<HashMap<String, RawSearchResult>, Report<EngineError>>>> = let tasks: Vec<JoinHandle<Result<HashMap<String, RawSearchResult>, Report<EngineError>>>> =
search_engines search_engines
.iter() .into_iter()
.map(|search_engine| { .map(|search_engine| {
tokio::spawn(search_engine.results(query.clone(), page, user_agent.clone())) let query: String = query.clone();
let user_agent: String = user_agent.clone();
tokio::spawn(
async move { search_engine.results(query, page, user_agent.clone()).await },
)
}) })
.collect(); .collect();
let mut outputs = Vec::with_capacity(search_engines.len()); let mut outputs = Vec::with_capacity(task_capacity);
for task in tasks { for task in tasks {
outputs.push(task.await.ok()) if let Ok(result) = task.await {
outputs.push(result.ok())
}
} }
let mut initial: bool = true; let mut initial: bool = true;
@ -87,8 +96,7 @@ pub async fn aggregate(
if initial { if initial {
match results { match results {
Some(result) => { Some(result) => {
let new_result = result.clone(); result_map.extend(result.clone());
result_map.extend(new_result.as_ref().unwrap().clone());
counter += 1; counter += 1;
initial = false initial = false
} }
@ -105,13 +113,7 @@ pub async fn aggregate(
} else { } else {
match results { match results {
Some(result) => { Some(result) => {
let new_result = result.clone(); result.clone().into_iter().for_each(|(key, value)| {
new_result
.as_ref()
.unwrap()
.clone()
.into_iter()
.for_each(|(key, value)| {
result_map result_map
.entry(key) .entry(key)
.and_modify(|result| { .and_modify(|result| {