1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//! # SerpAPI Rust SDK
//!
//! [](https://crates.io/crates/serp-sdk)
//! [](https://docs.rs/serp-sdk)
//! [](#license)
//! [](https://github.com/your-org/serp-sdk/actions)
//!
//! A comprehensive, production-ready Rust SDK for the [SerpAPI](https://serpapi.com) service
//! that provides real-time search engine results through a unified, type-safe interface.
//!
//! > 🏆 **Developed during the [Realtime Search AI Hackathon (Hybrid)](https://www.eventbrite.com/e/realtime-search-ai-hackathon-hybrid-powered-by-serpapi-tickets)
//! > powered by SerpAPI and organized by [AI Tinkerers Paris](https://paris.aitinkerers.org/)**
//!
//! ## Overview
//!
//! The SerpAPI Rust SDK is designed with developer experience at its core. It provides a fluent,
//! intuitive API that makes complex search operations simple while maintaining the flexibility
//! needed for advanced use cases. Whether you're building a search aggregator, market research
//! tool, or AI-powered application, this SDK handles the complexity of search API interactions
//! so you can focus on your business logic.
//!
//! ## Key Features
//!
//! - 🦀 **Type-safe by Design**: Leverage Rust's type system to catch errors at compile-time
//! - ⚡ **Async-First Architecture**: Built on Tokio for high-performance concurrent operations
//! - 🎯 **Intuitive Builder Pattern**: Chain methods naturally to construct complex queries
//! - 🔄 **Intelligent Retry Logic**: Automatic retries with exponential backoff for resilience
//! - 🌊 **Streaming Support**: Efficiently handle large result sets with async streams
//! - 🏭 **Production-Ready**: Battle-tested error handling, comprehensive logging, and metrics
//! - 🔍 **Multi-Engine Support**: Google, Bing, Yahoo, Yandex, and 40+ search engines
//! - 📊 **Specialized Searches**: Images, news, videos, shopping, maps, and local results
//!
//! ## Installation
//!
//! Add the SDK to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! serp-sdk = "0.2"
//! tokio = { version = "1.0", features = ["full"] }
//!
//! # Optional: For streaming support
//! futures = "0.3"
//!
//! # Optional: For enhanced logging
//! tracing = "0.1"
//! tracing-subscriber = "0.3"
//! ```
//!
//! ## Quick Start
//!
//! ### Basic Search Example
//!
//! ```rust,no_run
//! use serp_sdk::{SerpClient, SearchQuery};
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Initialize client with API key from environment or explicit configuration
//! let client = SerpClient::builder()
//! .api_key("your-serp-api-key")
//! .build()?;
//!
//! // Perform a simple search
//! let results = client.search(
//! SearchQuery::new("Rust programming language")
//! .language("en")
//! .country("us")
//! .limit(10)?
//! ).await?;
//!
//! // Process organic search results
//! if let Some(organic) = results.organic_results {
//! for result in organic {
//! println!("Title: {}", result.title);
//! println!("Link: {}", result.link);
//! if let Some(snippet) = result.snippet {
//! println!("Snippet: {}", snippet);
//! }
//! println!("---");
//! }
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ### Environment Configuration
//!
//! The SDK supports configuration through environment variables for production deployments:
//!
//! ```bash
//! export SERP_API_KEY="your-api-key"
//! export SERP_TIMEOUT="30" # Timeout in seconds
//! export SERP_MAX_RETRIES="5" # Maximum retry attempts
//! ```
//!
//! ## Advanced Usage
//!
//! ### Streaming Large Result Sets
//!
//! For queries that return large numbers of results, use streaming to process them efficiently:
//!
//! ```rust,no_run
//! use futures::StreamExt;
//! use serp_sdk::{SerpClient, SearchQuery, StreamConfig};
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let client = SerpClient::builder().api_key("test").build()?;
//! // Configure streaming parameters
//! let stream_config = StreamConfig::new()
//! .page_size(20)? // Results per page
//! .max_pages(5) // Maximum pages to fetch
//! .delay(std::time::Duration::from_millis(500)); // Rate limiting
//!
//! // Create a search stream
//! let mut stream = client.search_stream(
//! SearchQuery::new("rust async programming"),
//! stream_config
//! );
//!
//! // Process results as they arrive
//! while let Some(page_result) = stream.next().await {
//! match page_result {
//! Ok(results) => {
//! println!("Processing page with {} results",
//! results.organic_results.as_ref().map_or(0, |r| r.len()));
//!
//! // Process each page's results
//! if let Some(organic) = results.organic_results {
//! for result in organic {
//! // Your processing logic here
//! }
//! }
//! }
//! Err(e) => eprintln!("Error fetching page: {}", e),
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ### Specialized Search Types
//!
//! The SDK provides built-in support for various search types with tailored result structures:
//!
//! ```rust,no_run
//! # use serp_sdk::{SerpClient, SearchQuery};
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let client = SerpClient::builder().api_key("test").build()?;
//! // Image search with visual results
//! let image_results = client.search(
//! SearchQuery::new("rust logo")
//! .images() // Automatically sets tbm=isch parameter
//! .limit(50)?
//! ).await?;
//!
//! if let Some(images) = image_results.inline_images {
//! for image in images {
//! println!("Image: {:?}", image.original);
//! println!("Thumbnail: {:?}", image.thumbnail);
//! }
//! }
//!
//! // News search with recent articles
//! let news_results = client.search(
//! SearchQuery::new("rust programming language")
//! .news() // Automatically sets tbm=nws parameter
//! .language("en")
//! .time_filter("d") // Last 24 hours
//! ).await?;
//!
//! // Video search results
//! let video_results = client.search(
//! SearchQuery::new("rust tutorial")
//! .videos() // Automatically sets tbm=vid parameter
//! ).await?;
//!
//! // Shopping/product search
//! let shopping_results = client.search(
//! SearchQuery::new("rust programming book")
//! .shopping() // Automatically sets tbm=shop parameter
//! .country("us")
//! ).await?;
//!
//! // Local search with location
//! let local_results = client.search(
//! SearchQuery::new("coffee shops")
//! .location("Austin, Texas")
//! .limit(20)?
//! ).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Advanced Query Building
//!
//! Leverage the fluent builder pattern for complex queries:
//!
//! ```rust,no_run
//! # use serp_sdk::{SerpClient, SearchQuery};
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let client = SerpClient::builder().api_key("test").build()?;
//! let complex_query = SearchQuery::new("site:github.com rust async")
//! .language("en")
//! .country("us")
//! .device("desktop") // Desktop, tablet, or mobile
//! .safe_search("off") // off, active, or medium
//! .time_filter("m") // Past month
//! .filter("0") // Include similar results
//! .offset(10) // Start from result 10
//! .limit(50)? // Get 50 results
//! .custom_param("gl", "us") // Add any SerpAPI parameter
//! .custom_param("lr", "lang_en");
//!
//! let results = client.search(complex_query).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Error Handling
//!
//! The SDK provides granular error types for precise error handling:
//!
//! ```rust,no_run
//! use serp_sdk::{SerpClient, SearchQuery, SerpError};
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let client = SerpClient::builder().api_key("test").build()?;
//! # let query = SearchQuery::new("test");
//! match client.search(query).await {
//! Ok(results) => {
//! // Process successful results
//! }
//! Err(SerpError::RateLimited { retry_after }) => {
//! // Handle rate limiting
//! println!("Rate limited. Retry after {} seconds", retry_after);
//! tokio::time::sleep(std::time::Duration::from_secs(retry_after)).await;
//! // Retry the request
//! }
//! Err(SerpError::ApiError { code, message }) => {
//! // Handle API errors
//! match code {
//! 401 => println!("Invalid API key: {}", message),
//! 403 => println!("Access forbidden: {}", message),
//! 404 => println!("Resource not found: {}", message),
//! _ => println!("API error {}: {}", code, message),
//! }
//! }
//! Err(SerpError::InvalidQuery(msg)) => {
//! // Handle query validation errors
//! println!("Invalid query parameters: {}", msg);
//! }
//! Err(SerpError::NetworkError(e)) => {
//! // Handle network-level errors
//! println!("Network error: {}", e);
//! }
//! Err(e) => {
//! // Handle other errors
//! println!("Unexpected error: {}", e);
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ### Custom Retry Policies
//!
//! Configure retry behavior for resilient applications:
//!
//! ```rust,no_run
//! use serp_sdk::{SerpClient, RetryPolicy};
//! use std::time::Duration;
//!
//! let retry_policy = RetryPolicy::new(5) // Max 5 retries
//! .with_base_delay(Duration::from_millis(200)) // Start with 200ms delay
//! .with_max_delay(Duration::from_secs(30)) // Cap at 30 seconds
//! .with_jitter(true); // Add randomization
//!
//! let client = SerpClient::builder()
//! .api_key("your-key")
//! .timeout(Duration::from_secs(60))
//! .retry_policy(retry_policy)
//! .build()?;
//! # Ok::<(), serp_sdk::SerpError>(())
//! ```
//!
//! ### Working with Response Data
//!
//! The SDK provides strongly-typed response structures for all result types:
//!
//! ```rust,no_run
//! # use serp_sdk::{SerpClient, SearchQuery};
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let client = SerpClient::builder().api_key("test").build()?;
//! let results = client.search(SearchQuery::new("rust")).await?;
//!
//! // Access search metadata
//! println!("Search ID: {}", results.search_metadata.id);
//! if let Some(time) = results.search_metadata.total_time_taken {
//! println!("Time taken: {:.2}s", time);
//! }
//!
//! // Access knowledge graph
//! if let Some(kg) = results.knowledge_graph {
//! println!("Knowledge Graph: {}", kg.title);
//! if let Some(desc) = kg.description {
//! println!("Description: {}", desc);
//! }
//! }
//!
//! // Access answer box
//! if let Some(answer) = results.answer_box {
//! println!("Answer: {:?}", answer.answer);
//! }
//!
//! // Access related searches
//! if let Some(related) = results.related_searches {
//! println!("Related searches:");
//! for search in related {
//! // Handle both simple and block formats
//! match search {
//! serp_sdk::response::RelatedSearch::Simple { query, .. } => {
//! println!(" - {}", query);
//! }
//! serp_sdk::response::RelatedSearch::Block { items, .. } => {
//! for item in items {
//! if let Some(name) = item.name {
//! println!(" - {}", name);
//! }
//! }
//! }
//! }
//! }
//! }
//!
//! // Access pagination information
//! if let Some(pagination) = results.pagination {
//! println!("Current page: {:?}", pagination.current);
//! if let Some(next) = pagination.next {
//! println!("Next page: {}", next);
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Performance Considerations
//!
//! ### Connection Pooling
//!
//! The SDK automatically manages connection pooling for optimal performance:
//!
//! ```rust,no_run
//! # use serp_sdk::SerpClient;
//! // The client reuses connections efficiently
//! let client = SerpClient::builder()
//! .api_key("your-key")
//! .max_connections(100) // Maximum concurrent connections
//! .connection_timeout(std::time::Duration::from_secs(10))
//! .build()?;
//! # Ok::<(), serp_sdk::SerpError>(())
//! ```
//!
//! ### Batch Processing
//!
//! Process multiple queries efficiently:
//!
//! ```rust,no_run
//! # use serp_sdk::{SerpClient, SearchQuery};
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let client = SerpClient::builder().api_key("test").build()?;
//! use futures::future::join_all;
//!
//! let queries = vec![
//! SearchQuery::new("rust async"),
//! SearchQuery::new("rust web framework"),
//! SearchQuery::new("rust database"),
//! ];
//!
//! // Execute queries concurrently
//! let futures = queries.into_iter()
//! .map(|q| client.search(q))
//! .collect::<Vec<_>>();
//!
//! let results = join_all(futures).await;
//!
//! for result in results {
//! match result {
//! Ok(data) => println!("Got {} results",
//! data.organic_results.as_ref().map_or(0, |r| r.len())),
//! Err(e) => eprintln!("Query failed: {}", e),
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Integration Examples
//!
//! ### MCP (Model Context Protocol) Integration
//!
//! The SDK is designed to work seamlessly with AI agents and LLM tools:
//!
//! ```rust,no_run
//! # use serp_sdk::{SerpClient, SearchQuery};
//! use serde_json::json;
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let client = SerpClient::builder().api_key("test").build()?;
//! // Convert search results to MCP-compatible format
//! async fn search_for_mcp(
//! client: &SerpClient,
//! query: String,
//! ) -> Result<serde_json::Value, Box<dyn std::error::Error>> {
//! let results = client.search(SearchQuery::new(&query)).await?;
//!
//! Ok(json!({
//! "results": results.organic_results.unwrap_or_default()
//! .iter()
//! .map(|r| json!({
//! "title": r.title,
//! "url": r.link,
//! "snippet": r.snippet
//! }))
//! .collect::<Vec<_>>(),
//! "metadata": {
//! "total_time": results.search_metadata.total_time_taken,
//! "query": query
//! }
//! }))
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Troubleshooting
//!
//! ### Common Issues
//!
//! 1. **Rate Limiting**: Implement exponential backoff or use the built-in retry policy
//! 2. **Timeout Errors**: Increase the timeout duration for slow queries
//! 3. **Invalid API Key**: Ensure your API key is correctly set and has sufficient quota
//! 4. **Deserialization Errors**: Update to the latest SDK version for API compatibility
//!
//! ### Debug Logging
//!
//! Enable detailed logging for troubleshooting:
//!
//! ```rust,no_run
//! use tracing_subscriber;
//!
//! // Enable debug logging
//! tracing_subscriber::fmt()
//! .with_max_level(tracing::Level::DEBUG)
//! .init();
//! ```
//!
//! ## Contributing
//!
//! We welcome contributions! Please see our [GitHub repository](https://github.com/RustSandbox/SerpRS)
//! for contribution guidelines and development setup.
//!
//! ## License
//!
//! This project is dual-licensed under MIT and Apache-2.0 licenses.
//!
//! ## See Also
//!
//! - [`client`]: HTTP client implementation and configuration
//! - [`query`]: Query builder and search parameters
//! - [`response`]: Response structures and deserialization
//! - [`streaming`]: Async streaming support for pagination
//! - [`error`]: Error types and handling
//! - [`retry`]: Retry policies and backoff strategies
/// HTTP client module providing the main SerpAPI client implementation.
///
/// This module contains the [`SerpClient`](client::SerpClient) struct which is the primary
/// interface for interacting with the SerpAPI service. It provides methods for executing
/// searches, handling retries, and managing HTTP connections efficiently.
///
/// # Examples
///
/// ```rust,no_run
/// use serp_sdk::client::SerpClient;
///
/// let client = SerpClient::builder()
/// .api_key("your-api-key")
/// .build()
/// .expect("Failed to create client");
/// ```
/// Comprehensive error types for all SDK operations.
///
/// This module defines the [`SerpError`] enum and related types that
/// represent all possible error conditions in the SDK. It provides detailed error information
/// with actionable messages for debugging and error recovery.
/// Fluent query builder for constructing search requests.
///
/// The [`SearchQuery`](query::SearchQuery) builder provides a type-safe, ergonomic API
/// for constructing complex search queries with compile-time validation where possible.
/// Strongly-typed response structures for SerpAPI results.
///
/// This module contains all the response types returned by SerpAPI, including organic results,
/// knowledge graphs, answer boxes, and specialized result types for images, videos, news, etc.
/// Retry policies with configurable backoff strategies.
///
/// The [`RetryPolicy`](retry::RetryPolicy) struct allows fine-grained control over retry
/// behavior, including exponential backoff, jitter, and maximum delay configuration.
/// Streaming support for paginated search results.
///
/// This module provides async stream implementations for efficiently processing large result
/// sets through pagination, with built-in rate limiting and error handling.
// Re-export main types for convenience
pub use ;
pub use ;
pub use ;
pub use SearchResults;
pub use RetryPolicy;
pub use StreamConfig;