serper-sdk 0.1.3

A minimalistic yet ergonomic Rust SDK for the Serper Google Search API
Documentation
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
/// Search response parsing and handling module
///
/// This module provides data structures and utilities for handling search responses
/// from the Serper API, including organic results, answer boxes, and knowledge graphs.
use serde::Deserialize;
use std::collections::HashMap;

/// Complete search response from the Serper API
///
/// This struct represents the full response structure that can be returned
/// by the Serper search API, with all possible fields as optional.
#[derive(Debug, Deserialize, PartialEq)]
pub struct SearchResponse {
    /// Metadata about the search request and response
    pub search_metadata: Option<SearchMetadata>,

    /// Organic search results
    pub organic: Option<Vec<OrganicResult>>,

    /// Answer box information (direct answers)
    pub answer_box: Option<AnswerBox>,

    /// Knowledge graph information
    pub knowledge_graph: Option<KnowledgeGraph>,

    /// Related questions/searches
    pub related_questions: Option<Vec<RelatedQuestion>>,

    /// Shopping results (if applicable)
    pub shopping: Option<Vec<ShoppingResult>>,

    /// News results (if applicable)
    pub news: Option<Vec<NewsResult>>,
}

impl SearchResponse {
    /// Creates a new empty search response
    pub fn new() -> Self {
        Self {
            search_metadata: None,
            organic: None,
            answer_box: None,
            knowledge_graph: None,
            related_questions: None,
            shopping: None,
            news: None,
        }
    }

    /// Checks if the response has any results
    pub fn has_results(&self) -> bool {
        self.organic.as_ref().is_some_and(|o| !o.is_empty())
            || self.answer_box.is_some()
            || self.knowledge_graph.is_some()
            || self.shopping.as_ref().is_some_and(|s| !s.is_empty())
            || self.news.as_ref().is_some_and(|n| !n.is_empty())
    }

    /// Gets the number of organic results
    pub fn organic_count(&self) -> usize {
        self.organic.as_ref().map_or(0, |o| o.len())
    }

    /// Gets organic results as a slice
    pub fn organic_results(&self) -> &[OrganicResult] {
        self.organic.as_deref().unwrap_or(&[])
    }

    /// Gets the first organic result if available
    pub fn first_result(&self) -> Option<&OrganicResult> {
        self.organic.as_ref()?.first()
    }

    /// Extracts all URLs from organic results
    pub fn extract_urls(&self) -> Vec<&str> {
        self.organic_results()
            .iter()
            .map(|result| result.link.as_str())
            .collect()
    }
}

impl Default for SearchResponse {
    fn default() -> Self {
        Self::new()
    }
}

/// Metadata about the search request and response
#[derive(Debug, Deserialize, PartialEq)]
pub struct SearchMetadata {
    /// Unique identifier for this search
    pub id: String,

    /// Status of the search request
    pub status: String,

    /// Timestamp when the search was created
    pub created_at: String,

    /// Time taken to process the request (seconds)
    pub request_time_taken: f64,

    /// Total time taken including network overhead (seconds)
    pub total_time_taken: f64,
}

/// Individual organic search result
#[derive(Debug, Deserialize, PartialEq, Clone)]
pub struct OrganicResult {
    /// Title of the search result
    pub title: String,

    /// URL of the search result
    pub link: String,

    /// Text snippet from the page (optional)
    pub snippet: Option<String>,

    /// Position in search results (1-based)
    pub position: u32,

    /// Additional metadata (optional)
    #[serde(flatten)]
    pub extra: HashMap<String, serde_json::Value>,
}

impl OrganicResult {
    /// Creates a new organic result
    pub fn new(title: String, link: String, position: u32) -> Self {
        Self {
            title,
            link,
            snippet: None,
            position,
            extra: HashMap::new(),
        }
    }

    /// Checks if the result has a snippet
    pub fn has_snippet(&self) -> bool {
        self.snippet.is_some()
    }

    /// Gets the snippet text or a default message
    pub fn snippet_or_default(&self) -> &str {
        self.snippet
            .as_deref()
            .unwrap_or("No description available")
    }

    /// Gets the domain from the URL
    pub fn domain(&self) -> Option<String> {
        url::Url::parse(&self.link)
            .ok()?
            .host_str()
            .map(|host| host.to_string())
    }
}

/// Answer box with direct answers to queries
#[derive(Debug, Deserialize, PartialEq)]
pub struct AnswerBox {
    /// Direct answer text (optional)
    pub answer: Option<String>,

    /// Snippet providing context for the answer (optional)
    pub snippet: Option<String>,

    /// Source title (optional)
    pub title: Option<String>,

    /// Source link (optional)
    pub link: Option<String>,
}

impl AnswerBox {
    /// Checks if the answer box has a direct answer
    pub fn has_answer(&self) -> bool {
        self.answer.is_some()
    }

    /// Gets the best available text (answer or snippet)
    pub fn best_text(&self) -> Option<&str> {
        self.answer.as_deref().or(self.snippet.as_deref())
    }
}

