oxblade 0.2.0

Laravel Blade templates for Rust: Blade's markup and directives with Rust expressions, rendering the same HTML as Laravel. Community package, not affiliated with Laravel.
Documentation
//! Laravel collection operations on Rust lists and insertion-ordered maps.
//!
//! List results compact indices after filtering, sorting, slicing, reversing, set operations,
//! grouping and chunking. Maps keep keys unless an operation explicitly emits values, and retain
//! their shape when empty. Group keys appear in first-seen order. Map equality compares fields
//! irrespective of insertion order, while list equality compares in order.
//!
//! The selector methods (`where`, membership, uniqueness, search, set operations and their
//! strict aliases) compare without PHP coercion. Any accepted PHP strictness flags leave that
//! behavior in place. Predicates require booleans, and `filter()` with no predicate removes
//! missing values. Ordering requires compatible Rust types, and ties in either sort direction
//! retain input order. Numeric aggregates accept homogeneous integers or floats, integer sums
//! check overflow, and `avg` always returns a float when it finds a result. A failed search is
//! `None`.
//!
//! `len`, `rev` and `join` replace Laravel's `count`, `reverse` and `implode`. Join uses Rust's
//! display formatting. The collection JSON methods use standard JSON escaping, and PHP-specific
//! JSON and sort flags are outside native dispatch. Collection constructors
//! produce lists and maps directly. `range` uses integer endpoints and steps. Map keys are
//! strings, and integer keys emitted into maps use their decimal spelling.
//!
//! In-place methods return the replacement collection. For operations that return an extracted
//! item in PHP (`pull`, `pop`, `shift`, `splice`, `get_or_put`), use lookup before the update
//! to read the old item. The static `Arr` update helpers also return the resulting collection.
//! Consult `design/DECISIONS.md` for operations requiring PHP objects, shared mutation, laziness,
//! or randomness.

use crate::{
    error::Result,
    library::CallLimits,
    value::{Arg, Value},
};

mod arrange;
mod arrays;
mod mutate;
mod select;
mod support;
mod transform;

use support::{Args, is_collection};

/// Dispatches a collection method for a list, map, or borrowed JSON collection.
///
/// Returns `None` for names outside the collection library or scalar receivers. The evaluator
/// attaches source information to errors and writes replacement collections to mutable bindings.
pub fn method<'a>(
    name: &str,
    receiver: &Value<'a>,
    args: Vec<Arg<'a, '_>>,
) -> Option<Result<Value<'a>>> {
    method_with_limits(name, receiver, args, CallLimits::default())
}

