Config expressions passing tests.

This commit is contained in:
mdecimus
2024-01-19 13:06:39 +01:00
parent 4551576e04
commit 02faa04e27
44 changed files with 344 additions and 304 deletions

View File

@@ -27,9 +27,12 @@ use deadpool::{
};
use std::{sync::Arc, time::Duration};
use store::{Store, Stores};
use utils::config::{
utils::{AsKey, ParseValue},
Config, Servers,
use utils::{
config::{
utils::{AsKey, ParseValue},
Config, Servers,
},
expr::Token,
};
use ahash::AHashMap;
@@ -171,9 +174,13 @@ impl AddressMapping {
"Invalid value for address mapping {key:?}: {value:?}",
)),
}
} else if let Some(if_block) =
config.parse_if_block(key, |name| Err(format!("Invalid variable name {name:?}.",)))?
{
} else if let Some(if_block) = config.parse_if_block(key, |name| {
if ["address", "email"].contains(&name) {
Ok(Token::Variable(1))
} else {
Err(format!("Invalid variable name {name:?}.",))
}
})? {
Ok(AddressMapping::Custom(if_block))
} else {
Ok(AddressMapping::Disable)

View File

@@ -300,15 +300,21 @@ impl AddressMapping {
}
}
AddressMapping::Custom(if_block) => {
let result = if_block
.eval(
|_| Variable::default(),
|_, _| async { Variable::default() },
)
.await
.into_string();
if !result.is_empty() {
return result.into_owned().into();
if let Ok(result) = String::try_from(
if_block
.eval(
|name| {
if name == 1 {
Variable::from(address)
} else {
Variable::default()
}
},
|_, _| async { Variable::default() },
)
.await,
) {
return result.into();
}
}
AddressMapping::Disable => (),
@@ -323,16 +329,23 @@ impl AddressMapping {
.rsplit_once('@')
.map(|(_, domain_part)| format!("@{}", domain_part))
.map(Cow::Owned),
AddressMapping::Custom(if_block) => {
let result = if_block
.eval(
|_| Variable::default(),
|_, _| async { Variable::default() },
)
.await
.into_string();
if !result.is_empty() {
Some(result.into_owned().into())
if let Ok(result) = String::try_from(
if_block
.eval(
|name| {
if name == 1 {
Variable::from(address)
} else {
Variable::default()
}
},
|_, _| async { Variable::default() },
)
.await,
) {
Some(result.into())
} else {
None
}