use crate::ability::api_type::ApiType;
use crate::spellability::SpellAbility;
pub fn is_api(api: ApiType) -> impl Fn(&SpellAbility) -> bool {
move |sa: &SpellAbility| sa.api == Some(api)
}
pub fn has_sub_ability_api(api: ApiType) -> impl Fn(&SpellAbility) -> bool {
move |sa: &SpellAbility| {
let mut current = sa.sub_ability.as_deref();
while let Some(sub) = current {
if sub.api == Some(api) {
return true;
}
current = sub.sub_ability.as_deref();
}
false
}
}
pub fn is_valid<'a>(restrictions: &'a [&'a str]) -> impl Fn(&SpellAbility) -> bool + 'a {
move |sa: &SpellAbility| {
for &restriction in restrictions {
if let Some(key) = restriction.strip_prefix('!') {
if sa.param_is_true(key) {
return false;
}
} else if restriction.contains('$') {
let parts: Vec<&str> = restriction.splitn(2, '$').collect();
if parts.len() == 2 {
let key = parts[0].trim();
let expected = parts[1].trim();
match sa.param_value(key) {
Some(val) if val.eq_ignore_ascii_case(expected) => {}
_ => return false,
}
}
} else {
if !sa.param_is_true(restriction) {
return false;
}
}
}
true
}
}