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 grammers_client::{Client, InputMessage, Update};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
@ -131,6 +134,7 @@ impl Handler for CurrencyConverter {
|
|||||||
|
|
||||||
let mut calucates = Vec::new();
|
let mut calucates = Vec::new();
|
||||||
|
|
||||||
|
let mut mapped = HashSet::new();
|
||||||
for capture in CUR_REGEX.captures_iter(message.text()) {
|
for capture in CUR_REGEX.captures_iter(message.text()) {
|
||||||
// We parse supplied value from message
|
// We parse supplied value from message
|
||||||
let Some(num_value) = capture
|
let Some(num_value) = capture
|
||||||
@ -149,25 +153,38 @@ impl Handler for CurrencyConverter {
|
|||||||
.or(cur_name) else{
|
.or(cur_name) else{
|
||||||
continue;
|
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
|
let Some(nominal) = valutes
|
||||||
|
// We search for it using cur_name.
|
||||||
.get(cur_name)
|
.get(cur_name)
|
||||||
.and_then(|info| info.get("Nominal"))
|
.and_then(|info| info.get("Nominal"))
|
||||||
.map(ToString::to_string)
|
.map(ToString::to_string)
|
||||||
.and_then(|value| value.as_str().parse::<f64>().ok())
|
.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{
|
else{
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
let calculated = valutes
|
|
||||||
.get(cur_name)
|
calucates.push(format!(
|
||||||
.and_then(|info| info.get("Value"))
|
"<pre>{num_value} {cur_name} = {value:.2} RUB</pre><br>",
|
||||||
.map(ToString::to_string)
|
value = multiplier * num_value / nominal,
|
||||||
.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>"
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !calucates.is_empty() {
|
if !calucates.is_empty() {
|
||||||
|
Reference in New Issue
Block a user