diff --git a/src/bot/handlers/basic/currency_converter.rs b/src/bot/handlers/basic/currency_converter.rs index 76ded6538fefb812eb1bed95e25b3d26fde7f2b8..5acfc21018cf8609fa6df643207efbbb35578215 100644 --- a/src/bot/handlers/basic/currency_converter.rs +++ b/src/bot/handlers/basic/currency_converter.rs @@ -1,4 +1,7 @@ -use std::{collections::HashMap, time::Duration}; +use std::{ + collections::{HashMap, HashSet}, + time::Duration, +}; use grammers_client::{Client, InputMessage, Update}; use regex::Regex; @@ -131,6 +134,7 @@ impl Handler for CurrencyConverter { let mut calucates = Vec::new(); + let mut mapped = HashSet::new(); for capture in CUR_REGEX.captures_iter(message.text()) { // We parse supplied value from message let Some(num_value) = capture @@ -149,25 +153,38 @@ impl Handler for CurrencyConverter { .or(cur_name) else{ continue; }; + let fingerprint = format!("{num_value:.5} {cur_name}"); + // Check if we already processed this value. + if mapped.contains(&fingerprint) { + continue; + } + // Add a value to not calculate it again. + mapped.insert(fingerprint); + // Now we want to know current nominal for this value. let Some(nominal) = valutes + // We search for it using cur_name. .get(cur_name) .and_then(|info| info.get("Nominal")) .map(ToString::to_string) .and_then(|value| value.as_str().parse::<f64>().ok()) + // If the name cannot be found, we continue. else{ continue; }; - let calculated = valutes + // Now we want to know multiplier. + let Some(multiplier) = valutes .get(cur_name) .and_then(|info| info.get("Value")) .map(ToString::to_string) .and_then(|value| value.as_str().parse::<f64>().ok()) - .map(|multiplier| multiplier * num_value / nominal); - if let Some(value) = calculated { - calucates.push(format!( - "<pre>{num_value} {cur_name} = {value:.2} RUB</pre><br>" - )); - } + else{ + continue; + }; + + calucates.push(format!( + "<pre>{num_value} {cur_name} = {value:.2} RUB</pre><br>", + value = multiplier * num_value / nominal, + )); } if !calucates.is_empty() {