mln-rs-style 0.0.21

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
use std::borrow::Cow;

use crate::conversion::Convertible;
use crate::expression::functions::Role;
use crate::expression::functions::builtin::{feature_unavailable, identifier_to_feature_value};
use crate::expression::parse::{ParseResult, ParsingContext};
use crate::expression::type_::{self, Type, check_subtype};
use crate::expression::value::{Value, type_of};
use crate::expression::{EvalResult, EvaluationContext, EvaluationError, EvaluationResult, Expr, Node, Output};
use crate::feature::Value as FeatureValue;

/// `["number", ...]` and the other type assertions: the first input whose
/// value has the type, or an error.
#[derive(Clone, PartialEq, mln_wire::Wire)]
pub(crate) struct Assertion {
    type_: Type,
    inputs: Box<[Expr]>,
    /// Whether each input has the asserted type, so its typed evaluation
    /// needs no check.
    typed: Box<[bool]>,
}

fn assertion_type(name: &str) -> Option<Type> {
    match name {
        "string" => Some(Type::String),
        "number" => Some(Type::Number),
        "boolean" => Some(Type::Boolean),
        "object" => Some(Type::Object),
        _ => None,
    }
}

impl Assertion {
    pub(crate) fn new_expr(type_: Type, inputs: Vec<Expr>) -> Expr {
        assert!(!inputs.is_empty(), "Assertion without inputs");
        let typed = inputs.iter().map(|input| input.type_() == type_).collect();
        Expr::new(Node::Assertion(Box::new(Assertion { type_, inputs: inputs.into(), typed })))
    }

    pub(crate) fn type_(&self) -> &Type {
        &self.type_
    }

    pub(crate) fn inputs(&self) -> &[Expr] {
        &self.inputs
    }

