mln-rs-style 0.0.21

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
use crate::conversion::Convertible;
use crate::expression::parse::{ParseResult, ParsingContext};
use crate::expression::type_::Type;
use crate::expression::value::Value;
use crate::expression::{EvaluationContext, EvaluationError, EvaluationResult, Expr, Node};
use crate::string::to_string_double;

/// `["at", index, array]`.
#[derive(Clone, PartialEq, mln_wire::Wire)]
pub(crate) struct At {
    index: Expr,
    input: Expr,
}

impl At {
    pub(crate) fn parse(value: &Convertible<'_>, ctx: &mut ParsingContext) -> ParseResult {
        debug_assert!(value.is_array());
        let length = value.array_length();
        if length != 3 {
            ctx.error(format!("Expected 2 arguments, but found {} instead.", length - 1));
            return None;
        }

        let index = ctx.parse(&value.array_member(1), 1, Some(Type::Number), None);
        let input_type = Type::array(ctx.expected().unwrap_or(Type::Value));
        let input = ctx.parse(&value.array_member(2), 2, Some(input_type), None);

        let (Some(index), Some(input)) = (index, input) else {
            return None;
        };
        debug_assert!(input.type_().is_array());
        Some(Expr::new(Node::At(Box::new(At { index, input }))))
    }

    /// The array's item type.
    pub(crate) fn type_(&self) -> Type {
        self.input.type_().as_array().map_or(Type::Value, |array| array.item_type.clone())
    }

    pub(crate) fn each_child<'a>(&'a self, visit: &mut dyn FnMut(&'a Expr)) {
        visit(&self.index);
        visit(&self.input);
    }

    pub(crate) fn evaluate(&self, params: &EvaluationContext<'_>) -> EvaluationResult {
        let evaluated_index = self.index.eval_num(params);
        let evaluated_input = self.input.evaluate_ref(params);
        let i = evaluated_index?;
        let evaluated_input = evaluated_input?;
        let Value::Array(input_array) = &*evaluated_input else {
            return Err(EvaluationError::type_mismatch(&self.input.type_(), &evaluated_input));
        };

        if i < 0.0 {
            return Err(EvaluationError::new(format!("Array index out of bounds: {} < 0.", to_string_double(i))));
        }

        if i >= input_array.len() as f64 {
            return Err(EvaluationError::new(format!(
                "Array index out of bounds: {} > {}.",
                to_string_double(i),
                to_string_double(input_array.len() as f64 - 1.0)
            )));
        }
        if i != i.floor() {
            return Err(EvaluationError::new(format!(
                "Array index must be an integer, but found {} instead.",
                to_string_double(i)
            )));
        }
        Ok(input_array[i as usize].clone())
    }
}