Skip to main content

mln_style/property/
value.rs

1use std::fmt::Debug;
2use std::sync::Arc;
3
4use crate::collections::VecSet;
5use crate::conversion::to_value::{ValueFactory, make_style_property};
6use crate::conversion::{Converter, Convertible, Error};
7use crate::expression::{Dependency, Functions, Type, ValueConverter, global_state_refs_intersect};
8use crate::property::convert::convert_property_value;
9use crate::property::evaluated::{Evaluated, Evaluation};
10use crate::property::evaluation::PropertyEvaluationParameters;
11use crate::property::expression::PropertyExpression;
12use crate::property::table::{PropertyInfo, StyleProperty};
13use crate::property::transition::GlobalStateChange;
14
15/// A style property's value: undefined (the default), a constant or an
16/// expression.
17#[derive(Debug, Default, mln_wire::Wire)]
18pub enum PropertyValue<T> {
19    #[default]
20    Undefined,
21    Constant(T),
22    Expression(PropertyExpression<T>),
23}
24
25// Clone and PartialEq are written out rather than derived so that they can be
26// `#[inline(never)]`: a layer's impl, its property sets and the transitions
27// clone and compare one per property, and a derive inlined there copies the
28// match into every one of them, which costs code size (the browser build's
29// wasm most) for no speed that style mutation needs.
30impl<T: Clone> Clone for PropertyValue<T> {
31    #[inline(never)]
32    fn clone(&self) -> Self {
33        match self {
34            PropertyValue::Undefined => PropertyValue::Undefined,
35            PropertyValue::Constant(v) => PropertyValue::Constant(v.clone()),
36            PropertyValue::Expression(e) => PropertyValue::Expression(e.clone()),
37        }
38    }
39}
40
41impl<T: PartialEq> PartialEq for PropertyValue<T> {
42    #[inline(never)]
43    fn eq(&self, other: &Self) -> bool {
44        match (self, other) {
45            (PropertyValue::Undefined, PropertyValue::Undefined) => true,
46            (PropertyValue::Constant(a), PropertyValue::Constant(b)) => a == b,
47            (PropertyValue::Expression(a), PropertyValue::Expression(b)) => a == b,
48            _ => false,
49        }
50    }
51}
52
53impl<T> From<PropertyExpression<T>> for PropertyValue<T> {
54    fn from(expression: PropertyExpression<T>) -> Self {
55        PropertyValue::Expression(expression)
56    }
57}
58
59impl<T> PropertyValue<T> {
60    pub fn is_constant(&self) -> bool {
61        matches!(self, PropertyValue::Constant(_))
62    }
63
64    pub fn is_expression(&self) -> bool {
65        matches!(self, PropertyValue::Expression(_))
66    }
67
68    /// The constant, or `None` when the value isn't a constant.
69    pub fn as_constant(&self) -> Option<&T> {
70        match self {
71            PropertyValue::Constant(t) => Some(t),
72            _ => None,
73        }
74    }
75
76    /// The expression, or `None` when the value isn't an expression.
77    pub fn as_expression(&self) -> Option<&PropertyExpression<T>> {
78        match self {
79            PropertyValue::Expression(e) => Some(e),
80            _ => None,
81        }
82    }
83
84    /// The expression, or `None` when the value isn't an expression.
85    pub fn as_expression_mut(&mut self) -> Option<&mut PropertyExpression<T>> {
86        match self {
87            PropertyValue::Expression(e) => Some(e),
88            _ => None,
89        }
90    }
91}
92
93/// A style value a property holds: a [`PropertyValue`], or a
94/// [`ColorRampPropertyValue`](crate::property::color_ramp::ColorRampPropertyValue).
95pub trait StyleValue: Clone + Default + Debug + PartialEq + 'static {
96    /// What the value evaluates to.
97    type Type: Clone + Debug + Default + ValueConverter;
98
99    /// Converts a style JSON value for the property `info`, parsing
100    /// expressions against `functions`.
101    fn convert(value: &Convertible<'_>, info: &PropertyInfo, functions: &Arc<Functions>) -> Result<Self, Error>;
102
103    fn style_property(&self) -> StyleProperty;
104
105    /// The value for a frame, with `default` for an undefined value.
106    fn evaluate(
107        &self,
108        evaluation: Evaluation<Self::Type>,
109        parameters: &PropertyEvaluationParameters,
110        default: Self::Type,
111    ) -> Evaluated<Self::Type>;
112
113    fn is_undefined(&self) -> bool;
114
115    /// Whether the value may differ by feature.
116    fn is_data_driven(&self) -> bool;
117
118    fn dependencies(&self) -> Dependency;
119
120    fn global_state_refs(&self) -> Option<&VecSet<String>>;
121
122    /// Whether the values differ and either may differ by feature.
123    fn has_data_driven_property_difference(&self, other: &Self) -> bool {
124        (self.is_data_driven() || other.is_data_driven()) && self != other
125    }
126
127    /// Whether both values are constants, and differ.
128    fn constant_differs(&self, _other: &Self) -> bool {
129        false
130    }
131
132    /// Whether a change of the keys in `changed_keys` transitions the value
133    /// rather than snapping it.
134    fn transitions_on_global_state_change(&self, _changed_keys: Option<&VecSet<String>>) -> bool {
135        false
136    }
137
138    /// Keeps the value reading the global state from before `change`, if it
139    /// transitions on it.
140    fn retain_prior_global_state(&mut self, _change: &GlobalStateChange) {}
141}
142
143impl<T> StyleValue for PropertyValue<T>
144where
145    T: Converter + ValueConverter + ValueFactory + Clone + Debug + Default + PartialEq + 'static,
146{
147    type Type = T;
148
149    fn convert(value: &Convertible<'_>, info: &PropertyInfo, functions: &Arc<Functions>) -> Result<Self, Error> {
150        let mut error = Error::default();
151        convert_property_value::<T>(value, &mut error, info.data_driven, info.tokens, functions).ok_or(error)
152    }
153
154    fn style_property(&self) -> StyleProperty {
155        make_style_property(self)
156    }
157
158    fn evaluate(
159        &self,
160        evaluation: Evaluation<T>,
161        parameters: &PropertyEvaluationParameters,
162        default: T,
163    ) -> Evaluated<T> {
164        evaluation(self, parameters, default)
165    }
166
167    fn is_undefined(&self) -> bool {
168        matches!(self, PropertyValue::Undefined)
169    }
170
171    fn is_data_driven(&self) -> bool {
172        match self {
173            PropertyValue::Undefined | PropertyValue::Constant(_) => false,
174            PropertyValue::Expression(fn_) => !fn_.is_feature_constant(),
175        }
176    }
177
178    fn dependencies(&self) -> Dependency {
179        match self {
180            PropertyValue::Undefined | PropertyValue::Constant(_) => Dependency::NONE,
181            PropertyValue::Expression(ex) => ex.dependencies(),
182        }
183    }
184
185    fn global_state_refs(&self) -> Option<&VecSet<String>> {
186        match self {
187            PropertyValue::Undefined | PropertyValue::Constant(_) => None,
188            PropertyValue::Expression(ex) => ex.global_state_refs(),
189        }
190    }
191
192    fn constant_differs(&self, other: &Self) -> bool {
193        match (self, other) {
194            (PropertyValue::Constant(a), PropertyValue::Constant(b)) => a != b,
195            _ => false,
196        }
197    }
198
199    /// A value that reads a changed key transitions unless it differs by
200    /// feature or is a pattern image: images are never interpolated, so a
201    /// transition would only delay the image the tiles are laid out with.
202    fn transitions_on_global_state_change(&self, changed_keys: Option<&VecSet<String>>) -> bool {
203        T::expression_type() != Type::Image
204            && !self.is_data_driven()
205            && self.dependencies().intersects(Dependency::GLOBAL_STATE)
206            && global_state_refs_intersect(self.global_state_refs(), changed_keys)
207    }
208
209    fn retain_prior_global_state(&mut self, change: &GlobalStateChange) {
210        if self.transitions_on_global_state_change(Some(&change.changed_keys))
211            && let Some(expression) = self.as_expression_mut()
212        {
213            expression.pin_global_state(Arc::clone(&change.prior_state));
214        }
215    }
216}