    pub(crate) fn parse(value: &Convertible<'_>, ctx: &mut ParsingContext) -> ParseResult {
        let length = value.array_length();

        if length < 2 {
            ctx.error("Expected at least one argument.".to_owned());
            return None;
        }

        let mut i = 1;

        let Some(name) = value.array_member(0).to_string() else {
            ctx.error("Expected the assertion name to be a string.".to_owned());
            return None;
        };
        let type_ = if name == "array" {
            let item_type;
            if length > 2 {
                let item_type_name = value.array_member(1).to_string();
                let it = item_type_name.as_deref().and_then(assertion_type);
                match it {
                    Some(t) if t != Type::Object => item_type = t,
                    _ => {
                        ctx.error_at(
                            r#"The item type argument of "array" must be one of string, number, boolean"#.to_owned(),
                            1,
                        );
                        return None;
                    }
                }
                i += 1;
            } else {
                item_type = Type::Value;
            }

            let mut n = None;
            if length > 3 {
                let m = value.array_member(2);
                let num = m.to_number();
                if !m.is_undefined() && num.is_none_or(|v| v < 0.0 || v != v.floor()) {
                    ctx.error_at(r#"The length argument to "array" must be a positive integer literal."#.to_owned(), 2);
                    return None;
                }
                if let Some(num) = num {
                    n = Some(num as usize);
                }
                i += 1;
            }

            type_::Array::with_n(item_type, n).into()
        } else {
            let Some(type_) = assertion_type(&name) else {
                ctx.error(format!("Unknown assertion type {name}."));
                return None;
            };
            type_
        };

        let mut parsed = Vec::with_capacity(length - 1);
        while i < length {
            let input = ctx.parse(&value.array_member(i), i, Some(Type::Value), None)?;
            parsed.push(input);
            i += 1;
        }

        Some(Assertion::new_expr(type_, parsed))
    }

    pub(crate) fn evaluate(&self, params: &EvaluationContext<'_>) -> EvaluationResult {
        // The parser makes an assertion of at least one input.
        let mut mismatch = Value::Null;
        for input in &self.inputs {
            let value = input.evaluate(params)?;
            if check_subtype(&self.type_, &type_of(&value)).is_none() {
                return Ok(value);
            }
            mismatch = value;
        }
        Err(self.mismatch(&mismatch))
    }

    fn mismatch(&self, value: &Value) -> EvaluationError {
        EvaluationError::type_mismatch(&self.type_, value)
    }

    /// The typed evaluations: an input of the asserted type evaluates typed,
    /// and a feature accessor (`["get", k]`, `["id"]`) is read without
    /// building a value first.
    fn eval_typed<'e, T: Output<'e>>(
        &'e self,
        params: &EvaluationContext<'_>,
        from_feature: impl Fn(&FeatureValue) -> Option<T>,
    ) -> EvalResult<T> {
        let mut mismatch = Value::Null;
        for (input, &typed) in self.inputs.iter().zip(&self.typed) {
            if typed {
                return T::eval(input, params);
            }
            if let Some(read) = read_feature(input, params) {
                match read? {
                    Some(value) => match from_feature(&value) {
                        Some(typed) => return Ok(typed),
                        None => mismatch = crate::expression::value::from_feature_value_owned(value),
                    },
                    None => mismatch = Value::Null,
                }
                continue;
            }
            let value = input.evaluate(params)?;
            if check_subtype(&self.type_, &type_of(&value)).is_none() {
                return T::from_value(value);
            }
            mismatch = value;
        }
        Err(self.mismatch(&mismatch))
    }

    pub(crate) fn eval_num(&self, params: &EvaluationContext<'_>) -> EvalResult<f64> {
        self.eval_typed(params, |v| match *v {
            FeatureValue::Double(n) => Some(n),
            FeatureValue::Uint(n) => Some(n as f64),
            FeatureValue::Int(n) => Some(n as f64),
            _ => None,
        })
    }

    pub(crate) fn eval_bool(&self, params: &EvaluationContext<'_>) -> EvalResult<bool> {
        self.eval_typed(params, |v| match *v {
            FeatureValue::Bool(b) => Some(b),
            _ => None,
        })
    }

    pub(crate) fn eval_str<'e>(&'e self, params: &EvaluationContext<'_>) -> EvalResult<Cow<'e, str>> {
        self.eval_typed(params, |v| match v {
            FeatureValue::String(s) => Some(Cow::Owned(s.clone())),
            _ => None,
        })
    }

    pub(crate) fn serialize(&self) -> FeatureValue {
        let mut serialized = vec![FeatureValue::String(self.operator().to_owned())];

        if let Type::Array(array) = &self.type_
            && matches!(array.item_type, Type::String | Type::Number | Type::Boolean)
        {
            serialized.push(FeatureValue::String(type_::to_string(&array.item_type)));
            if let Some(n) = array.n {
                serialized.push(FeatureValue::Uint(n as u64));
            } else if self.inputs.len() > 1 {
                serialized.push(FeatureValue::Null);
            }
        }

        for input in &self.inputs {
            serialized.push(input.serialize());
        }

        FeatureValue::Array(serialized)
    }

    pub(crate) fn operator(&self) -> &'static str {
        match self.type_ {
            Type::Array(_) => "array",
            Type::String => "string",
            Type::Number => "number",
            Type::Boolean => "boolean",
            Type::Object => "object",
            _ => unreachable!("assertion to {}", self.type_),
        }
    }
}

/// The feature value or id that `input` reads, when it is `["get", key]`
/// with a literal key or `["id"]`: `Some(Ok(None))` when the feature lacks
/// it, and `None` when `input` is another expression.
fn read_feature(input: &Expr, params: &EvaluationContext<'_>) -> Option<EvalResult<Option<FeatureValue>>> {
    let compound = input.as_compound()?;
    let overload = compound.overload();
    match overload.role {
        Role::FeatureProperty if overload.result == Type::Value => {
            let key = compound.args()[0].literal_value()?.as_str()?;
            Some(match params.feature {
                Some(feature) => Ok(feature.value(key)),
                None => Err(feature_unavailable()),
            })
        }
        Role::FeatureId => Some(match params.feature {
            Some(feature) => Ok(identifier_to_feature_value(feature.id())),
            None => Err(feature_unavailable()),
        }),
        _ => None,
    }
}