use std::fmt;
use crate::collections::VecSet;
use crate::crs::{MERCATOR, WorldMapping};
use crate::expression::Expr;
use crate::expression::forms::image::UnavailableImages;
use crate::expression::type_::Type;
use crate::expression::value::{Value, type_of};
use crate::feature::geojson_feature::GeoJSONFeature;
use crate::feature::{Feature, FeatureState, GeometryTileFeature, Value as FeatureValue, ValueObject};
use crate::font_face::FontCoverage;
use crate::tile_id::CanonicalTileID;
pub type GlobalStateMap = ValueObject;
#[derive(Clone, PartialEq, Eq, thiserror::Error)]
#[error("{}", .0.message)]
pub struct EvaluationError(Box<ErrorMessage>);
#[derive(Clone, PartialEq, Eq)]
struct ErrorMessage {
message: String,
}
impl EvaluationError {
pub(crate) fn new(message: impl Into<String>) -> Self {
EvaluationError(Box::new(ErrorMessage { message: message.into() }))
}
pub(crate) fn type_mismatch(expected: &Type, value: &Value) -> Self {
EvaluationError::new(format!(
"Expected value to be of type {}, but found {} instead.",
crate::expression::type_::to_string(expected),
crate::expression::type_::to_string(&type_of(value))
))
}
#[must_use]
pub fn message(&self) -> &str {
&self.0.message
}
}
impl fmt::Debug for EvaluationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "EvaluationError({:?})", self.0.message)
}
}
pub type EvalResult<T> = Result<T, EvaluationError>;
pub type EvaluationResult = Result<Value, EvaluationError>;
#[derive(Clone, Copy, Default)]
pub struct EvaluationContext<'a> {
pub zoom: Option<f64>,
pub accumulated: Option<&'a FeatureValue>,
pub feature: Option<&'a dyn GeometryTileFeature>,
pub color_ramp_parameter: Option<f64>,
pub elevation: Option<f32>,
pub formatted_section: Option<&'a Value>,
pub feature_state: Option<&'a FeatureState>,
pub available_images: Option<&'a VecSet<String>>,
pub unavailable_images: Option<&'a UnavailableImages>,
pub canonical: Option<&'a CanonicalTileID>,
pub world: Option<&'a WorldMapping>,
pub global_state: Option<&'a GlobalStateMap>,
pub font_faces: Option<&'a dyn FontCoverage>,
}
impl<'a> EvaluationContext<'a> {
#[must_use]
pub fn new(zoom: f64) -> Self {
EvaluationContext { zoom: Some(zoom), ..Default::default() }
}
#[must_use]
pub fn from_feature(feature: Option<&'a dyn GeometryTileFeature>) -> Self {
EvaluationContext { feature, ..Default::default() }
}
#[must_use]
pub fn with_zoom_feature(zoom: f64, feature: Option<&'a dyn GeometryTileFeature>) -> Self {
EvaluationContext { zoom: Some(zoom), feature, ..Default::default() }
}
#[must_use]
pub(crate) fn from_accumulated(
accumulated: Option<&'a FeatureValue>,
feature: Option<&'a dyn GeometryTileFeature>,
) -> Self {
EvaluationContext { accumulated, feature, ..Default::default() }
}
#[must_use]
pub fn with_zoom_feature_state(
zoom: f64,
feature: Option<&'a dyn GeometryTileFeature>,
state: Option<&'a FeatureState>,
) -> Self {
EvaluationContext { zoom: Some(zoom), feature, feature_state: state, ..Default::default() }
}
#[must_use]
pub fn with_color_ramp(
zoom: Option<f64>,
feature: Option<&'a dyn GeometryTileFeature>,
color_ramp_parameter: Option<f64>,
) -> Self {
EvaluationContext { zoom, feature, color_ramp_parameter, ..Default::default() }
}
#[must_use]
pub fn with_formatted_section(mut self, formatted_section: Option<&'a Value>) -> Self {
self.formatted_section = formatted_section;
self
}
#[must_use]
pub fn with_feature_state(mut self, feature_state: Option<&'a FeatureState>) -> Self {
self.feature_state = feature_state;
self
}
#[must_use]
pub fn with_available_images(mut self, available_images: Option<&'a VecSet<String>>) -> Self {
self.available_images = available_images;
self
}
#[must_use]
pub fn with_unavailable_images(mut self, unavailable_images: Option<&'a UnavailableImages>) -> Self {
self.unavailable_images = unavailable_images;
self
}
#[must_use]
pub fn with_canonical_tile_id(mut self, canonical: Option<&'a CanonicalTileID>) -> Self {
self.canonical = canonical;
self
}
#[must_use]
pub fn with_world(mut self, world: Option<&'a WorldMapping>) -> Self {
self.world = world;
self
}
pub(crate) fn world(&self) -> &'a WorldMapping {
self.world.unwrap_or(&MERCATOR)
}
#[cfg(test)]
#[must_use]
pub(crate) fn with_elevation(mut self, elevation: f32) -> Self {
self.elevation = Some(elevation);
self
}
#[must_use]
pub fn with_global_state(mut self, global_state: Option<&'a GlobalStateMap>) -> Self {
self.global_state = global_state;
self
}
#[must_use]
pub fn with_font_faces(mut self, font_faces: Option<&'a impl FontCoverage>) -> Self {
self.font_faces = font_faces.map(|faces| -> &dyn FontCoverage { faces });
self
}
}
#[cfg(test)]
pub(crate) fn evaluate_feature(
e: &Expr,
zoom: Option<f64>,
feature: &Feature<f64>,
color_ramp_parameter: Option<f64>,
) -> EvaluationResult {
let f = GeoJSONFeature::new(feature);
e.evaluate(&EvaluationContext::with_color_ramp(zoom, Some(&f), color_ramp_parameter))
}
pub fn evaluate_accumulated(e: &Expr, accumulated: Option<&FeatureValue>, feature: &Feature<f64>) -> EvaluationResult {
let f = GeoJSONFeature::new(feature);
e.evaluate(&EvaluationContext::from_accumulated(accumulated, Some(&f)))
}
pub fn evaluate_feature_with_context(
e: &Expr,
feature: &Feature<f64>,
params: EvaluationContext<'_>,
) -> EvaluationResult {
with_geojson_feature(feature, params, |cx| e.evaluate(cx))
}
pub fn with_geojson_feature<R>(
feature: &Feature<f64>,
params: EvaluationContext<'_>,
f: impl FnOnce(&EvaluationContext<'_>) -> R,
) -> R {
let view = match params.canonical {
Some(canonical) => GeoJSONFeature::with_canonical(feature, canonical),
None => GeoJSONFeature::new(feature),
};
f(&EvaluationContext { feature: Some(&view), world: None, ..params })
}