diff options
Diffstat (limited to 'qml_plugin/src/web')
| -rw-r--r-- | qml_plugin/src/web/duckduckgo.rs | 78 |
1 files changed, 46 insertions, 32 deletions
diff --git a/qml_plugin/src/web/duckduckgo.rs b/qml_plugin/src/web/duckduckgo.rs index 6b4edae..e5f1b64 100644 --- a/qml_plugin/src/web/duckduckgo.rs +++ b/qml_plugin/src/web/duckduckgo.rs @@ -1,6 +1,5 @@ use std::ops::{Deref, DerefMut}; -use crate::launcher::duckduckgo::QmlDuckDuckGo; use duckduckgo::{ browser::Browser, params::{SafeSearch, SearchParams, Toggle}, @@ -8,6 +7,8 @@ use duckduckgo::{ use reqwest::Url; use serde::Deserialize; +use crate::qml_interface::duckduckgo::QmlDuckDuckGo; + const DUCKDUCKGO: &str = "https://duckduckgo.com"; #[derive(Default, PartialEq)] @@ -83,36 +84,49 @@ pub async fn query(input: &str) -> anyhow::Result<DuckDuckGoContent> { _ => None, }; - let alt_desc = resp.entity.unwrap_or(match infobox { - Some(i) => i - .iter() - .find(|i| i.data_type == "wd_description" || i.data_type == "official_website") - .map_or(String::new(), |i| { - if i.data_type == "official_website" { - i.label.clone() - } else { - match i.value { - InfoboxValue::Entity(_) => unreachable!(), - InfoboxValue::String(ref s) => s.to_string(), + let alt_desc = resp.entity.unwrap_or( + match infobox { + Some(i) => i + .iter() + .find(|i| i.data_type == "wd_description" || i.data_type == "official_website") + .map_or( + String::new(), + |i| { + if i.data_type == "official_website" { + i.label.clone() + } else { + match i.value { + InfoboxValue::Entity(_) => unreachable!(), + InfoboxValue::String(ref s) => s.to_string(), + } + } + }, + ), + // TODO: find better way to get title from infobox + None => String::new(), + }, + ); + + Ok( + DuckDuckGoContent { + title: resp + .heading + .filter(|h| !h.is_empty()) + .unwrap_or(resp.definition.filter(|d| !d.is_empty()).unwrap_or(alt_desc.clone())), + description_html: resp + .answer + .filter(|a| !a.is_empty()) + .unwrap_or(resp.r#abstract.filter(|a| !a.is_empty()).unwrap_or(alt_desc)), + // TODO: try also Results[*].Icon.URL and RelatedTopics[*].Icon.URL + image: resp.image.filter(|i| !i.is_empty()).and_then( + |i| { + if !i.is_empty() { + Url::parse(&format!("{DUCKDUCKGO}{i}",)).ok() + } else { + None } - } - }), - // TODO: find better way to get title from infobox - None => String::new(), - }); - - Ok(DuckDuckGoContent { - title: resp - .heading - .filter(|h| !h.is_empty()) - .unwrap_or(resp.definition.filter(|d| !d.is_empty()).unwrap_or(alt_desc.clone())), - description_html: resp - .answer - .filter(|a| !a.is_empty()) - .unwrap_or(resp.r#abstract.filter(|a| !a.is_empty()).unwrap_or(alt_desc)), - // TODO: try also Results[*].Icon.URL and RelatedTopics[*].Icon.URL - image: resp.image.filter(|i| !i.is_empty()).and_then(|i| { - if !i.is_empty() { Url::parse(&format!("{DUCKDUCKGO}{i}",)).ok() } else { None } - }), - }) + }, + ), + }, + ) } |
