Added new handle.

Signed-off-by: Pavel Kirilin <win10@list.ru>
This commit is contained in:
2024-04-04 03:02:57 +02:00
parent 07c6ff69e0
commit 90ac6d77aa
10 changed files with 77 additions and 32 deletions
+24 -19
View File
@@ -74,7 +74,7 @@ lazy_static::lazy_static! {
);
static ref CUR_REGEX: Regex = {
#[allow(clippy::clone_double_ref)]
#[allow(suspicious_double_ref_op)]
let a = CONVERTION_ALIASES.keys()
.copied()
.chain(SUPPORTED_CURS.iter().copied())
@@ -115,7 +115,9 @@ impl Filter for CurrencyTextFilter {
#[async_trait::async_trait]
impl Handler for CurrencyConverter {
async fn react(&self, _: &Client, update: &Update) -> anyhow::Result<()> {
let Some(message) = get_message(update) else{ return Ok(())};
let Some(message) = get_message(update) else {
return Ok(());
};
let response = self
.client
.get("https://www.cbr-xml-daily.ru/daily_json.js")
@@ -127,10 +129,11 @@ impl Handler for CurrencyConverter {
let Some(valutes) = response
.get("Valute")
.and_then(serde_json::Value::as_object) else{
log::warn!("Can't get valutes fom response.");
return Ok(());
};
.and_then(serde_json::Value::as_object)
else {
log::warn!("Can't get valutes fom response.");
return Ok(());
};
let mut calucates = Vec::new();
@@ -142,17 +145,19 @@ impl Handler for CurrencyConverter {
// Convert match to string.
.map(|mtch| mtch.as_str())
// Parse it.
.and_then(|val| val.parse::<f64>().ok()) else{
continue;
};
.and_then(|val| val.parse::<f64>().ok())
else {
continue;
};
let cur_name = capture.name("cur_name").map(|mtch| mtch.as_str());
let Some(cur_name) = cur_name
// We check if the value is an alias.
.and_then(|val| CONVERTION_ALIASES.get(val).copied())
// get previous value if not.
.or(cur_name) else{
continue;
};
.or(cur_name)
else {
continue;
};
let fingerprint = format!("{num_value:.5} {cur_name}");
// Check if we already processed this value.
if mapped.contains(&fingerprint) {
@@ -167,19 +172,19 @@ impl Handler for CurrencyConverter {
.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;
};
// 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;
};
else {
continue;
};
calucates.push(format!(
"<pre>{num_value} {cur_name} = {value:.2} RUB</pre><br>",