From 44b2c505b601828013439e974b410d8ee4a489d4 Mon Sep 17 00:00:00 2001 From: Lorenz Leitner Date: Thu, 14 Oct 2021 19:36:28 +0200 Subject: Remove whitespace in result text --- src/hooks/wolfram_alpha.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/hooks/wolfram_alpha.rs b/src/hooks/wolfram_alpha.rs index 5584935..1e7b6d8 100644 --- a/src/hooks/wolfram_alpha.rs +++ b/src/hooks/wolfram_alpha.rs @@ -32,6 +32,16 @@ struct SubPod { plaintext: String, } +fn clean_result_text(text: &str) -> String { + text + // Remove newlines + .replace("\n", "; ") + // Remove multiple whitespace + .split_whitespace() + .collect::>() + .join(" ") +} + /// Reduces all 'pod' plaintexts to a single string. /// Same as gonzobot does it. fn to_single_string(wa_res: WaResponse) -> String { @@ -44,7 +54,7 @@ fn to_single_string(wa_res: WaResponse) -> String { let subpod_texts = pod .subpods .iter() - .map(|subpod| subpod.plaintext.clone()) + .map(|subpod| clean_result_text(&subpod.plaintext)) .collect::>() .join(", "); @@ -143,6 +153,8 @@ pub async fn wa(bot: &crate::Bot, msg: Message) -> Result<()> { #[cfg(test)] mod tests { + use crate::hooks::wolfram_alpha::clean_result_text; + use super::{get_input_query, get_wa_user_short_url, wa_query}; use anyhow::{Error, Result}; use mockito::{self, Matcher}; @@ -221,6 +233,14 @@ mod tests { Ok(()) } + #[test] + fn test_clean_result_text() { + assert_eq!( + clean_result_text("Newlines\nand multiple\n\n whitespace is removed."), + "Newlines; and multiple; ; whitespace is removed.", + ) + } + #[tokio::test] async fn test_query_result_parsing() -> Result<(), Error> { let body = include_str!("../../tests/resources/wolfram_alpha_api_response.json"); -- cgit v1.2.3