Fixed convertion repetitions.
Signed-off-by: Pavel Kirilin <win10@list.ru>
This commit is contained in:
@ -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,26 +153,39 @@ 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;
|
||||
};
|
||||
// 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())
|
||||
else{
|
||||
continue;
|
||||
};
|
||||
let calculated = 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>"
|
||||
"<pre>{num_value} {cur_name} = {value:.2} RUB</pre><br>",
|
||||
value = multiplier * num_value / nominal,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
if !calucates.is_empty() {
|
||||
let mut bot_response =
|
||||
|
Reference in New Issue
Block a user