Skip to main content

asimov_serpapi_module/api/
duckduckgo.rs

1// This is free and unencumbered software released into the public domain.
2
3#![allow(unused)]
4
5use asimov_module::prelude::{FromStr, String};
6use serde::Serialize;
7
8/// See: https://serpapi.com/duckduckgo-search-api#api-parameters
9#[derive(Clone, Debug, Default, Serialize)]
10pub struct DuckDuckGoSearchRequest {
11    /// See: https://serpapi.com/duckduckgo-search-api#api-parameters-search-query-q
12    pub q: String,
13
14    /// See: https://serpapi.com/duckduckgo-search-api#api-parameters-localization-kl
15    pub kl: Option<String>,
16}
17
18impl FromStr for DuckDuckGoSearchRequest {
19    type Err = url::ParseError;
20
21    fn from_str(input: &str) -> Result<Self, Self::Err> {
22        url::Url::parse(input).map(|url| {
23            let mut output = Self::default();
24            for (k, v) in url.query_pairs() {
25                match k.as_ref() {
26                    "q" => output.q = v.trim().into(),
27                    "kl" => output.kl = Some(v.trim().into()),
28                    _ => {}
29                }
30            }
31            output
32        })
33    }
34}