Skip to main content

mln_style/expression/forms/
format_section_override.rs

1//! `format-section-override`: a property's value that a section of a
2//! `formatted` value can override. The style installs it on a symbol layer's
3//! `text-color`; it has no style JSON of its own.
4
5use std::sync::Arc;
6
7use crate::color::Color;
8use crate::expression::value::Value;
9use crate::expression::{EvalResult, EvaluationContext, Expr, GlobalStateMap, Node, Output, SharedExpr};
10use crate::feature::Value as FeatureValue;
11
12/// The value a property takes where no section overrides it.
13#[derive(Clone, PartialEq, mln_wire::Wire)]
14pub enum OverrideDefault {
15    Constant(Color),
16    /// The property's expression, and the global state captured for it,
17    /// which stands in when an evaluation carries none.
18    Expression {
19        expression: SharedExpr,
20        captured_global_state: Option<Arc<GlobalStateMap>>,
21    },
22}
23
24#[derive(Clone, PartialEq, mln_wire::Wire)]
25pub struct FormatSectionOverride {
26    default_value: OverrideDefault,
27    property_name: String,
28}
29
30impl FormatSectionOverride {
31    /// An override of the color property `property_name`.
32    #[must_use]
33    pub fn new_expr(default_value: OverrideDefault, property_name: String) -> Expr {
34        Expr::new(Node::FormatSectionOverride(Box::new(FormatSectionOverride { default_value, property_name })))
35    }
36
37    #[must_use]
38    pub fn default_value(&self) -> &OverrideDefault {
39        &self.default_value
40    }
41
42    #[must_use]
43    pub fn property_name(&self) -> &str {
44        &self.property_name
45    }
46
47    pub(crate) fn each_child<'a>(&'a self, visit: &mut dyn FnMut(&'a Expr)) {
48        if let OverrideDefault::Expression { expression, .. } = &self.default_value {
49            visit(expression);
50        }
51    }
52
53    pub(crate) fn eval_color(&self, context: &EvaluationContext<'_>) -> EvalResult<Color> {
54        if let Some(Value::Object(section)) = context.formatted_section
55            && let Some(hit) = section.get(&self.property_name)
56        {
57            return Color::from_value(hit.clone());
58        }
59        match &self.default_value {
60            OverrideDefault::Expression { expression, captured_global_state } => {
61                // Binder contexts do not carry global state, so the captured
62                // state is used as a fallback. If the caller explicitly
63                // provides context state, it takes precedence.
64                if context.global_state.is_none()
65                    && let Some(state) = captured_global_state
66                {
67                    let mut context_with_state = *context;
68                    context_with_state.global_state = Some(state);
69                    return expression.eval_color(&context_with_state);
70                }
71                expression.eval_color(context)
72            }
73            OverrideDefault::Constant(color) => Ok(*color),
74        }
75    }
76
77    pub(crate) fn serialize(&self) -> FeatureValue {
78        let mut serialized = vec![FeatureValue::String("format-section-override".to_owned())];
79        self.each_child(&mut |child| serialized.push(child.serialize()));
80        FeatureValue::Array(serialized)
81    }
82}