1 Commits

Author SHA1 Message Date
s3rius 1dd6c21ce4 Updated converted again.
/ docker_build (push) Successful in 3m52s
/ deploy_helm (push) Successful in 14s
Signed-off-by: Pavel Kirilin <s3riussan@gmail.com>
2026-07-17 15:08:51 +02:00
+33 -19
View File
@@ -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::<Vec<_>>()
.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();
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::<f64>().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::<f64>().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)