use crate::compound_expression::CompoundExpression;
use crate::expression::{Dependency, Expression, Kind, downcast};
use crate::literal::Literal;
use crate::value::Value;
pub fn is_global_property_constant(expression: &dyn Expression, properties: &[&str]) -> bool {
if expression.get_kind() == Kind::CompoundExpression {
let e = downcast::<CompoundExpression>(expression).expect("CompoundExpression");
for property in properties {
if e.get_operator() == *property {
return false;
}
}
}
let mut is_constant = true;
expression.each_child(&mut |e| {
if is_constant && !is_global_property_constant(e, properties) {
is_constant = false;
}
});
is_constant
}
const FILTER: &str = "filter-";
pub fn is_feature_constant(expression: &dyn Expression) -> bool {
if expression.get_kind() == Kind::CompoundExpression {
let e = downcast::<CompoundExpression>(expression).expect("CompoundExpression");
let name = e.get_operator();
let parameter_count = e.get_parameter_count();
if ((name == "get" || name == "has") && parameter_count == Some(1))
|| name == "properties"
|| name == "geometry-type"
|| name == "id"
|| name == "feature-state"
|| name.starts_with(FILTER)
{
return false;
}
}
if expression.get_kind() == Kind::FormatSectionOverride {
return false;
}
if expression.get_kind() == Kind::Within {
return false;
}
if expression.get_kind() == Kind::Distance {
return false;
}
if expression.get_kind() == Kind::CollatorExpression {
return false;
}
let mut feature_constant = true;
expression.each_child(&mut |e| {
if feature_constant && !is_feature_constant(e) {
feature_constant = false;
}
});
feature_constant
}
const ZOOM_PROPERTY: [&str; 1] = ["zoom"];
pub fn is_zoom_constant(e: &dyn Expression) -> bool {
is_global_property_constant(e, &ZOOM_PROPERTY)
}
pub fn is_runtime_constant(expression: &dyn Expression) -> bool {
if expression.get_kind() == Kind::ImageExpression {
return false;
}
if expression.get_kind() == Kind::CompoundExpression {
let e = downcast::<CompoundExpression>(expression).expect("CompoundExpression");
if e.get_operator() == "global-state" {
return false;
}
}
let mut runtime_constant = true;
expression.each_child(&mut |e| {
if runtime_constant && !is_runtime_constant(e) {
runtime_constant = false;
}
});
runtime_constant
}
pub fn feature_property_keys(expression: &dyn Expression) -> Option<Vec<String>> {
let mut keys = Vec::new();
if !collect_feature_property_keys(expression, &mut keys) {
return None;
}
keys.sort_unstable();
keys.dedup();
Some(keys)
}
fn collect_feature_property_keys(expression: &dyn Expression, keys: &mut Vec<String>) -> bool {
match expression.get_kind() {
Kind::Within | Kind::Distance | Kind::FormatSectionOverride => return false,
Kind::CompoundExpression => {
let Some(e) = downcast::<CompoundExpression>(expression) else {
return false;
};
let signature = e.signature();
if signature.dependencies.intersects(Dependency::FEATURE) {
let reads_key = match signature.name {
"get" | "has" => e.get_parameter_count() == Some(1),
"filter-==" | "filter-<" | "filter->" | "filter-<=" | "filter->=" | "filter-has" | "filter-in" => {
true
}
"geometry-type" | "filter-type-==" | "filter-type-in" => false,
_ => return false,
};
if reads_key {
let key = e.args().first().and_then(|arg| downcast::<Literal>(&**arg));
match key.map(Literal::get_value) {
Some(Value::String(key)) => keys.push(key.clone()),
_ => return false,
}
}
}
}
_ => {}
}
let mut groupable = true;
expression.each_child(&mut |e| {
groupable = groupable && collect_feature_property_keys(e, keys);
});
groupable
}