pub(crate) fn method_with_limits<'a>(
    name: &str,
    receiver: &Value<'a>,
    args: Vec<Arg<'a, '_>>,
    limits: CallLimits,
) -> Option<Result<Value<'a>>> {
    if !is_collection(receiver) {
        return None;
    }

    let mut args = Args(args, limits);
    macro_rules! dispatch {
        ($($group:ident { $($method:literal : $min:literal $(, $max:expr)?;)* })*) => {
            match name {
                $($($method => {
                    let max = [$min $(, $max)?].into_iter().last().unwrap_or($min);
                    args.count(name, $min, max)
                        .and_then(|()| $group::run(name, receiver, &mut args))
                },)*)*
                _ => return None,
            }
        };
    }

    Some(dispatch! {
        select {
            "len": 0;
            "is_empty": 0;
            "is_not_empty": 0;
            "filter": 0, 1;
            "reject": 0, 1;
            "where": 1, 3;
            "where_by": 1;
            "where_strict": 2;
            "where_null": 0, 1;
            "where_not_null": 0, 1;
            "where_in": 2, 3;
            "where_in_strict": 2;
            "where_not_in": 2, 3;
            "where_not_in_strict": 2;
            "where_between": 2;
            "where_not_between": 2;
            "where_instance_of": 1;
            "first_where": 1, 3;
            "first": 0, 2;
            "last": 0, 2;
            "sole": 0, 3;
            "first_or_fail": 0, 3;
            "has_sole": 0, 3;
            "has_many": 0, 3;
            "contains": 1, 3;
            "some": 1, 3;
            "doesnt_contain": 1, 3;
            "doesnt_contain_strict": 1, 3;
            "contains_strict": 1, 2;
            "contains_one_item": 0, 1;
            "contains_many_items": 0, 1;
            "every": 1, 3;
            "partition": 1, 3;
            "percentage": 1, 2;
            "ensure": 1;
            "before": 1, 2;
            "after": 1, 2;
            "search": 1, 2;
            "diff": 1;
            "diff_using": 2;
            "diff_assoc": 1;
            "diff_assoc_using": 2;
            "diff_keys": 1;
            "diff_keys_using": 2;
            "intersect": 1;
            "intersect_using": 2;
            "intersect_assoc": 1;
            "intersect_assoc_using": 2;
            "intersect_by_keys": 1;
            "only_values": 1, 2;
            "except_values": 1, 2;
        }
        transform {
            "all": 0;
            "collect": 0;
            "to_base": 0;
            "to_array": 0;
            "json_serialize": 0;
            "to_json": 0;
            "to_pretty_json": 0;
            "map": 1;
            "transform": 1;
            "map_spread": 1;
            "map_to_dictionary": 1;
            "map_to_groups": 1;
            "map_with_keys": 1;
            "flat_map": 1;
            "pluck": 1, 2;
            "key_by": 1;
            "group_by": 1, 2;
            "count_by": 0, 1;
            "keys": 0;
            "values": 0;
            "divide": 0;
            "flip": 0;
            "select": 1;
            "sum": 0, 1;
            "avg": 0, 1;
            "average": 0, 1;
            "min": 0, 1;
            "max": 0, 1;
            "median": 0, 1;
            "mode": 0, 1;
            "reduce": 1, 2;
            "reduce_with_keys": 1, 2;
            "reduce_spread": 1, usize::MAX;
            "pipe": 1;
            "pipe_through": 1;
            "when": 2, 3;
            "unless": 2, 3;
            "when_empty": 1, 2;
            "when_not_empty": 1, 2;
            "unless_empty": 1, 2;
            "unless_not_empty": 1, 2;
            "join": 1, 2;
            "to_css_classes": 0;
            "to_css_styles": 0;
            "query": 0;
        }
        arrange {
            "rev": 0;
            "sort": 0, 1;
            "sort_desc": 0;
            "sort_by": 1, 3;
            "sort_by_desc": 1, 2;
            "sort_keys": 0, 2;
            "sort_keys_desc": 0, 1;
            "sort_keys_using": 1;
            "sort_recursive": 0, 2;
            "sort_recursive_desc": 0, 1;
            "unique": 0, 2;
            "unique_strict": 0, 1;
            "duplicates": 0, 2;
            "duplicates_strict": 0, 1;
            "chunk": 1, 2;
            "chunk_while": 1;
            "chunk_by": 1;
            "split": 1;
            "split_in": 1;
            "sliding": 0, 2;
            "take": 1;
            "take_until": 1;
            "take_while": 1;
            "skip": 1;
            "skip_until": 1;
            "skip_while": 1;
            "slice": 1, 2;
            "for_page": 2;
            "nth": 1, 2;
            "flatten": 0, 1;
            "collapse": 0;
            "collapse_with_keys": 0;
            "dot": 0, 1;
            "undot": 0;
            "zip": 1, usize::MAX;
            "cross_join": 0, usize::MAX;
        }
        mutate {
            "push": 0, usize::MAX;
            "add": 1;
            "unshift": 0, usize::MAX;
            "prepend": 1, 2;
            "pop": 0, 1;
            "shift": 0, 1;
            "splice": 1, 3;
            "put": 2;
            "set": 2;
            "get_or_put": 2;
            "forget": 1;
            "pull": 1, 2;
            "merge": 1;
            "merge_recursive": 1;
            "replace": 1;
            "replace_recursive": 1;
            "concat": 1;
            "union": 1;
            "combine": 1;
            "multiply": 1;
            "pad": 2;
            "prepend_keys_with": 1;
        }
        arrays {
            "get": 1, 2;
            "value": 1, 2;
            "array": 1, 2;
            "boolean": 1, 2;
            "float": 1, 2;
            "integer": 1, 2;
            "string": 1, 2;
            "contains_key": 1;
            "exists": 1;
            "offset_exists": 1;
            "offset_get": 1;
            "offset_set": 2;
            "offset_unset": 1;
            "has": 1, usize::MAX;
            "has_all": 1, usize::MAX;
            "has_any": 1, usize::MAX;
            "is_assoc": 0;
            "is_list": 0;
            "only": 1;
            "except": 1;
        }
    })
}

/// Dispatches static constructors and `Arr` helpers with their array argument first.
///
/// Use `collection::` or `arr::` as the namespace. Static array operations whose signature
/// differs from a collection method use the corresponding Laravel array signature.
pub fn function<'a>(name: &str, args: Vec<Arg<'a, '_>>) -> Option<Result<Value<'a>>> {
    function_with_limits(name, args, CallLimits::default())
}

pub(crate) fn function_with_limits<'a>(
    name: &str,
    args: Vec<Arg<'a, '_>>,
    limits: CallLimits,
) -> Option<Result<Value<'a>>> {
    arrays::function(name, args, limits)
}