/// Knowledge graph information
#[derive(Debug, Deserialize, PartialEq)]
pub struct KnowledgeGraph {
    /// Title of the entity
    pub title: Option<String>,

    /// Description of the entity
    pub description: Option<String>,

    /// Entity type (person, organization, etc.)
    #[serde(rename = "type")]
    pub entity_type: Option<String>,

    /// Website URL (optional)
    pub website: Option<String>,

    /// Additional attributes
    #[serde(flatten)]
    pub attributes: HashMap<String, serde_json::Value>,
}

/// Related question from "People also ask"
#[derive(Debug, Deserialize, PartialEq)]
pub struct RelatedQuestion {
    /// The question text
    pub question: String,

    /// Snippet answering the question (optional)
    pub snippet: Option<String>,

    /// Source title (optional)
    pub title: Option<String>,

    /// Source link (optional)
    pub link: Option<String>,
}

/// Shopping result for product searches
#[derive(Debug, Deserialize, PartialEq)]
pub struct ShoppingResult {
    /// Product title
    pub title: String,

    /// Product link
    pub link: String,

    /// Product price (optional)
    pub price: Option<String>,

    /// Product source/merchant (optional)
    pub source: Option<String>,

    /// Product image URL (optional)
    pub image: Option<String>,

    /// Position in shopping results
    pub position: u32,
}

/// News result for news searches
#[derive(Debug, Deserialize, PartialEq)]
pub struct NewsResult {
    /// News article title
    pub title: String,

    /// News article link
    pub link: String,

    /// Article snippet (optional)
    pub snippet: Option<String>,

    /// News source (optional)
    pub source: Option<String>,

    /// Publication date (optional)
    pub date: Option<String>,

    /// Position in news results
    pub position: u32,
}

/// Response parser for handling different response formats
pub struct ResponseParser;

impl ResponseParser {
    /// Parses a JSON response into a SearchResponse
    ///
    /// # Arguments
    ///
    /// * `json_str` - The JSON response string
    ///
    /// # Returns
    ///
    /// Result containing the parsed SearchResponse or an error
    pub fn parse_response(json_str: &str) -> crate::core::Result<SearchResponse> {
        serde_json::from_str(json_str).map_err(crate::core::error::SerperError::Json)
    }

    /// Validates that a response has the expected structure
    pub fn validate_response(response: &SearchResponse) -> crate::core::Result<()> {
        // Basic validation - could be extended with more checks
        if let Some(metadata) = &response.search_metadata
            && metadata.id.is_empty()
        {
            return Err(crate::core::error::SerperError::validation_error(
                "Response metadata has empty ID",
            ));
        }

        // Validate organic results
        if let Some(organic) = &response.organic {
            for (idx, result) in organic.iter().enumerate() {
                if result.title.is_empty() {
                    return Err(crate::core::error::SerperError::validation_error(format!(
                        "Organic result {} has empty title",
                        idx
                    )));
                }
                if result.link.is_empty() {
                    return Err(crate::core::error::SerperError::validation_error(format!(
                        "Organic result {} has empty link",
                        idx
                    )));
                }
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_search_response_creation() {
        let response = SearchResponse::new();
        assert!(!response.has_results());
        assert_eq!(response.organic_count(), 0);
    }

    #[test]
    fn test_organic_result() {
        let result = OrganicResult::new(
            "Test Title".to_string(),
            "https://example.com".to_string(),
            1,
        );

        assert_eq!(result.title, "Test Title");
        assert_eq!(result.position, 1);
        assert!(!result.has_snippet());
        assert_eq!(result.snippet_or_default(), "No description available");
    }

    #[test]
    fn test_response_parsing() {
        let json_data = json!({
            "search_metadata": {
                "id": "test-123",
                "status": "Success",
                "created_at": "2023-01-01T00:00:00Z",
                "request_time_taken": 0.5,
                "total_time_taken": 1.0
            },
            "organic": [
                {
                    "title": "Test Result",
                    "link": "https://example.com",
                    "snippet": "Test snippet",
                    "position": 1
                }
            ]
        });

        let response: SearchResponse = serde_json::from_value(json_data).unwrap();
        assert!(response.has_results());
        assert_eq!(response.organic_count(), 1);

        let first = response.first_result().unwrap();
        assert_eq!(first.title, "Test Result");
    }

    #[test]
    fn test_answer_box() {
        let answer_box = AnswerBox {
            answer: Some("42".to_string()),
            snippet: Some("The answer to everything".to_string()),
            title: None,
            link: None,
        };

        assert!(answer_box.has_answer());
        assert_eq!(answer_box.best_text(), Some("42"));
    }

    #[test]
    fn test_response_validation() {
        let mut response = SearchResponse::new();

        // Valid response should pass
        assert!(ResponseParser::validate_response(&response).is_ok());

        // Response with empty organic result title should fail
        response.organic = Some(vec![OrganicResult {
            title: "".to_string(),
            link: "https://example.com".to_string(),
            snippet: None,
            position: 1,
            extra: HashMap::new(),
        }]);

        assert!(ResponseParser::validate_response(&response).is_err());
    }
}