Skip to main content

web_search/providers/
base.rs

1//! Base provider trait and common types
2
3use crate::error::SearchError;
4use crate::transport::SearchTransport;
5use async_trait::async_trait;
6
7pub use crate::SearchResult;
8
9/// Options for search queries
10#[derive(Debug, Clone, Default)]
11pub struct SearchOptions {
12    /// Maximum number of results to return
13    pub limit: Option<usize>,
14    /// Language code (e.g., "en", "de")
15    pub language: Option<String>,
16    /// Region code (e.g., "us", "de")
17    pub region: Option<String>,
18    /// Enable safe search filtering
19    pub safe_search: Option<bool>,
20}
21
22/// Trait that all search providers must implement
23#[async_trait]
24pub trait SearchProvider: Send + Sync {
25    /// Get the provider name
26    fn name(&self) -> &str;
27
28    /// Check if the provider is available/enabled
29    fn is_available(&self) -> bool;
30
31    /// Get the provider weight for reranking
32    fn weight(&self) -> f64;
33
34    /// Set the provider weight for reranking
35    fn set_weight(&mut self, weight: f64);
36
37    /// Enable or disable the provider
38    fn set_enabled(&mut self, enabled: bool);
39
40    /// Perform a search with the provider's default transport.
41    async fn search(
42        &self,
43        query: &str,
44        options: &SearchOptions,
45    ) -> Result<Vec<SearchResult>, SearchError>;
46
47    /// Perform a search with a caller-owned transport.
48    ///
49    /// The default preserves compatibility for third-party providers; built-in
50    /// providers override this method so every network request is routed through
51    /// the supplied transport.
52    async fn search_with_transport(
53        &self,
54        query: &str,
55        options: &SearchOptions,
56        transport: &dyn SearchTransport,
57    ) -> Result<Vec<SearchResult>, SearchError> {
58        let _ = transport;
59        self.search(query, options).await
60    }
61}