pub mod arxiv;
pub mod bing;
pub mod brave;
pub mod duckduckgo;
pub mod generic;
pub mod health;
pub mod marginalia_api;
pub mod mojeek;
pub mod openalex;
pub mod qwant;
pub mod searxng;
pub mod startpage;
pub mod wiby;
pub mod wikipedia;
pub mod yahoo;
use std::sync::Arc;
use crate::fetcher::Fetcher;
use crate::models::RawSearchResult;
tokio::task_local! {
static PAGE_BUDGET: usize;
}
pub fn page_budget(default: usize) -> usize {
PAGE_BUDGET
.try_with(|b| (*b).max(1).min(default.max(1)))
.unwrap_or(default)
}
pub async fn with_page_budget<F: std::future::Future>(budget: usize, fut: F) -> F::Output {
PAGE_BUDGET.scope(budget.max(1), fut).await
}
#[async_trait::async_trait]
pub trait SearchEngine: Send + Sync {
fn name(&self) -> &str;
fn needs_stealth(&self) -> bool {
false
}
async fn search(&self, f: &Fetcher, query: &str) -> Vec<RawSearchResult>;
}
pub fn get_engines(enabled: &[String]) -> Vec<Arc<dyn SearchEngine>> {
let mut engines: Vec<Arc<dyn SearchEngine>> = Vec::new();
let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
for name in enabled {
match name.as_str() {
"duckduckgo" | "duckduckgo_html" => {
if seen.insert("duckduckgo".to_string()) {
engines.push(Arc::new(duckduckgo::DuckDuckGo));
}
}
"brave" => {
if seen.insert("brave".to_string()) {
engines.push(Arc::new(brave::Brave));
}
}
n if n.starts_with("bing") && !n.contains("_images") && !n.contains("_videos") => {
if seen.insert(n.to_string()) {
engines.push(Arc::new(bing::Bing::new(n)));
}
}
"yahoo" => {
if seen.insert("yahoo".to_string()) {
engines.push(Arc::new(yahoo::Yahoo));
}
}
"qwant" => {
if seen.insert("qwant".to_string()) {
engines.push(Arc::new(qwant::Qwant));
}
}
"mojeek" => {
if seen.insert("mojeek".to_string()) {
engines.push(Arc::new(mojeek::Mojeek));
}
}
"startpage" => {
if seen.insert("startpage".to_string()) {
engines.push(Arc::new(startpage::Startpage));
}
}
"wikipedia" => {
if seen.insert("wikipedia".to_string()) {
engines.push(Arc::new(wikipedia::Wikipedia));
}
}
"wiby" => {
if seen.insert("wiby".to_string()) {
engines.push(Arc::new(wiby::Wiby));
}
}
"searxng" => {
if seen.insert("searxng".to_string()) {
engines.push(Arc::new(searxng::SearxNG));
}
}
"openalex" => {
if seen.insert("openalex".to_string()) {
engines.push(Arc::new(openalex::OpenAlex));
}
}
"arxiv" => {
if seen.insert("arxiv".to_string()) {
engines.push(Arc::new(arxiv::Arxiv));
}
}
"marginalia" | "marginalia_api" => {
if seen.insert("marginalia_api".to_string()) {
engines.push(Arc::new(marginalia_api::MarginaliaApi));
}
}
other => {
if let Some(spec) = generic::spec_for(other) {
engines.push(Arc::new(generic::GenericEngine::new(other, spec)));
} else {
tracing::warn!("Unknown engine (no spec): {}", other);
}
}
}
}
engines
}
pub fn primary_engines() -> Vec<String> {
vec![
"wikipedia", "marginalia_api", "wiby", "bing", "brave", "duckduckgo", "yahoo", "mojeek",
"yandex",
]
.into_iter()
.map(|s| s.to_string())
.collect()
}
pub fn backup_engines() -> Vec<String> {
vec![
"bing_news",
"brave_news",
"bing_uk",
"bing_in",
"bing_de",
"mwmbl",
"ecosia",
"yacy",
]
.into_iter()
.map(|s| s.to_string())
.collect()
}
pub fn all_engines() -> Vec<String> {
if let Some(v) = crate::config::engines_override() {
return v;
}
let mut all = primary_engines();
all.extend(backup_engines());
if searxng::is_enabled() {
all.insert(0, "searxng".to_string());
}
all
}
pub fn filter_healthy(names: Vec<String>) -> Vec<String> {
let before = names.len();
let kept: Vec<String> = names
.into_iter()
.filter(|n| {
if health::should_skip(n) {
tracing::debug!("Skipping quarantined engine: {}", n);
false
} else {
true
}
})
.collect();
if kept.len() < before {
tracing::info!(
"Engine quarantine: {} of {} engines skipped (0 results over their sample)",
before - kept.len(),
before
);
}
kept
}
pub fn specialized_engines(domain: &str) -> Vec<String> {
let base: Vec<&str> = match domain {
"tech" => vec![
"duckduckgo",
"brave",
"google",
"bing",
"startpage",
"qwant",
"google_news",
"bing_news",
"marginalia",
"stract",
"wiby",
],
"science" => vec![
"wikipedia",
"google",
"google_scholar",
"brave",
"duckduckgo",
"startpage",
"mojeek",
"bing",
"ecosia",
"qwant",
],
"finance" => vec![
"google",
"google_news",
"bing",
"bing_news",
"yahoo",
"yahoo_news",
"brave",
"brave_news",
"duckduckgo",
"startpage",
],
"health" => vec![
"google",
"wikipedia",
"bing",
"duckduckgo",
"brave",
"google_scholar",
"mojeek",
"startpage",
"ecosia",
"qwant",
],
"news" => vec![
"google_news",
"bing_news",
"brave_news",
"yahoo_news",
"duckduckgo_news",
"google",
"bing",
"brave",
"yahoo",
"duckduckgo",
],
_ => vec![
"wikipedia",
"duckduckgo",
"brave",
"google",
"bing",
"yahoo",
"qwant",
"startpage",
"mojeek",
"ecosia",
],
};
base.into_iter().map(|s| s.to_string()).collect()
}
pub fn engine_family(name: &str) -> &'static str {
if let Some(upstream) = name.strip_prefix("searxng:") {
let fam = engine_family(upstream);
return if fam == "indie" { "searxng" } else { fam };
}
if name == "searxng" {
return "searxng";
}
if name.starts_with("google") {
return "google";
}
if name.starts_with("startpage") || name.starts_with("ecosia") {
return "google"; }
if name.starts_with("bing") {
return "bing";
}
if name.starts_with("duckduckgo") || name.starts_with("yahoo") {
return "bing"; }
if name.starts_with("brave") {
return "brave";
}
if name.starts_with("yandex") {
return "yandex";
}
if name.starts_with("mojeek") {
return "mojeek";
}
if name.starts_with("marginalia") {
return "marginalia";
}
if name.starts_with("wikipedia") {
return "wikipedia";
}
if name.starts_with("openalex") {
return "openalex";
}
if name.starts_with("arxiv") {
return "arxiv";
}
if name.starts_with("qwant") {
return "qwant";
}
if name.starts_with("mwmbl") {
return "mwmbl";
}
if name.starts_with("wiby") {
return "wiby";
}
"indie"
}
pub fn family_weight(family: &str) -> f32 {
match family {
"google" | "bing" => 1.5,
"wikipedia" => 1.5,
"brave" => 1.4,
"yandex" | "mojeek" => 1.1,
"qwant" => 1.1,
"marginalia" | "mwmbl" | "wiby" => 0.9,
"openalex" => 1.2,
"arxiv" => 1.0,
_ => 1.0,
}
}
pub fn engine_weight(name: &str) -> f32 {
family_weight(engine_family(name))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_default_engine_is_constructible() {
for name in all_engines() {
let built = get_engines(std::slice::from_ref(&name));
assert_eq!(built.len(), 1, "roster entry {name:?} produced no engine");
assert!(!built[0].name().is_empty());
}
}
#[test]
fn duplicate_aliases_collapse_to_one_instance() {
let built = get_engines(&[
"duckduckgo".into(),
"duckduckgo_html".into(), "wikipedia".into(),
"wikipedia".into(), ]);
let names: Vec<&str> = built.iter().map(|e| e.name()).collect();
assert_eq!(built.len(), 2, "aliases did not collapse: {names:?}");
}
#[test]
fn structurally_broken_engines_are_not_in_the_default_roster() {
let roster = all_engines();
for dead in [
"google",
"google_news",
"google_uk",
"google_in",
"google_de",
"startpage",
"qwant",
"4get",
"yahoo_news",
] {
assert!(
!roster.contains(&dead.to_string()),
"{dead} is JS-walled/dead and must not be dispatched by default"
);
}
}
#[test]
fn removed_engines_can_still_be_forced_explicitly() {
for forced in ["google", "startpage", "qwant"] {
let built = get_engines(&[forced.to_string()]);
assert_eq!(
built.len(),
1,
"{forced} should still be constructible on request"
);
}
}
#[test]
fn waf_engines_declare_needs_stealth() {
for name in ["bing", "brave", "duckduckgo", "yahoo"] {
let built = get_engines(&[name.to_string()]);
assert!(
built[0].needs_stealth(),
"{name} must use TLS impersonation"
);
}
for name in ["wikipedia", "marginalia_api", "wiby"] {
let built = get_engines(&[name.to_string()]);
assert!(!built[0].needs_stealth(), "{name} should not need stealth");
}
}
#[test]
fn engine_family_groups_derived_indices_correctly() {
for e in [
"bing",
"bing_uk",
"bing_news",
"duckduckgo",
"yahoo",
"yahoo_news",
] {
assert_eq!(engine_family(e), "bing", "{e}");
}
for e in ["google", "google_in", "startpage", "ecosia"] {
assert_eq!(engine_family(e), "google", "{e}");
}
assert_eq!(engine_family("brave"), "brave");
assert_eq!(engine_family("brave_news"), "brave");
assert_eq!(engine_family("marginalia_api"), "marginalia");
assert_eq!(engine_family("yandex"), "yandex");
assert_eq!(engine_family("mojeek"), "mojeek");
}
#[test]
fn default_roster_has_enough_independent_families() {
let fams: std::collections::HashSet<&str> =
all_engines().iter().map(|e| engine_family(e)).collect();
assert!(
fams.len() >= 6,
"only {} independent families in the roster: {fams:?}",
fams.len()
);
}
}