stylex_macros/collection_macros.rs
1//! Collection and iteration helper macros.
2//!
3//! These macros simplify common patterns when collecting values from evaluations
4//! and iterating over data structures.
5
6/// Collects an evaluation result if it's confident, otherwise returns None.
7///
8/// This macro simplifies the common pattern of checking if an evaluation is confident
9/// and either collecting the value or early-returning from the function.
10///
11/// # Usage
12/// ```ignore
13/// // Basic usage - pushes value if confident, returns None if not
14/// for elem in arr_path.elems.iter().flatten() {
15/// let elem_value = evaluate(&elem.expr, traversal_state, &state.functions);
16/// collect_confident!(elem_value, arr);
17/// }
18///
19/// // With transformation - applies transform before pushing
20/// collect_confident!(elem_value, collection, |v| transform(v));
21/// ```
22///
23/// # Arguments
24/// - `$eval_result`: An evaluation result with a `confident` field and optional `value`
25/// - `$collection`: The collection to push values into
26/// - `$transform` (optional): A transformation function to apply to the value before pushing
27#[macro_export]
28macro_rules! collect_confident {
29 ($eval_result:expr, $collection:expr) => {
30 if $eval_result.confident {
31 $collection.push($eval_result.value);
32 } else {
33 return None;
34 }
35 };
36 ($eval_result:expr, $collection:expr, $transform:expr) => {
37 if $eval_result.confident {
38 $collection.push($transform($eval_result.value));
39 } else {
40 return None;
41 }
42 };
43}