Updated converted again.
/ docker_build (push) Successful in 3m52s
/ deploy_helm (push) Successful in 14s

Signed-off-by: Pavel Kirilin <s3riussan@gmail.com>
This commit is contained in:
2026-07-17 15:05:43 +02:00
parent 8d86ca5fae
commit 1dd6c21ce4
+33 -19
View File
@@ -38,7 +38,8 @@ lazy_static::lazy_static! {
"TRY", "TRY",
"AMD", "AMD",
"RSD", "RSD",
"THB" "THB",
"GEL",
]; ];
static ref CONVERTION_ALIASES: HashMap<&'static str, &'static str> = HashMap::from( static ref CONVERTION_ALIASES: HashMap<&'static str, &'static str> = HashMap::from(
@@ -79,6 +80,8 @@ lazy_static::lazy_static! {
("динар", "RSD"), ("динар", "RSD"),
// THB // THB
("бат", "THB"), ("бат", "THB"),
// GEL
("лари", "GEL"),
] ]
); );
@@ -89,7 +92,7 @@ lazy_static::lazy_static! {
.chain(SUPPORTED_CURS.iter().copied()) .chain(SUPPORTED_CURS.iter().copied())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join("|"); .join("|");
Regex::new(format!(r"\s*(?P<cur_value>\d+([\.,]\d+)?)\s+((?P<cur_name>{a})[\p{{L}}]*)(\s+(в|to|in)\s+(?P<target>{a})[\p{{L}}]*)?").as_str()).unwrap() Regex::new(format!(r"(?i)\s*(?P<cur_value>\d+([\.,]\d+)?)\s+((?P<cur_name>{a})[\p{{L}}]*)(\s+(в|to|in)\s+(?P<target>{a})[\p{{L}}]*)?").as_str()).unwrap()
}; };
} }
@@ -173,7 +176,6 @@ impl Handler for CurrencyConverter {
let mut mapped = HashSet::new(); let mut mapped = HashSet::new();
for capture in CUR_REGEX.captures_iter(message.text()) { for capture in CUR_REGEX.captures_iter(message.text()) {
log::warn!("Captured: {:?}", capture);
// We parse supplied value from message // We parse supplied value from message
let Some(num_value) = capture let Some(num_value) = capture
.name("cur_value") .name("cur_value")
@@ -184,19 +186,24 @@ impl Handler for CurrencyConverter {
else { else {
continue; continue;
}; };
let cur_name = capture.name("cur_name").map(|mtch| mtch.as_str()); let cur_name_raw = capture.name("cur_name").map(|mtch| mtch.as_str());
let Some(cur_name) = cur_name let cur_name: String = if let Some(val) = cur_name_raw {
// We check if the value is an alias. let lower = val.to_lowercase();
.and_then(|val| CONVERTION_ALIASES.get(val).copied()) if let Some(alias) = CONVERTION_ALIASES.get(lower.as_str()) {
// get previous value if not. alias.to_string()
.or(cur_name) } else {
else { val.to_uppercase().to_string()
continue; }
} else {
String::new()
}; };
if cur_name.is_empty() {
continue;
}
// Now we want to know current nominal for this value. // 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. // We search for it using cur_name.
.get(cur_name) .get(cur_name.as_str())
.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())
@@ -207,7 +214,7 @@ impl Handler for CurrencyConverter {
// Now we want to know multiplier. // Now we want to know multiplier.
let Some(multiplier) = valutes let Some(multiplier) = valutes
.get(cur_name) .get(cur_name.as_str())
.and_then(|info| info.get("Value")) .and_then(|info| info.get("Value"))
.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())
@@ -219,15 +226,22 @@ impl Handler for CurrencyConverter {
let mut target_name = String::from("RUB"); let mut target_name = String::from("RUB");
let target_cur_name = capture.name("target").map(|mtch| mtch.as_str()); let target_cur_name = capture.name("target").map(|mtch| mtch.as_str());
let target_cur_name = target_cur_name let target_cur_name: String = if let Some(val) = target_cur_name {
.and_then(|val| CONVERTION_ALIASES.get(val).copied()) let lower = val.to_lowercase();
.or(target_cur_name); 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_name.is_empty() {
if target_cur == cur_name { if target_cur_name == cur_name {
continue; 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 let Some(target_nominal) = info
.get("Nominal") .get("Nominal")
.map(ToString::to_string) .map(ToString::to_string)