aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLorenz Leitner <lrnz.ltnr@gmail.com>2021-10-15 09:44:58 +0200
committerLorenz Leitner <lrnz.ltnr@gmail.com>2021-10-15 09:51:56 +0200
commit9424ae2f749d9e3b1d00f6801f685fba3d9a2694 (patch)
treeaf6a1f534f4556bb6a37e71e25a32541f74d6d14 /src
parentAdd UrlShortener trait (diff)
Return no results of json parsing fails
Diffstat (limited to '')
-rw-r--r--src/hooks/wolfram_alpha.rs33
1 files changed, 26 insertions, 7 deletions
diff --git a/src/hooks/wolfram_alpha.rs b/src/hooks/wolfram_alpha.rs
index 24e6318..70a771c 100644
--- a/src/hooks/wolfram_alpha.rs
+++ b/src/hooks/wolfram_alpha.rs
@@ -3,7 +3,7 @@ use crate::util::{
web::{quote_plus, IsgdUrlShortener, UrlShortener},
};
use anyhow::{bail, Context, Error, Result};
-use futures::try_join;
+use futures::join;
use irc::client::prelude::*;
use macros::privmsg;
use reqwest::{get, Url};
@@ -113,12 +113,17 @@ async fn wa_query(
let url = get_url(query_str, api_key, base_url)?;
let wa_res_fut = handle_wa_req(&url);
- // Can't just (foo.await, bar.await), smh
- // https://rust-lang.github.io/async-book/06_multiple_futures/02_join.html
- let (wa_res, user_url_shortened) = try_join!(wa_res_fut, user_url_shortened_fut)?;
+ let futs = join!(wa_res_fut, user_url_shortened_fut);
+ let wa_res = match futs.0 {
+ Ok(x) => x,
+ // Return early if there are no results at all
+ _ => return Ok("No results.".to_string()),
+ };
+ let user_url_shortened = futs.1?;
let string_result = match to_single_string(wa_res) {
- x if x.is_empty() => "No primary results.".to_string(),
+ // Return with user link, but no plaintext results
+ x if x.is_empty() => "No plaintext results.".to_string(),
x => x,
};
@@ -279,7 +284,7 @@ mod tests {
}
#[tokio::test]
- async fn test_query_with_result_with_primary_pods_parsing() -> Result<(), Error> {
+ async fn test_query_with_result_with_no_primary_pods_parsing() -> Result<(), Error> {
let body =
include_str!("../../tests/resources/wolfram_alpha_api_response_with_no_primaries.json");
let _m = mockito::mock("GET", Matcher::Any)
@@ -290,7 +295,21 @@ mod tests {
let res = wa_query("what is a url", None, Some(&mockito::server_url())).await?;
let res_without_link = res.rsplitn(2, "-").collect::<Vec<&str>>()[1..].join(" ");
- assert_eq!(res_without_link.trim(), "No primary results.");
+ assert_eq!(res_without_link.trim(), "No plaintext results.");
+ Ok(())
+ }
+
+ #[tokio::test]
+ async fn test_query_with_result_with_wrong_json_parsing() -> Result<(), Error> {
+ let body = include_str!("../../tests/resources/wolfram_alpha_api_response_wrong_json.json");
+ let _m = mockito::mock("GET", Matcher::Any)
+ // Trimmed down version of a full WA response:
+ .with_body(body)
+ .create();
+ mockito::start();
+
+ let res = wa_query("what is a url", None, Some(&mockito::server_url())).await?;
+ assert_eq!(res, "No results.");
Ok(())
}
}
2025-05-06update dependenciesMax Audron-691/+1659 2024-08-12add nix build and moduleMax Audron-5/+427 2022-02-19remove jsonnet lock fileMax Audron-36/+0 2022-02-19fix deploy to work with gitlab agentMax Audron-4/+4 2021-10-22write tons of documentation and reorganize some modulesMax Audron-65/+300 2021-10-22remove wolfram alpha url shorteningMax Audron-1/+2 2021-10-20remove failing wolfram alpha test casesMax Audron-105/+55 this is due to the url shorterner dying randomly and also just generally bad idea to call external services during unit tests. 2021-10-20bump version to 1.6.2Max Audron-3/+2 2021-10-20prepare for release on crates.ioMax Audron-39/+65 2021-10-20add async docs to macro crate and bump versionMax Audron-9/+10 2021-10-20change hook errors to be logged as warningsMax Audron-3/+3 they in nearly all cases aren't critical enough to warrant an actual error messages 2021-10-20fix configuration not loading correctly on release buildsMax Audron-8/+23 2021-10-19replace sedregex crate8-rework-sedMax Audron-20/+358 This replaces the sedregex crate with our own implementation for multiple reasons: 1. We required to access the parsed regex, this required a patch to the sedregex crate which did not get merged due to an inactive dev, blocking us from publishing on crates.Io 2. We wanted to highlight the changes done in bold 3. We want to add execution of multiple chained sed commands in the future which would require more modification 2021-10-19add formatting trait for irc codesMax Audron-0/+129 add an impl off the formatting trait on String to format Strings with the typical irc formatting codes for bold, italic etc 2021-10-17fix links in readmeMax Audron-2/+2