use crate::merge::{merge, MergeOptions, MergedResult};
use crate::search::{fanout, Options, SearchProvider};
use crate::types::{Failure, FileQuery, Outcome};
use futures::{Stream, StreamExt};
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
#[cfg(feature = "fetch")]
use crate::fetch::{FetchError, FetchRegistry, Fetched};
#[cfg(feature = "fetch")]
use std::path::PathBuf;
pub struct SearchReport {
pub results: Vec<MergedResult>,
pub failures: Vec<Failure>,
pub skipped: Vec<String>,
}
pub struct SearchStream {
pub skipped: Vec<String>,
outcomes: Pin<Box<dyn Stream<Item = Outcome> + Send>>,
}
impl SearchStream {
pub fn into_outcomes(self) -> Pin<Box<dyn Stream<Item = Outcome> + Send>> {
self.outcomes
}
}
impl Stream for SearchStream {
type Item = Outcome;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Option<Self::Item>> {
self.outcomes.as_mut().poll_next(cx)
}
}
pub struct Engine {
providers: Vec<Arc<dyn SearchProvider>>,
options: Options,
provider_weights: HashMap<String, f64>,
#[cfg(feature = "fetch")]
fetchers: FetchRegistry,
}
impl Engine {
#[cfg(feature = "fetch")]
pub fn new(providers: Vec<Arc<dyn SearchProvider>>) -> Self {
Self {
providers,
options: Options::default(),
provider_weights: HashMap::new(),
fetchers: FetchRegistry::with_defaults(),
}
}
#[cfg(not(feature = "fetch"))]
pub fn new(providers: Vec<Arc<dyn SearchProvider>>) -> Self {
Self {
providers,
options: Options::default(),
provider_weights: HashMap::new(),
}
}
pub fn with_options(mut self, options: Options) -> Self {
self.options = options;
self
}
pub fn with_weights(mut self, provider_weights: HashMap<String, f64>) -> Self {
self.provider_weights = provider_weights;
self
}
#[cfg(feature = "fetch")]
pub fn with_fetchers(mut self, fetchers: FetchRegistry) -> Self {
self.fetchers = fetchers;
self
}
pub fn providers(&self) -> &[Arc<dyn SearchProvider>] {
&self.providers
}
pub async fn search(&self, query: FileQuery) -> SearchReport {
let SearchStream { skipped, outcomes } = self.search_stream(query).await;
let outcomes = collect(outcomes).await;
let results: Vec<crate::types::FileResult> = outcomes
.iter()
.filter_map(|outcome| outcome.as_result().cloned())
.collect();
let failures = outcomes
.iter()
.filter_map(|outcome| outcome.as_failure().cloned())
.collect();
let results = merge(
results,
&MergeOptions {
provider_weights: self.provider_weights.clone(),
},
);
SearchReport {
results,
failures,
skipped,
}
}
pub async fn search_stream(&self, query: FileQuery) -> SearchStream {
let (providers, skipped) = self.capable(&query).await;
SearchStream {
skipped,
outcomes: Box::pin(fanout(query, providers, self.options.clone())),
}
}
async fn capable(&self, query: &FileQuery) -> (Vec<Arc<dyn SearchProvider>>, Vec<String>) {
let checks = self.providers.iter().map(|provider| {
let provider = provider.clone();
let probe = query.clone();
async move {
let accepts = provider.capabilities().await.accepts(&probe);
(accepts, provider)
}
});
let evaluated = futures::future::join_all(checks).await;
let mut providers = Vec::new();
let mut skipped = Vec::new();
for (accepts, provider) in evaluated {
if accepts {
providers.push(provider);
} else {
skipped.push(provider.name().to_owned());
}
}
(providers, skipped)
}
#[cfg(feature = "fetch")]
pub async fn fetch(
&self,
result: &MergedResult,
destination: impl Into<PathBuf>,
) -> Result<Fetched, FetchError> {
self.fetchers.fetch_merged(result, destination).await
}
}
async fn collect(
stream: impl futures::Stream<Item = crate::types::Outcome>,
) -> Vec<crate::types::Outcome> {
futures::pin_mut!(stream);
let mut outcomes = Vec::new();
while let Some(outcome) = stream.next().await {
outcomes.push(outcome);
}
outcomes
}