Skip to main content

web_search/providers/
base.rs

1//! Base provider trait and common types
2
3use async_trait::async_trait;
4use serde::{Deserialize, Serialize};
5
6use crate::error::SearchError;
7
8/// A single search result
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct SearchResult {
11    /// The title of the search result
12    pub title: String,
13    /// The URL of the search result
14    pub url: String,
15    /// The description/snippet of the search result
16    pub snippet: String,
17    /// The search provider that returned this result
18    pub source: String,
19    /// The rank position in the original results (1-based)
20    pub rank: usize,
21    /// Computed score after merging (optional)
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub score: Option<f64>,
24    /// Sources that returned this result (after deduplication)
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub sources: Option<Vec<String>>,
27}
28
29/// Options for search queries
30#[derive(Debug, Clone, Default)]
31pub struct SearchOptions {
32    /// Maximum number of results to return
33    pub limit: Option<usize>,
34    /// Language code (e.g., "en", "de")
35    pub language: Option<String>,
36    /// Region code (e.g., "us", "de")
37    pub region: Option<String>,
38    /// Enable safe search filtering
39    pub safe_search: Option<bool>,
40}
41
42/// Trait that all search providers must implement
43#[async_trait]
44pub trait SearchProvider: Send + Sync {
45    /// Get the provider name
46    fn name(&self) -> &str;
47
48    /// Check if the provider is available/enabled
49    fn is_available(&self) -> bool;
50
51    /// Get the provider weight for reranking
52    fn weight(&self) -> f64;
53
54    /// Set the provider weight for reranking
55    fn set_weight(&mut self, weight: f64);
56
57    /// Enable or disable the provider
58    fn set_enabled(&mut self, enabled: bool);
59
60    /// Perform a search
61    async fn search(
62        &self,
63        query: &str,
64        options: &SearchOptions,
65    ) -> Result<Vec<SearchResult>, SearchError>;
66}