allow calc queries to end in =

This commit is contained in:
mat 2023-12-21 04:01:28 -06:00
parent e35a840a5c
commit 64fd3bcaae

View File

@ -3,27 +3,35 @@ use crate::engines::EngineResponse;
use super::regex; use super::regex;
pub fn request(query: &str) -> EngineResponse { pub fn request(query: &str) -> EngineResponse {
let Some(result_html) = evaluate(query, true) else { let query = clean_query(query.to_string());
let Some(result_html) = evaluate(&query, true) else {
return EngineResponse::new(); return EngineResponse::new();
}; };
EngineResponse::answer_html(format!( EngineResponse::answer_html(format!(
r#"<p class="answer-calc-query">{query} =</p> r#"<p class="answer-calc-query">{query} =</p>
<h3><b>{result_html}</b></h3>"#, <h3><b>{result_html}</b></h3>"#,
query = html_escape::encode_text(query), query = html_escape::encode_text(&query),
)) ))
} }
pub fn request_autocomplete(query: &str) -> Vec<String> { pub fn request_autocomplete(query: &str) -> Vec<String> {
let mut results = Vec::new(); let mut results = Vec::new();
if let Some(result) = evaluate(query, false) { let query = clean_query(query.to_string());
if let Some(result) = evaluate(&query, false) {
results.push(format!("{query}={result}")); results.push(format!("{query}={result}"));
} }
results results
} }
fn clean_query(query: String) -> String {
query.strip_suffix('=').unwrap_or(&query).trim().to_string()
}
fn evaluate(query: &str, html: bool) -> Option<String> { fn evaluate(query: &str, html: bool) -> Option<String> {
// at least 3 characters and not one of the short constants // at least 3 characters and not one of the short constants
if query.len() < 3 && !matches!(query.to_lowercase().as_str(), "pi" | "e" | "c") { if query.len() < 3 && !matches!(query.to_lowercase().as_str(), "pi" | "e" | "c") {