From 1dd6c21ce4adc072991ef1b005bf989caebd6f52 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Fri, 17 Jul 2026 15:05:43 +0200 Subject: [PATCH] Updated converted again. Signed-off-by: Pavel Kirilin --- src/bot/handlers/basic/currency_converter.rs | 52 +++++++++++++------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/src/bot/handlers/basic/currency_converter.rs b/src/bot/handlers/basic/currency_converter.rs index aefbfcd..d5be78f 100644 --- a/src/bot/handlers/basic/currency_converter.rs +++ b/src/bot/handlers/basic/currency_converter.rs @@ -38,7 +38,8 @@ lazy_static::lazy_static! { "TRY", "AMD", "RSD", - "THB" + "THB", + "GEL", ]; static ref CONVERTION_ALIASES: HashMap<&'static str, &'static str> = HashMap::from( @@ -79,6 +80,8 @@ lazy_static::lazy_static! { ("динар", "RSD"), // THB ("бат", "THB"), + // GEL + ("лари", "GEL"), ] ); @@ -89,7 +92,7 @@ lazy_static::lazy_static! { .chain(SUPPORTED_CURS.iter().copied()) .collect::>() .join("|"); - Regex::new(format!(r"\s*(?P\d+([\.,]\d+)?)\s+((?P{a})[\p{{L}}]*)(\s+(в|to|in)\s+(?P{a})[\p{{L}}]*)?").as_str()).unwrap() + Regex::new(format!(r"(?i)\s*(?P\d+([\.,]\d+)?)\s+((?P{a})[\p{{L}}]*)(\s+(в|to|in)\s+(?P{a})[\p{{L}}]*)?").as_str()).unwrap() }; } @@ -173,7 +176,6 @@ impl Handler for CurrencyConverter { let mut mapped = HashSet::new(); for capture in CUR_REGEX.captures_iter(message.text()) { - log::warn!("Captured: {:?}", capture); // We parse supplied value from message let Some(num_value) = capture .name("cur_value") @@ -184,19 +186,24 @@ impl Handler for CurrencyConverter { 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; + let cur_name_raw = capture.name("cur_name").map(|mtch| mtch.as_str()); + let cur_name: String = if let Some(val) = cur_name_raw { + let lower = val.to_lowercase(); + if let Some(alias) = CONVERTION_ALIASES.get(lower.as_str()) { + alias.to_string() + } else { + val.to_uppercase().to_string() + } + } else { + String::new() }; + if cur_name.is_empty() { + continue; + } // Now we want to know current nominal for this value. let Some(nominal) = valutes // We search for it using cur_name. - .get(cur_name) + .get(cur_name.as_str()) .and_then(|info| info.get("Nominal")) .map(ToString::to_string) .and_then(|value| value.as_str().parse::().ok()) @@ -207,7 +214,7 @@ impl Handler for CurrencyConverter { // Now we want to know multiplier. let Some(multiplier) = valutes - .get(cur_name) + .get(cur_name.as_str()) .and_then(|info| info.get("Value")) .map(ToString::to_string) .and_then(|value| value.as_str().parse::().ok()) @@ -219,15 +226,22 @@ impl Handler for CurrencyConverter { let mut target_name = String::from("RUB"); let target_cur_name = capture.name("target").map(|mtch| mtch.as_str()); - let target_cur_name = target_cur_name - .and_then(|val| CONVERTION_ALIASES.get(val).copied()) - .or(target_cur_name); + let target_cur_name: String = if let Some(val) = target_cur_name { + let lower = val.to_lowercase(); + if let Some(alias) = CONVERTION_ALIASES.get(lower.as_str()) { + alias.to_string() + } else { + val.to_uppercase().to_string() + } + } else { + String::new() + }; - if let Some(target_cur) = target_cur_name { - if target_cur == cur_name { + if !target_cur_name.is_empty() { + if target_cur_name == cur_name { continue; } - if let Some(info) = valutes.get(target_cur) { + if let Some(info) = valutes.get(target_cur_name.as_str()) { let Some(target_nominal) = info .get("Nominal") .map(ToString::to_string)